ios ScrollView 手势识别器吃掉所有触摸事件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16882737/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
ScrollView gesture recognizer eating all touch events
提问by Itai Hanski
I have a UIScrollView
to which I added a single tap gesture recognizer to show/hide some UI overlay using:
我有一个UIScrollView
我添加了一个单击手势识别器来显示/隐藏一些 UI 覆盖的使用:
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTap:)];
[scrollView addGestureRecognizer:singleTap];
and:
和:
- (void)handleTap:(UITapGestureRecognizer *)sender {
// report click to UI changer
}
I added an easy table viewto the bottom of the UIScrollView
. Everything works right (scrolling both horizontally and vertically) but the problem is that taps are recognized only by the gesture recognizer (above), but not by the easy table view.
If I remove The line that registers the gesture listener, everything works fine, the table view notices taps on itself.
我在底部添加了一个简单的表格视图UIScrollView
。一切正常(水平和垂直滚动),但问题是点击只能被手势识别器(上图)识别,而不是被简单的表格视图识别。如果我删除注册手势侦听器的行,一切正常,表视图会注意到点击了自身。
It's as if the gesture recognizer function "eats" the tap events on the table view and doesn't propagate them downward.
就好像手势识别器功能“吃掉”了表格视图上的点击事件并且不会向下传播它们。
Any help is appreciated
任何帮助表示赞赏
回答by zambrey
This should solve your problem.
Detect touch event on UIScrollView AND on UIView's components [which is placed inside UIScrollView]
The idea is to tell the gesture recognizer to not swallow up the touch events. To do this you need to set singleTap's cancelsTouchesInView
property to NO
, which is YES
by default.
这应该可以解决您的问题。
在 UIScrollView 和 UIView 的组件上检测触摸事件[放置在 UIScrollView 内]
想法是告诉手势识别器不要吞下触摸事件。为此,您需要将 singleTap 的cancelsTouchesInView
属性设置为NO
,这是YES
默认设置。
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTap:)];
singleTap.cancelsTouchesInView = NO;
[scrollView addGestureRecognizer:singleTap];
回答by Jaydeep
Swift 3.0
斯威夫特 3.0
let singleTap = UITapGestureRecognizer(target: self, action: #selector(handleTap(_:)))
singleTap.cancelsTouchesInView = false
singleTap.numberOfTapsRequired = 1
scrollView.addGestureRecognizer(singleTap)
And the selector method be like.
和选择器方法一样。
@objc func handleTap(_ recognizer: UITapGestureRecognizer) {
// Perform operation
}
回答by RandyTek
I think the reason is that User Interaction Enabled
is set to false
for UIImageView. You should set it to true to enable tapping in it
我认为原因是User Interaction Enabled
设置false
为 UIImageView。您应该将其设置为 true 以启用点击
回答by sangony
You can set which objects are to be included/excluded for touches.
您可以设置要包含/排除触摸的对象。
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gesture shouldReceiveTouch:(UITouch *)touch {
if (touch.view == [self view]) {
return YES;
}
return NO;
}
回答by hamishkeith
This worked for me in Swift 3 / Xcode 8
这在Swift 3 / Xcode 8 中对我有用
self.scrollView.touchesShouldCancel(in: ** the view you want the touches in **)
self.scrollView.canCancelContentTouches = false
Good luck!
祝你好运!
回答by Nuno Vieira
Thanks @zambrey
谢谢@zambrey
Swift 2.2+ Version:
斯威夫特 2.2+ 版本:
scrollView.delegate = self
let allowMultipleTouches = UITapGestureRecognizer(target: self, action: #selector(genderPressed))
allowMultipleTouches.numberOfTapsRequired = 1
allowMultipleTouches.cancelsTouchesInView = false
scrollView.addGestureRecognizer(allowMultipleTouches)
If your scroll view is in the Storyboard, don't forget to pin the Outlet in the view controller. In this example, scrollView
is the Outlet of the UIScrollView
.
如果您的滚动视图在 Storyboard 中,请不要忘记将 Outlet 固定在视图控制器中。在本例中,scrollView
是 的 Outlet UIScrollView
。
回答by Maria
TapGestures worked for me. The swipe on the other hand, I had to disable the scrolling and it worked.
TapGestures 为我工作。另一方面,滑动,我不得不禁用滚动并且它起作用了。
swipeLeftGesture = UISwipeGestureRecognizer(target: self, action: #selector(swipeToNewImage(_:)))
swipeLeftGesture.direction = .left
scrollView.addGestureRecognizer(swipeLeftGesture)
swipeRightGesture = UISwipeGestureRecognizer(target: self, action: #selector(swipeToNewImage(_:)))
scrollView.addGestureRecognizer(swipeRightGesture)
scrollView.isScrollEnabled = false
回答by Aks
You can capture any kind of gestures in the UIscrollView. Make sure you also handle some of the default properties as well like set cancelsTouchesInView property to false, it is true by default. Also give some tag nos to your sub views to distinguish in selectors. & also enable their User interaction to true.
您可以在 UIscrollView 中捕捉任何类型的手势。确保您还处理了一些默认属性以及将 cancelsTouchesInView 属性设置为 false,默认情况下为 true。还为您的子视图提供一些标签编号以区分选择器。& 还使他们的用户交互为真。
let tap = UITapGestureRecognizer(target: self, action:
selector(didTapByUser(_:)))
让 tap = UITapGestureRecognizer(target: self, action:
选择器(didTapByUser(_:)))