UIPinchGestureRecognizer questions with subviews - ios

I have the following code:
m_singleView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, nWidth - 1, nHeight - 1)];
m_singleView.backgroundColor = [UIColor clearColor];
UIPinchGestureRecognizer *pinchGesture = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:#selector(pinch:)];
[pinchGesture setCancelsTouchesInView:YES];
[m_singleView addGestureRecognizer:pinchGesture];
[m_singleView setUserInteractionEnabled:YES];
[m_MainView addSubview:m_singleView];
The issue that I'm having is that the pinch event for some reason does not fire. However, if i change line from [m_singleView addGestureRecognizer:pinchGesture] to [m_MainView addGestureRecognizer:pinchGesture]; then everything will work fine... can I not add the event for subview only?
Thanks!

Yes,you can add gesture to subview. i tested your code like below works fine.
First add delegate.
#interface ViewController : UIViewController<UIGestureRecognizerDelegate>
- (void)viewDidLoad {
[super viewDidLoad];
UIView *m_singleView = [[UIView alloc] initWithFrame:CGRectMake(50, 50, self.view.frame.size.width - 50, self.view.frame.size.height - 50)];
self.view.backgroundColor=[UIColor greenColor];
m_singleView.backgroundColor = [UIColor redColor];
UIPinchGestureRecognizer *pinchGesture = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:#selector(pinch)];
pinchGesture.delegate=self;
[pinchGesture setCancelsTouchesInView:YES];
[m_singleView addGestureRecognizer:pinchGesture];
[m_singleView setUserInteractionEnabled:YES];
[self.view addSubview:m_singleView];
}
-(void)pinch{
NSLog(#"In PInch");
}
you have used pinchgesture so you can use it like below.

Use Debug view hierarchy to see whether the size of m_singleView are 0 or not? If so, the size of m_singleView needs to be changed until you can touch it.

Related

UIGestureRecognizer in objective-C++

I want to know the UIGestureRecognizer working or not in Objective-C++ because i've implemented this one but tap method never calling. So please let me know is it possible or not in Objective-C++.
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(100, 100, 200, 200)];
imgView.image = [UIImage imageNamed:#"dharm"];
[self.view addSubview:imgView];
imgView.backgroundColor = [UIColor redColor];
UITapGestureRecognizer* tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(tap:)];
tapRecognizer.numberOfTapsRequired = 1;
[tapRecognizer setDelegate:self];
[imgView addGestureRecognizer:tapRecognizer];
}
- (void)tap:(id)sender {
NSLog(#"Tap Pressed");
}
Try adding [imgView setUserInteractionEnabled:YES];

Touch events on views inside UI Scroll View

I have Implemented a UIScrollView on MainView as follows:
UIScrollView* scrollView =[[UIScrollView alloc]initWithFrame:CGRectMake(-160, -540, 480, 1300)];
[scrollView addSubview:self.MainView];
scrollView.contentSize = CGSizeMake(400, 2000);
[self.view addSubview:scrollView];
I have the following Views under MainView in Storyboard:
Main View ----> View1, View2
I want to implement touch events with View 1 and View 2 separately. For that I have the following code:
UITapGestureRecognizer *tapSlidersView1 = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(tapSlidersActionView1:)];
[self.View1 addGestureRecognizer:tapSlidersView1];
UITapGestureRecognizer *tapSlidersView2 = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(tapSlidersActionView2:)];
[self.View2 addGestureRecognizer:tapSlidersView2];
And the corresponding event handlers:
- (void)tapSlidersActionView1:(UITapGestureRecognizer *)sender
{
[self.navigationController pushViewController:DropDownView1 animated:YES];
}
- (void)tapSlidersActionView2:(UITapGestureRecognizer *)sender
{
[self.navigationController pushViewController:DropDownView2 animated:YES];
}
The tap events does not seem to work at all. please suggest. Thanks in advance
First thing, you have to make sure that your view is touchable by enabling userInteractionEnabled.
Second, you need to declare numberOfTapsRequired for your tap event.
Then you have your code like this:
UITapGestureRecognizer *tapSlidersView1 = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(tapSlidersActionView1:)];
tapSlidersView1.numberOfTapsRequired = 1;
self.View1.userInteractionEnabled = YES;
[self.View1 addGestureRecognizer:tapSlidersView1];
Try following code and also every view is created programmatically..
- (void)viewDidLoad
{
[super viewDidLoad];
UIView *mainView=[[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height)];
[mainView setBackgroundColor:[UIColor blackColor ]];
[self.view addSubview:mainView];
UIScrollView* scrollView =[[UIScrollView alloc]initWithFrame:CGRectMake(0, 0,mainView .bounds.size.width, mainView.bounds.size.height)];
scrollView.contentSize = CGSizeMake(400, 2000);
[mainView addSubview:scrollView];
UIView *View1=[[UIView alloc]initWithFrame:CGRectMake(50, 150, 100, 100)];
[View1 setBackgroundColor:[UIColor grayColor]];
[scrollView addSubview:View1];
UIView *View2=[[UIView alloc]initWithFrame:CGRectMake(200, 150, 100, 100)];
[View2 setBackgroundColor:[UIColor greenColor]];
[scrollView addSubview:View2];
UITapGestureRecognizer *tapSlidersView1 = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(tapSlidersActionView1:)];
[View1 addGestureRecognizer:tapSlidersView1];
UITapGestureRecognizer *tapSlidersView2 = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(tapSlidersActionView2:)];
[View2 addGestureRecognizer:tapSlidersView2];
// Do any additional setup after loading the view from its nib.
}
- (void)tapSlidersActionView1:(UITapGestureRecognizer *)sender
{
//your class
// secViewController *DropDownView1=[[secViewController alloc]init];
[self.navigationController pushViewController:DropDownView1 animated:YES];
}
- (void)tapSlidersActionView2:(UITapGestureRecognizer *)sender
{
//your class
// secViewController *DropDownView2=[[secViewController alloc]init];
[self.navigationController pushViewController:DropDownView2 animated:YES];
}

