UIScrollView子视图中UILabels上的UITapGestureRecognizer不

UITapGestureRecognizer on UILabels in subview of UIScrollView not working(UIScrollView子视图中UILabels上的UITapGestureRecognizer不起作用)
本文介绍了UIScrollView子视图中UILabels上的UITapGestureRecognizer不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 UIScrollView 的内容视图中的 UITapGestureRecognizer 上的 UITapGestureRecognizer 没有调用它的方法.

I have a problem where my UITapGestureRecognizer on my UILabels in a content view in my UIScrollView is not calling it's methods.

视图层次结构如下:

  • 滚动视图(UIScrollView)
    • 内容视图(UIView)
      • testLabel (UILabel) - 这里是 UITapGestureRecognizer 的附加位置

      我已将代码提炼成一个示例来突出问题

      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 return YES. This will allow it to work at the same time as the scroll view.

      这篇关于UIScrollView子视图中UILabels上的UITapGestureRecognizer不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Why local notification is not firing for UNCalendarNotificationTrigger(为什么没有为UNCalendarNotificationTrigger触发本地通知)
iOS VoiceOver functionality changes with Bundle Identifier(IOS画外音功能随捆绑包标识符而变化)
tabbar middle tab out of tabbar corner(选项卡栏中间的选项卡角外)
Pushing UIViewController above UITabBar(将UIView控制器推送到UITabBar上方)
Dropbox Files.download does not start when number of files in folder is gt; 1000(当文件夹中的文件数为1000时,Dropbox Files.Download不会启动)
How to target newer versions in .gitlab-ci.yml using auto devops (java 11 instead of 8 and Android 31 instead of 29)(如何在.gitlab-ci.yml中使用自动开发工具(Java 11而不是8,Android 31而不是29)瞄准较新的版本)