单个视图的多个手势响应器
我有一个图像,我想设置它来响应几个不同的手势响应器。例如,如果触摸图片的一个部分,我希望调用一个选择器,并调用另一个选择器来调用图片的不同部分。
我查看了 UIGestureRecognizer
和 UITapGestureRecognizer
类,但找不到指定与它们关联的图像区域的方法。这在 iOS 中可能吗?如果是这样,我应该考虑使用哪些类?
I have an image that I would like to set up to respond to several different gesture responders. So for example, if one part of the picture is touched I would like one selector to be called, and another selector for a different part of the picture.
I looked at the UIGestureRecognizer
and UITapGestureRecognizer
classes, but I couldn't find a way to specify the image zones to be associated with them. Is this at all possible in iOS? And if so what classes should I look into using?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
最简单的解决方案是在图像上放置不可见的视图,并在其上放置手势识别器。
如果这不可行,您必须查看手势识别器的点击处理程序中的 locationInView ,并根据用户点击的位置找出您想要执行的操作。
The easiest solution is to lay invisible views over the image and put the gesture recognizers on them.
If that's not feasible you'll have to look at the locationInView in the gesture recognizer's tap handler and figure out what you want to do based on where the user tapped.
使用
locationInView:
属性确定点击发生的位置,然后有条件地调用方法。您可以通过设置一些与您的点击区域相对应的 CGRect 来做到这一点。然后使用 CGRectContainsPoint() 函数确定点击是否落在某一点击区域。您的点击手势识别器操作可能如下所示:
Use the
locationInView:
property to determine where your tap occurred and then conditionally invoke a method. You can do this by setting up someCGRect
s that correspond to your hit areas. Then use theCGRectContainsPoint()
function to determine if the tap landed in one of the hit areas.Your tap gesture recognizer action may look something like this: