将点击手势限制为滚动视图,而不是放置在其上的按钮

发布于 2024-11-17 03:08:29 字数 425 浏览 5 评论 0原文

我有一个滚动视图,上面放置了一个按钮,当我添加点击手势识别器时,按钮不起作用。有没有办法限制点击仅滚动视图而不是按钮,以便按钮正常工作。

这是我的代码

UITapGestureRecognizer* tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGesture:)];
tap.numberOfTapsRequired = 1;
tap.numberOfTouchesRequired = 1;
[scroll addGestureRecognizer:tap];
[tap release];


- (void)tapGesture:(UIGestureRecognizer*)gesture{


NSLog(@"scroll tapped");

}

I have a scroll view and a button placed over it , When i add a tap gesture recognizer the button does not work. Is there any way of limiting the tap only to scroll view and not to the button, so that the button functions normally.

here is my code

UITapGestureRecognizer* tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGesture:)];
tap.numberOfTapsRequired = 1;
tap.numberOfTouchesRequired = 1;
[scroll addGestureRecognizer:tap];
[tap release];


- (void)tapGesture:(UIGestureRecognizer*)gesture{


NSLog(@"scroll tapped");

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

拍不死你 2024-11-24 03:08:29

如果你这样做,

tap.cancelsTouchesInView = NO;

它将允许按下按钮。但是,当您按下按钮时,将检测到轻击以及按下按钮。为了避免这种情况,您必须子类化 UIScrollView 并实现以下方法 -

- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
    for ( UIView * subview in self.subviews ) {
        UIView * hitView = [subview hitTest:point withEvent:event];
        if ( hitView )
            return hitView;
    }

    return [super hitTest:point withEvent:event];
}

实现上述方法将触摸传递给滚动视图的子视图。

If you do

tap.cancelsTouchesInView = NO;

It will allow button to be pressed. However taps will be detected along with the button press when you press a button. Do avoid this, you will have to subclass UIScrollView and implement the following method –

- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
    for ( UIView * subview in self.subviews ) {
        UIView * hitView = [subview hitTest:point withEvent:event];
        if ( hitView )
            return hitView;
    }

    return [super hitTest:point withEvent:event];
}

Implementing the method above pass the touches to the scroll view's subviews.

清音悠歌 2024-11-24 03:08:29

不需要子类化 Scrollview。以下代码可以轻松解决问题。 gestureRecognizer:shouldReceiveTouch: 方法可以解决这个问题。

UITapGestureRecognizer* tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGesture:)];
tap.numberOfTapsRequired = 1;
tap.numberOfTouchesRequired = 1;
tap.delegate = self;
tap.cancelsTouchesInView = NO;
[scroll addGestureRecognizer:tap];
[tap release];

-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
    if (scroll.superview != nil) {
        if ([touch.view isKindOfClass:[UIButton class]])
        {
            return NO; // ignore the touch
        }
    }
    return YES; // handle the touch
}

- (void)tapGesture:(UIGestureRecognizer*)gesture {
    NSLog(@"scroll tapped");
}

There is no need to subclass Scrollview. Following code solves the problem easy way. The method gestureRecognizer:shouldReceiveTouch: does the trick.

UITapGestureRecognizer* tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGesture:)];
tap.numberOfTapsRequired = 1;
tap.numberOfTouchesRequired = 1;
tap.delegate = self;
tap.cancelsTouchesInView = NO;
[scroll addGestureRecognizer:tap];
[tap release];

-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
    if (scroll.superview != nil) {
        if ([touch.view isKindOfClass:[UIButton class]])
        {
            return NO; // ignore the touch
        }
    }
    return YES; // handle the touch
}

- (void)tapGesture:(UIGestureRecognizer*)gesture {
    NSLog(@"scroll tapped");
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文