UIScrollView子视图中的UILabel上的UITapGestureRecognizer无法正常工作 [英] UITapGestureRecognizer on UILabels in subview of UIScrollView not working
问题描述
我有一个问题,我的UIScrollView的内容视图中的UILabel上的UITapGestureRecognizer没有调用它的方法.
I have a problem where my UITapGestureRecognizer on my UILabels in a content view in my UIScrollView is not calling it's methods.
视图层次结构如下:
- scrollView(UIScrollView)
- contentView(UIView)
- testLabel(UILabel)-这是连接UITapGestureRecognizer的地方
- 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); } }
当我尝试触摸UILabel时,永远不会调用此方法.
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.
奇怪的是,如果我在scrollView之外添加UILabel,则手势识别器将起作用!因此,问题仅出现在我的contentView(它是我的scrollView的子视图)中.
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?
谢谢!
推荐答案
滚动视图还具有手势识别器.默认情况下,任何时候只能有1个手势识别器可以处理触摸.您需要使自己成为手势的委托,然后实现
gestureRecognizer:shouldRecognizeSimultaneouslyWithGestureRecognizer:
以返回YES
.这将使其能够与滚动视图同时工作.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 returnYES
. This will allow it to work at the same time as the scroll view.这篇关于UIScrollView子视图中的UILabel上的UITapGestureRecognizer无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
- contentView (UIView)
- contentView(UIView)