【问题标题】:UITapGestureRecognizer on UILabels in subview of UIScrollView not workingUIScrollView子视图中UILabels上的UITapGestureRecognizer不起作用
【发布时间】:2013-07-17 06:25:39
【问题描述】:

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

视图层次结构如下:

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

我已将代码提炼为一个示例以突出问题

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

这是点击手势识别器应该调用的方法

- (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 时,从未调用此方法。

我已尝试按照thread 的建议在滚动视图上将属性 canCancelContentTouches 设置为 YES 和 NO,但仍然无法正常工作。

奇怪的是,如果我在 scrollView 之外添加一个 UILabel,那么手势识别器就会起作用!所以问题只发生在我的 contentView 中,它是我的 scrollView 的子视图。

我正在使用自动布局,这可能有什么不同吗?

谢谢!

【问题讨论】:

  • 尝试设置 [contentView setClipBounds:YES];并查看ContentView 中的Label 是否可见,如果不可见,则您的Label 不在触摸区域,请调整调整大小属性..

标签: ios objective-c uiscrollview uilabel uigesturerecognizer


【解决方案1】:

将委托添加到 tagGestures,

UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(playSound:)];
singleTap.numberOfTapsRequired = 1;
singleTap.delegate = self;
[testLabel addGestureRecognizer:singleTap];

编辑:-

contentView.userInteractionEnabled = YES;

把这行代码放到你的代码里就行了。

【讨论】:

  • 嗨,我刚刚尝试将委托设置为 self,并实现了 <UIGestureRecognizerDelegate> 协议和 - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch 方法。仍然不起作用,当我不需要它为滚动视图之外的 UILabels 时,为什么我需要为它实现一个委托?
  • @Joseph Williamson:您是否在我的答案中添加了最后一行代码?添加并检查。
  • 检查一下你的 scrollView 视图框架。
  • 抱歉,检查我的滚动视图框架是什么意思?
  • @Joseph Williamson:一旦给出静态值并检查,问题在于 contentView 框架或滚动视图框架。
【解决方案2】:

滚动视图也有一个手势识别器。默认情况下,任何时候只有 1 个手势识别器可以处理触摸。您需要让自己成为手势的代表,然后实现gestureRecognizer:shouldRecognizeSimultaneouslyWithGestureRecognizer: 以返回YES。这将允许它与滚动视图同时工作。

【讨论】:

  • 好的,所以我在我的.h文件中设置了singleTap.delegate = self;并实现了UIGestureRecognizerDelegate,并添加了gestureRecognizer:shouldRecognizeSimultaneouslyWithGestureRecognizer:,但还是不行。
  • 是否调用了委托方法?内容视图的大小是多少(记录它)?
  • 天啊!你说对了!它是contentView size <UIView: 0xc0345b0; frame = (0 0; 640 0),所以它的高度为零!!非常感谢秀秀动物园!!!
  • 奇怪的是,@wain,如果您在滚动视图内向堆栈视图的“单元格”添加一个水龙头(UITapGestureRecognizer),它似乎确实有效(无论如何)。不使用 shouldRecognizeSimultaneously。我不知道为什么。
  • 对于任何阅读本文的人来说,我遇到了一个令人着迷的晦涩问题。我正在动态生成 UIViewController 及其视图,并将它们插入到滚动视图中。当然,视图控制器 - 本身 - 在您创建它们时并没有特别保留!视图仍然存在,但 VC 已消失,除非您保留它。
【解决方案3】:
[yourlabel.addGestureRecognizer:tapGestureDeFromage];

应该将手势明确添加到您的标签中。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-09-25
    • 1970-01-01
    • 2019-09-17
    • 1970-01-01
    • 1970-01-01
    • 2023-04-06
    • 2019-08-02
    • 1970-01-01
    相关资源
    最近更新 更多