UINavigationItem touchView touch size - ios

I'm setting a view on my UINavigationItem's touchView and adding a tap gesture recognizer to it. What's weird, is that the tap recognizer is getting called even when the tap is outside of the view. Any idea why this might be happening?
UIView *testView = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 120.0f, 20.0f)];
testView.backgroundColor = [UIColor redColor];
UIGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(doStuff:)];
tapRecognizer.cancelsTouchesInView = YES;
[testView addGestureRecognizer:tapRecognizer];
testView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
self.navigationItem.titleView = testView;
I can click outside the red box and still trigger the gesture recognizer.

use this way
UITapGestureRecognizer *tapRecognizer =
[[UITapGestureRecognizer alloc] initWithTarget:self
action:#selector(doStuff:)];
instead of this code
UIGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(doStuff:)];

Related

iOS UITapGestureRecognizer not working sometimes

Sometimes the selector method "tagClickAtIndex" not getting called.
UILabel* label = [[UILabel alloc] init];
label.userInteractionEnabled = YES;
UITapGestureRecognizer* tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(tagClickAtIndex:)];
[label addGestureRecognizer:tapGesture];
-(void)tagClickAtIndex:(UITapGestureRecognizer*)gesture
{
NSMutableDictionary *mutDict = [[[NSMutableDictionary alloc]initWithDictionary:[_tagArray objectAtIndex:gesture.view.tag]] mutableCopy];
[mutDict setValue:[NSNumber numberWithLong:gesture.view.tag] forKey:#"index"];
[self.delegate tagClickAtIndex:mutDict];
}
you forget to set the frame of label, the reason it only define the clickable area
label.frame = CGRectMake(0,0,200,30)
[yourMainview addSubView:label]
and set if you need
tapGesture.numberOfTapsRequired = 1;
If you are programmatically creating UIlabel,your above code is not working.I tried your code.
UILabel* label = [[UILabel alloc] init];
label.userInteractionEnabled = YES;
UITapGestureRecognizer* tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(tagClickAtIndex:)];
[label addGestureRecognizer:tapGesture];
It does not show anything.As you have not set the frame and add label view,it does not show.
So Then I tried creating UILabel freshly.I set frame,addSubView to self.view and I set the color as well as number of taps for label.
UILabel* label = [[UILabel alloc] init];
label.frame = CGRectMake(30, 100, 200, 100);
label.text = #"click me";
label.textColor = [UIColor blueColor];
[self.view addSubview:label];
Now Tap gesture recognizer for label
UITapGestureRecognizer* tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(tagClickAtIndex:)];
tapGesture.numberOfTapsRequired = 1;
label.userInteractionEnabled = YES;
[label addGestureRecognizer:tapGesture];
in delegate method I put log,It calls everytime whenever I touch the label
-(void)tagClickAtIndex:(UITapGestureRecognizer*)gesture
{
NSLog(#"The tap is called");
.........
}
The printed statements shows
The tap is called

UILabel on Multi gesture view

I have a third party view from a framework with multiple touch gestures and I want to add a UILabel on top that handles a single tap event. For some reason, the touch event on the UILabel does not work when it is a subview of the third party view, but when I switch the view to a normal UIView it works as intended. Here I make the label:
CGRect frame = CGRectMake(0, 0, 160, 40);
UILabel* label = [[UILabel alloc] initWithFrame:frame];
label.text = [NSString stringWithFormat:#"label"];
label.userInteractionEnabled = YES;
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self
action:#selector(labelTapped:)];
singleTap.numberOfTapsRequired = 1;
[label addGestureRecognizer:singleTap];

TapGesture recognizer on multiple UIImageView not working

TapGesture recognizer on multiple UIImageView is not working, while it detects last added imageviews gesture.. I have done this,
UITapGestureRecognizer *tapped = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(myFunction:)];
tapped.numberOfTapsRequired = 1;
tapped.delegate = self;
UIImageView *sample_book1= [[UIImageView alloc]initWithFrame:CGRectMake(70, 135, 100,125) ];
sample_book1.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"mathematics.png"]];
sample_book1.userInteractionEnabled = YES;
sample_book1.tag = 0;
[sample_book1 addGestureRecognizer:tapped];
[self.view addSubview:sample_book1];
UIImageView *sample_book2= [[UIImageView alloc]initWithFrame:CGRectMake(220, 135, 100,125) ];
sample_book2.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"biology.png"]];
sample_book2.userInteractionEnabled = YES;
sample_book2.tag = 1;
[sample_book2 addGestureRecognizer:tapped];
[self.view addSubview:sample_book2];
UIImageView *sample_book3= [[UIImageView alloc]initWithFrame:CGRectMake(370, 135, 100,125) ];
sample_book3.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"chemistry.png"]];
sample_book3.userInteractionEnabled = YES;
sample_book3.tag = 2;
[sample_book3 addGestureRecognizer:tapped];
[self.view addSubview:sample_book3];
The tap gesture is not working in sample_book1,sample_book2.... it's only working in sample_book3.. What i'm doing wrong..
As borrrden said, when trying to track gesture, each view must have its own gestureRecognizer.
For each of your sample_books, you should use
[sample_bookX addGestureRecognizer:[[UITapGestureRecognizer alloc]initWithTarget:self action:#selector(myFunction:)]];
instead of trying to add multiple times the same GR
The argument received by myFunction: would then be the proper tapGR and you could get to the tapped imageView by calling sender.view (providing your myFunction signature look like
- (void) myFunction:(UIGestureRecognizer *)sender
Cheers,
What you are doing wrong is trying to use the gesture in a way that it is not supposed to be used. A gesture can only be attached to one view. You need to make a new one for each view.

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