(WeeLoader) 'UITapGestureRecognizer' was not declared in this scope

I know that when the error is "'blah' was not declared in this scope" means when the object was not created correctly, but when I am making a Notification Center widget for iOS, using the WeeLoader template and THEOS to compile, I am getting this error: 'UITapGestureRecognizer' was not declared in this scope.
Here is my .mm file:
- (void)loadFullView {
UITapGestureRecognizer *Tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleSingleTap:)];
UIImage *bg = [[UIImage imageWithContentsOfFile:#"/System/Library/WeeAppPlugins/WeeAppTest.bundle/WeeAppBackground.png"] stretchableImageWithLeftCapWidth:5 topCapHeight:71];
UIImageView *bgView = [[UIImageView alloc] initWithImage:bg];
bgView.frame = CGRectMake(0, 0, 316, 71);
bgView.userInteractionEnabled = YES;
[bgView addGestureRecognizer:Tap];
[_view addSubview:bgView];
[bgView release];
[Tap release];
UILabel *lbl = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 316, 71)];
lbl.backgroundColor = [UIColor clearColor];
lbl.textColor = [UIColor whiteColor];
lbl.text = #"Hello world";
lbl.textAlignment = UITextAlignmentCenter;
[_view addSubview:lbl];
[lbl release];
}
-(void) handleTapGesture:(UIGestureRecognizer *) sender {
}
What am I doing wrong? I am pretty sure that I am declaring everything correctly, by the way, the code does work with out the UITapGestureRecognizer.
Thank you.
I think there are a few things going on here.
I think you could be getting the out of scope error because the gesture recognizer should be declared in the viewDidLoad or the init methods.
Also, in the UITapGestureRecognizer *Tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleSingleTap:)]; You call your method handleSingleTap:
Therefore your method should be -(void) handleSingleTap:(UITapGestureRecognizer *) sender

Detect tap on UIImageView inside UIScrollView

I have a horizontal scrollview filled with UIImageViews.
I want to detect a tap on the UIImageView and have its background color changed.
Somehow the tap gesture is not working or so.
However, when I add a tap gesture to the scrollview, it works. The scrollview.background color can be changed.
But I want to detect a tap on the UIImageViews it contains!
UIScrollView* scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 50, 768, 127)];
[scrollView setScrollEnabled:YES];
scrollView.backgroundColor = [UIColor orangeColor];
[scrollView setShowsHorizontalScrollIndicator:NO];
UIImageView *contentOfScrollView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 1, 1130, 125)];
scrollView.contentSize = CGSizeMake(contentOfScrollView.frame.size.width, contentOfScrollView.frame.size.height);
for (int aantal=0; aantal < 6; aantal++) {
UIImageView *item = [[UIImageView alloc] initWithFrame:CGRectMake(3+(aantal*188), 0, 185, 125)];
item.backgroundColor = [UIColor yellowColor];
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:item action:#selector(imageTapped:)];
tap.numberOfTapsRequired = 1;
tap.cancelsTouchesInView=YES;
item.userInteractionEnabled = YES;
[item addGestureRecognizer:tap];
[contentOfScrollView addSubview:item];
}
//UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(imageTapped:)];
//[scrollView addGestureRecognizer:tap];
scrollView.userInteractionEnabled=YES;
scrollView.delaysContentTouches=NO;
[scrollView addSubview:contentOfScrollView];
[self.view addSubview:scrollView];
And this is the imageTapped function.
-(void)imageTapped:(UITapGestureRecognizer *)gesture
{
NSLog(#"tapped!");
gesture.view.backgroundColor = [UIColor whiteColor];
}
User interaction is set to NO by default for UIImageView, so you need to set it to YES.
You set it to yes for "item", but not for contentOfScrollView.
Your error is here:
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:item action:#selector(imageTapped:)];
You need to change the target to "self" instead of "item", then it won't crash.
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(imageTapped:)];

uibutton only showing in the newly added uiview

I'm stuck at showing the uibutton *removeSticker, please help.
- (void)viewWillAppear:(BOOL)animated {
UIView *holderView = [[UIView alloc] initWithFrame:CGRectMake(0, 50, _imagePicker.selectedImage.size.width, _imagePicker.selectedImage.size.height)];
UIImageView *imageView = [[UIImageView alloc] initWithFrame:[holderView frame]];
[imageView setImage:_imagePicker.selectedImage];
[holderView addSubview:imageView];
removeSticker = [UIButton buttonWithType:UIButtonTypeCustom];
removeSticker.frame = CGRectMake(0, 0, 32, 32);
[removeSticker setImage:[UIImage imageNamed:#"cancel-disabled.png"] forState:UIControlStateNormal];
[removeSticker addTarget:self action:#selector(buttonClicked:) forControlEvents:UIControlEventTouchDown];
[holderView addSubview: removeSticker];
[removeSticker setHidden: YES];
UIPinchGestureRecognizer *pinchRecognizer = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:#selector(scale:)];
[pinchRecognizer setDelegate:self];
[holderView addGestureRecognizer:pinchRecognizer];
UIRotationGestureRecognizer *rotationRecognizer = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:#selector(rotate:)];
[rotationRecognizer setDelegate:self];
[holderView addGestureRecognizer:rotationRecognizer];
UIPanGestureRecognizer *panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:#selector(move:)];
[panRecognizer setMinimumNumberOfTouches:1];
[panRecognizer setMaximumNumberOfTouches:1];
[panRecognizer setDelegate:self];
[holderView addGestureRecognizer:panRecognizer];
UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(tapped:)];
[tapRecognizer setNumberOfTapsRequired:1];
[tapRecognizer setDelegate:self];
[holderView addGestureRecognizer:tapRecognizer];
UILongPressGestureRecognizer *longPressRecognizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:#selector(longPress:)];
[longPressRecognizer setDelegate:self];
[holderView addGestureRecognizer:longPressRecognizer];
[stickerView addSubview:holderView];
}
- (void)buttonClicked:(id)sender{
UIView *holderView = [(UIButton *)sender superview]; [holderView removeFromSuperview];
}
The uibutton *removeSticker only appears on the latest added uiview *holderView, if I want to edit the previously added once, nothing occurs. Please help, thanks a lot.
-(void)longPress:(id)sender {
[removeSticker setHidden:NO];
}
Please check the attached screenshot: http://i.stack.imgur.com/moMOj.png
For the screenshot above, bottle and watermelon are *holderView.
I've added the bottle first, and then added the watermelon afterwards, however when I click on the bottle, [removeSticker setHidden:NO]; shows up in the watermelon instead of the bottle.
[removeSticker setHidden:NO]; only shows up on the latest newly added *holderView.
From Apple's documentation:
"A view can have only one superview at the same time."
So if you add a view as a subview to another view, the subview will be removed from its previous superview and added to the new one. (see the addSubview method's documentation).
(consider thinking about why UIView has a single #property of type UIView *superview, and not an NSArray named superviews [in plural]).
Edit: if you actually want to have multiple views in multiple superviews, you'll have to create multiple instances of your UIView and add the multiple instances to the various superviews. You'll also want to take care of the corresponding UIView instances, so you may add them to an NSArray and manage that aeray using an NSDictionary.

Resources