UITapGestureRecognizer on UILabels in subview of UIScrollView not working - ios

I have a problem where my UITapGestureRecognizer on my UILabels in a content view in my UIScrollView is not calling it's methods.
The view hierarchy is as follows:
scrollView (UIScrollView)
contentView (UIView)
testLabel (UILabel) - here is where the UITapGestureRecognizer is attached
I have distilled the code down to an example to highlight the problem
// Set scrollview size - Added in Storyboad
[scrollView setContentSize:CGSizeMake([arrayOfVerbs count]*self.view.frame.size.width, scrollView.contentSize.height)];
[scrollView setCanCancelContentTouches:YES]; // Tried both yes and no
[scrollView setPagingEnabled:YES];
// Add content view
UIView *contentView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, scrollView.contentSize.width, scrollView.contentSize.height)];
[scrollView addSubview:contentView];
// Add test UILabel
UILabel *testLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 200, 100)];
[testLabel setBackgroundColor:[UIColor redColor]];
[testLabel setText:#"Test touch"];
[testLabel setUserInteractionEnabled:YES];
[contentView addSubview:testLabel];
// Add gesture recogniser
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(playSound:)];
singleTap.numberOfTapsRequired = 1;
[testLabel addGestureRecognizer:singleTap];
And this is the method that the tap gesture recogniser should call
- (void)playSound:(UITapGestureRecognizer *)sender {
NSLog(#"play sound");
if(sender.state == UIGestureRecognizerStateEnded)
{
int pronounNumber = [sender.view tag];
int exampleNumber = (int)sender.view.frame.origin.x%(int)self.view.frame.size.width;
NSLog(#"Pronoun is %i and example is %i", pronounNumber, exampleNumber);
}
}
This method is never called when I tried to touch on the UILabel.
I have tried setting the property canCancelContentTouches to both YES and NO on the scroll view as suggested by this thread, but it's still not working.
The strange thing is, if I add a UILabel outside of the scrollView, then the gesture recogniser works! So the problem only occurs in my contentView which is a subview of my scrollView.
I am using auto-layout, if that might be any difference?
Thanks!

The scroll view also has a gesture recogniser. By default, only 1 gesture recognizer can be handling touches at any one time. You need to make yourself the delegate of your gesture and then implement gestureRecognizer:shouldRecognizeSimultaneouslyWithGestureRecognizer: to return YES. This will allow it to work at the same time as the scroll view.

Add delegate to tagGestures,
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(playSound:)];
singleTap.numberOfTapsRequired = 1;
singleTap.delegate = self;
[testLabel addGestureRecognizer:singleTap];
EDIT:-
contentView.userInteractionEnabled = YES;
put this line your code it'l work.

[yourlabel.addGestureRecognizer:tapGestureDeFromage];
should add the gesture explicitly to your labels.

Related

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];

how to enable to move cursor in UITextView

I create UItextView in pragmatically, and when I tap to move cursor in UITextView, it does not worked. But it worked using IB. Do I supposed to set some attributes to UItextView or UIView?
UITextView *textView = [[UITextView alloc] initWithFrame:CGRectMake(0, 0,320, 460)];
textView.userInteractionEnabled = YES;
textView.editable = YES;
textView.multipleTouchEnabled =YES;
[self.view addSubview:textView];
textView.delegate = self;
I found default long tap does not working.
I have problem with add subView for gesture recognizer inside custom cell.
When I blocked it or move to background it've started to work:
contentView.insertSubview(view, atIndex: 0)
where view is view with gesture recognizer.

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.

UINavigationItem touchView touch size

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:)];

UITableView does not scroll if I add a Tap Gesture Recogniser to the TableView

I want to have a tap gesture recogniser with the UITableView. But if I add a gesture recogniser, scrolling function of UITableView doesn't work. How can I solve this?
self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, 320, 460) style:UITableViewStylePlain];
self.tableView.backgroundColor = [UIColor darkGrayColor];
self.tableView.dataSource = self;
self.tableView.delegate = self;
[self.view addSubview:self.tableView];
UITapGestureRecognizer *tap=[[UITapGestureRecognizer alloc]initWithTarget:self action:#selector(tap)];
[self.tableView addGestureRecognizer:tap];
-(void)tap
{
NSLog(#"You are Tap on Table.....");
}
It's working try it.

Resources