如何获取 UIView 中手指点击的坐标?
如何获取 UIView 中手指点击的坐标? (我不想使用一大堆按钮)
谢谢
How do I get the coordinates for finger tapping in UIView?
(I would prefer not to use a big array of buttons)
Thank you
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
有两种方法可以实现此目的。如果您已经拥有正在使用的 UIView 子类,则只需重写该子类上的
-touchesEnded:withEvent:
方法,如下所示:如果您尚未对 UIView 进行子类化,不过,并且视图由视图控制器或其他任何东西拥有,那么您可以使用 UITapGestureRecognizer,如下所示:
There’re two ways to accomplish this. If you’ve already got a subclass of UIView that you’re using, you can just override the
-touchesEnded:withEvent:
method on that subclass, like this:If you’re not already subclassing UIView, though, and the view is owned by a view controller or whatever, then you can use a UITapGestureRecognizer, like this:
斯威夫特 3 答案
Swift 3 answer
我假设你的意思是识别手势(和触摸)。开始寻找如此广泛的问题的最佳位置是Apple的示例代码 触摸。它遍历了大量的信息。
I assume you mean recognizing gestures (and touches). The best place to start looking for such a broad question is Apple's sample code Touches. It walks through a good amount of information.
你需要做这样的事情。
touchesBegan:withEvent:
是UIResponder
的一种方法,UIView
和UIViewController
均派生自该方法。如果你用谷歌搜索这个方法,你会发现几个教程。 Apple 的 MoveMe 示例是一个很好的示例。You need to do something like this.
touchesBegan:withEvent:
is a method ofUIResponder
from whichUIView
andUIViewController
both are derived. If you google for this method then you will find several tutorials. MoveMe sample from Apple is a good one.Swift 5.6:
您可以在 UIResponder(UIView 和 UIViewController 继承自)中重写以下内容:
或者在手势识别器处理程序中:
Swift 5.6:
You can override the following in your UIResponder (which both UIView and UIViewController inherit from):
Or in your gesture recognizer handler: