如果按下子视图的按钮,如何取消 UIGestureRecognizer [英] How to cancel UIGestureRecognizer if subview's button pressed

查看:14
本文介绍了如果按下子视图的按钮,如何取消 UIGestureRecognizer的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在努力从手势识别器中获得我想要的行为,特别是如果其他手势被触发则取消某些手势.

I am struggling to get the behaviour I would like from the gesture recognisers, specifically cancelling certain gestures if others have fired.

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

I have a scrollView set to paging and multiple subviews in each page. I have added a touch gesture recogniser to scroll to the next or prev page if the user taps to the right or left of the page.

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

还有我的手势处理程序:

and my gesture handler:

- (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 上的按钮,我希望能够忽略点击翻页,但我不知道该怎么做.

All works well, except where I have a subview on the page that has buttons in it. I want to be able to ignore the tap to turn the page if the user touches a button on the subView and I can't figure out how to do this.

干杯

戴夫

推荐答案

最终对我来说效果最好的解决方案是使用 hitTest 来确定点击手势位置下方是否有任何按钮.如果有则忽略其余的手势代码.

The solution that worked the best for me in the end was to use the hitTest to determine if there were any buttons underneath the location of the tap gesture. If there are then just ignore the rest of the gesture code.

似乎运作良好.想知道我所做的是否有任何问题.

Seems to work well. Would like to know if there are any gotchas with what I have done.

- (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];
    }   
}

这篇关于如果按下子视图的按钮,如何取消 UIGestureRecognizer的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