将点击手势限制为滚动视图,而不是放置在其上的按钮
我有一个滚动视图,上面放置了一个按钮,当我添加点击手势识别器时,按钮不起作用。有没有办法限制点击仅滚动视图而不是按钮,以便按钮正常工作。
这是我的代码
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果你这样做,
它将允许按下按钮。但是,当您按下按钮时,将检测到轻击以及按下按钮。为了避免这种情况,您必须子类化
UIScrollView
并实现以下方法 -实现上述方法将触摸传递给滚动视图的子视图。
If you do
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 –Implementing the method above pass the touches to the scroll view's subviews.
不需要子类化 Scrollview。以下代码可以轻松解决问题。 gestureRecognizer:shouldReceiveTouch: 方法可以解决这个问题。
There is no need to subclass Scrollview. Following code solves the problem easy way. The method gestureRecognizer:shouldReceiveTouch: does the trick.