首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如果按下子视图的按钮,如何取消UIGestureRecognizer

如果按下子视图的按钮,如何取消UIGestureRecognizer
EN

Stack Overflow用户
提问于 2011-05-03 15:39:18
回答 3查看 12.8K关注 0票数 19

我正在努力从手势识别器中获得我想要的行为,特别是在其他手势被解雇的情况下取消某些手势。

我有一个设置为分页的scrollView和每个页面中的多个子视图。我已经添加了一个触摸手势识别器,如果用户点击页面的右侧或左侧,可以滚动到下一页或上一页。

代码语言:javascript
复制
    // Add a gesture recogniser turn pages on a single tap at the edge of a page
    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGestureHandler:)];
    tapGesture.cancelsTouchesInView = NO;
    [self addGestureRecognizer:tapGesture];
    [tapGesture release];

和我的手势处理器:

代码语言:javascript
复制
- (void) tapGestureHandler:(UIGestureRecognizer *) gestureRecognizer {
    const CGFloat kTapMargin = 180;

    // Get the position of the point tapped in the window co-ordinate system
    CGPoint tapPoint = [gestureRecognizer locationInView:nil];

    // If the tap point is to the left of the page then go back a page
    if (tapPoint.x > (self.frame.size.width - kTapMargin)) [self scrollRectToVisible:pageViewRightFrame animated:YES];

    // If the tap point is to the right of the page then go forward a page
    else if (tapPoint.x < kTapMargin) [self scrollRectToVisible:pageViewLeftFrame animated:YES];
}

所有的工作都很好,除了我在页面上有一个子视图里面有按钮的地方。如果用户触摸subView上的按钮,而我不知道该如何操作,我希望能够忽略轻触翻页的操作。

干杯

戴夫

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2011-05-09 18:28:36

最终对我来说最有效的解决方案是使用hitTest来确定在点击手势的位置下是否有任何按钮。如果有,那就忽略手势代码的其余部分。

看起来效果很好。我想知道我的所作所为是否有什么问题。

代码语言:javascript
复制
- (void) tapGestureHandler:(UIGestureRecognizer *) gestureRecognizer {
    const CGFloat kTapMargin = 180;

    // Get the position of the point tapped in the window co-ordinate system
    CGPoint tapPoint = [gestureRecognizer locationInView:nil];

    // If there are no buttons beneath this tap then move to the next page if near the page edge
    UIView *viewAtBottomOfHeirachy = [self.window hitTest:tapPoint withEvent:nil];
    if (![viewAtBottomOfHeirachy isKindOfClass:[UIButton class]]) {

        // If the tap point is to the left of the page then go back a page
        if (tapPoint.x > (self.bounds.size.width - kTapMargin)) [self scrollRectToVisible:pageViewRightFrame animated:YES];

        // If the tap point is to the right of the page then go forward a page
        else if (tapPoint.x < kTapMargin) [self scrollRectToVisible:pageViewLeftFrame animated:YES];
    }   
}
票数 20
EN

Stack Overflow用户

发布于 2013-08-02 00:15:45

Apple documentation显示了答案:

代码语言:javascript
复制
- (void)viewDidLoad {
      [super viewDidLoad];
      // Add the delegate to the tap gesture recognizer
      self.tapGestureRecognizer.delegate = self;
}

// Implement the UIGestureRecognizerDelegate method
-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:    (UITouch *)touch {
      // Determine if the touch is inside the custom subview
     if ([touch view] == self.customSubview){
         // If it is, prevent all of the delegate's gesture recognizers
         // from receiving the touch
        return NO;
     }
     return YES;
}

当然,在本例中,customSubview应该是包含按钮(甚至是按钮)的页面上的子视图。

票数 15
EN

Stack Overflow用户

发布于 2011-05-04 01:08:37

您可以子类化UIView并覆盖-touchesBegan选择器,或者您可以使用子视图的opaque属性使它们对触摸“不可见”(如果view.opaque = NO,视图将忽略触摸事件)。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/5866520

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档