Iphone SDK 中的同步手势识别器
我需要使用 UISwipeGestureRecognizer 捕获两个不同的滑动手势(例如,UISwipeGestureRecognizerDirectionRight 和 UISwipeGestureRecognizerDirectionLeft)。当我使用 addGestureRecognizer 方法添加两个不同的识别器时,只有最后添加的识别器起作用。我读到我需要实现 UIGestureRecognizerDelegate
协议的 gestureRecognizer:shouldRecognizeSimultaneouslyWithGestureRecognizer: 方法,但没有任何效果。
任何人都可以帮忙提供捕捉两个或多个相同手势的简单示例吗? 谢谢!
I need to catch two different swipping gestures using UISwipeGestureRecognizer
(for example, UISwipeGestureRecognizerDirectionRight
and UISwipeGestureRecognizerDirectionLeft
). When I add two different recognisers with addGestureRecognizer method, only last added recognizer works. I've read that I need to implement gestureRecognizer:shouldRecognizeSimultaneouslyWithGestureRecognizer: method of UIGestureRecognizerDelegate
protocol, but nothing works.
Can anyone help with simple example of catching two or more same gestures?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
这非常简单:
首先我们应该创建实现
UIGestureRecognizerDelegate
协议的类:并像这样使用它:
It was really easy:
At first we should create class, that implements
UIGestureRecognizerDelegate
protocol:And use it like this:
答案:
“嗯,快速浏览一下文档...”来自 Phoenix 绝对行不通!
他正在设置一个掩码,然后使用相同的变量进行测试,就像识别器清除它并设置一个位一样。正如他从文档中正确引用的那样,它存储:
只会返回您最初设置的掩码,在他的示例中,永远不会解析为单个方向!
另外,在大多数情况下,您不需要:
以下对我有用:
The answer:
"Um, a quick look at the documentation..." from Phoenix absolutely will not work!
He is setting a mask, then using the same variable to test as if the recognizer cleared it and set a single bit. It stores, as he correctly quoted from the documentation:
will simply return the mask you set initially and in his example, will never resolve to a single direction!
Additionaly, for most cases you don't need to:
The following works for me:
嗯,快速浏览一下文档就会发现您所做的工作比您需要的要多:
“方向
允许的滑动方向。
讨论
您可以通过使用按位或操作数指定多个
UISwipeGestureRecognizerDirection
常量来指定多个方向。默认方向是UISwipeGestureRecognizerDirectionRight
。”也就是说,您可以执行以下操作,而不是使用两个 UISwipeGestureRecognizer:
并且在您的操作方法中:
更简单,并且更不容易发生冲突。
Um, a quick look at the documentation would reveal that you are doing way more work than you need to:
"direction
The permitted directions of the swipe.
Discussion
You may specify multiple directions by specifying multiple
UISwipeGestureRecognizerDirection
constants using bitwise-OR operands. The default direction isUISwipeGestureRecognizerDirectionRight
."Which is to say, instead of using two UISwipeGestureRecognizers, you can just do the following:
And in your action method:
Much simpler, and much less prone to conflict.
我建议您阅读一些有关手势识别器用于识别手势的技术的内容。我想,第一个识别器尝试识别该手势,但意识到他不必对其做出响应,然后不知何故他没有传递它。
阅读它们的工作原理以了解如何使用它们非常有用。
希望这有帮助。
I would suggest you read a little bit on the technique that the gesture recognizers use to recognize the gesture. I suppose, the first recognizer tries to recognize the gesture, but realizes that he does not has to respond to it and then somehow he does not pass it on.
It's very useful to read how they work in order to understand how to use them.
Hope this helps.
感谢 user294890 的建议,我成功地从列出的 UIGestureRecognizerDelegate 方法中返回 yes 。
然而,我发现在加载 Web 视图之前添加 GestureRecognizer 时它不起作用。然而,当我在 webView 委托方法中将 GestureRecognizer 添加到 webView 时,它确实起作用了:
- (void)webViewDidFinishLoad:(UIWebView *)webView
。Thanks to user294890's suggestion, I was successful by returning yes from the listed
UIGestureRecognizerDelegate
methods.I found however that it would not work when I was adding the GestureRecognizer before the web view had loaded. It did however work when I added the GestureRecognizer to the webView in the webView delegate method:
- (void)webViewDidFinishLoad:(UIWebView *)webView
.