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

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

问题描述



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

  //添加一个手势识别器在页面边缘单击一下翻页。
UITapGestureRecognizer * tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGestureHandler :)];
tapGesture.cancelsTouchesInView = NO;
[self addGestureRecognizer:tapGesture];
[tapGesture release];

和我的手势处理程序:

   - (void)tapGestureHandler:(UIGestureRecognizer *)gestureRecognizer {
const CGFloat kTapMargin = 180;

//获取在窗口坐标系中点击的点的位置
CGPoint tapPoint = [gestureRecognizer locationInView:nil];

//如果点击点在页面的左边,然后回到页面
if(tapPoint.x>(self.frame.size.width - kTapMargin))[ self scrollRectToVisible:pageViewRightFrame animated:YES];

//如果点击点在页面的右边,则前进一页
else if(tapPoint.x< kTapMargin)[self scrollRectToVisible:pageViewLeftFrame animated:YES];
}

一切都很好,除非在页面上有一个子视图,在里面。如果用户触摸subView上的按钮,我想要忽略点按即可翻页。我不知道如何做。



干杯



Dave

解决方案

最后是使用hitTest来确定在点击手势的位置下是否有任何按钮。如果只有忽略其​​余的手势代码。



看起来很好。

   - (void)tapGestureHandler:(UIGestureRecognizer *)gestureRecognizer {
const CGFloat kTapMargin = 180;

//获取在窗口坐标系中点击的点的位置
CGPoint tapPoint = [gestureRecognizer locationInView:nil];

//如果在这个点击下没有按钮,然后移动到下一页如果靠近页面边缘
UIView * viewAtBottomOfHeirachy = [self.window hitTest:tapPoint withEvent:nil];
if(![viewAtBottomOfHeirachy isKindOfClass:[UIButton class]]){

//如果点击点在页面的左边,然后回到页面
if tapPoint.x>(self.bounds.size.width - kTapMargin))[self scrollRectToVisible:pageViewRightFrame animated:YES];

//如果点击点在页面的右边,则前进一页
else if(tapPoint.x< kTapMargin)[self scrollRectToVisible:pageViewLeftFrame animated:YES];
}
}


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

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

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.

Cheers

Dave

解决方案

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天全站免登陆