用于UIScrollView的iOS5 UITapRecognizer干扰按钮。怎么修?

时间:2011-11-23 23:26:17

标签: ios objective-c uiscrollview uibutton uigesturerecognizer

我在UIButtonUIView内有一堆UIScrollViewUIScrollView。我正在尝试向滚动视图添加点按识别器。点击识别器会触发,但现在我的按钮都没有工作。

我知道在iOS5中,{{1}}可以在完成之后以某种方式将触摸事件传递给控件层次结构。任何人都可以帮我弄清楚如何做到这一点?

3 个答案:

答案 0 :(得分:15)

UIGestureRecognizer属性cancelsTouchesInView设置为NO。

UITapGestureRecognizer *singleTapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self 
                                                                                             action:@selector(singleTap:)];
singleTapGestureRecognizer.numberOfTapsRequired = 1;
singleTapGestureRecognizer.enabled = YES;
singleTapGestureRecognizer.cancelsTouchesInView = NO;
[tapableView addGestureRecognizer:singleTapGestureRecognizer];
[singleTapGestureRecognizer release];

来自UIGestureRecognizer Class Reference

  

影响触摸是否为的布尔值   在识别出手势时传送到视图。

     

当此属性为YES(默认值)和   接收器识别其手势,即手势的触摸   待处理未传递到视图和之前已交付的触摸   通过touchesCancelled:withEvent:消息被取消   风景。如果手势识别器无法识别其手势或是否   此属性的值为NO,视图接收所有触摸   多点触控序列。

答案 1 :(得分:2)

对我来说,上述答案的组合工作

UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(userDidTap:)];
tapRecognizer.cancelsTouchesInView = YES;
tapRecognizer.delegate = self;
[tapRecognizer requireGestureRecognizersToFail:self.scrollView.gestureRecognizers];
[self.view addGestureRecognizer:tapRecognizer];

-

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
    if ([touch.view.superview isKindOfClass:[UIButton class]] || [touch.view isKindOfClass:[UIButton class]])
    {
        return NO;
    }
    return YES;
}

答案 2 :(得分:0)

您还可以使用gestureRecognizer:shouldReceiveTouch:的{​​{1}}方法(记录为here)来完成相同的操作。它提供了更多的灵活性,例如,如果您想要取消某些触摸,而不是其他触摸。这是一个例子,

UIGestureRecognizerDelegate