获取 UIGestureRecognizer 的 UITouch 对象
有没有办法获取与手势关联的 UITouch
对象? UIGestureRecognizer
似乎没有任何方法来实现这一点。
Is there a way to get the UITouch
objects associated with a gesture? UIGestureRecognizer
doesn't seem to have any methods for this.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
杰伊是对的......你会想要一个子类。试试这个尺寸,它来自我的一个项目。在 DragGestureRecognizer.h 中:
在 DragGestureRecognizer.m 中:
当然,您需要
在委托中实现该方法 - 例如:
Jay's right... you'll want a subclass. Try this one for size, it's from one of my projects. In DragGestureRecognizer.h:
And in DragGestureRecognizer.m:
Of course, you'll need to implement the
method in your delegate -- for example:
如果它只是您感兴趣的位置,则不必子类化,您将收到 UIGestureRecognizer 中点击位置更改的通知。
使用目标进行初始化:
处理 UIGestureRecognizerStateChanged 以获取位置更改:
If it is just the location you are interested in, you do not have to subclass, you will be notified of location changes of the tap from the UIGestureRecognizer.
Initialize with target:
Handle UIGestureRecognizerStateChanged to get location changes:
如果您只需要找出手势的位置,则可以在 UIGestureRecognizer 对象上调用 locationInView: 或 locationOfTouch:inView: 。但是,如果您想做其他任何事情,那么您需要子类化。
If you only need to find out the location of the gesture, you can call either locationInView: or locationOfTouch:inView: on the UIGestureRecognizer object. However if you want to do anything else, then you'll need to subclass.
如果您正在编写自己的 UIGestureRecognizer,则可以覆盖触摸对象:
或等效的移动、结束或取消
Apple 文档 有很多关于子类化的信息
If you're writing your own UIGestureRecognizer you can get the touch objects overriding:
or the equivalent moved, ended or canceled
The Apple docs has lots of info on subclassing
这是一种将长按添加到任意 UIView 的方法。
这使您可以在任意数量的 UIView 上运行长按。
在此示例中,我通过标签限制对 UIView 方法的访问,但您也可以使用 isKindOfClass。
(据我发现,您必须对 LongPress 使用 TouchMoved,因为在 LongPress 激活之前,touchesBegan 会触发)
subclass - .h
subclass - .m
viewcontroller - .h
viewcontroller - .m
Here is a method to get a long press added to an arbitrary UIView.
This lets you run longpress on any number of UIViews.
In this example I restrict access to the UIView method through tags but you could use isKindOfClass as well.
(From what I found you have to use touchesMoved for LongPress because touchesBegan fires before the LongPress becomes active)
subclass - .h
subclass - .m
viewcontroller - .h
viewcontroller - .m
简单快速:
识别器就是您的
UIGestureRecognizer
Simple and fast:
Where recognizer is your
UIGestureRecognizer