如何响应开始在视图之外触摸并在iOS中拖动进入的触摸事件 [英] How to response a touch event which begin to touch down outside of a view and drag enter it in iOS

查看:21
本文介绍了如何响应开始在视图之外触摸并在iOS中拖动进入的触摸事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在视图上响应这种触摸事件:开始在视图外触摸并拖动进入.我曾尝试使用 iOS UIControlEvent,例如 UIControlEventTouchDragEnter、UIControlEventTouchDragInside 和 UIGesture,但我发现无法直接执行此操作.最后,我用自己的方式实现它:创建一个自定义的 UIView 子类并覆盖方法 (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event 并将触摸事件转发到响应者链.在父视图的touchesBegan、touchesMoved、touchesEnded方法中,我使用UITouch对象的位置来判断视图外的touch down并拖动进入.我对这种方式不满意.有没有人可以告诉我一个更有效和优雅的方法来解决它?非常感谢您的时间和考虑.

解决方案

开始在视图之外触地并拖动进入它

你永远不能更优雅"地做到这一点,因为如果你最初的着陆在视图之外,那么它就不会与这个视图相关联,也永远不会.无论触摸的初始命中测试与哪个视图相关联,该视图在整个手势中始终是此触摸的视图,并且触摸事件将仅发送到该视图.命中测试的默认定义是初始触摸 inside 的视图是命中测试视图,并且根据假设,不是您的视图.p>

I want to response this kind of touch event on a view:begin to touch down outside of the view and drag enter it. I have tried to use the iOS UIControlEvent such as UIControlEventTouchDragEnter,UIControlEventTouchDragInside and the UIGesture, and I found no way can do this directly. Finally, I implement it with my own way:Create a custom subclass of UIView and overwrite the method (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event and forward touch event up to the responder chain. In the touchesBegan, touchesMoved, touchesEnded method of parent view, I use the location of UITouch object to judge touch down outside of the view and drag enter it. I am not satisfied with this way. Is there anyone can tell me a more efficient and elegant way to work out it? Thank you very much for your time and consideration.

解决方案

begin to touch down outside of the view and drag enter it

You can never do this more "elegantly" because if your initial touch down is outside the view, then it is not associated with this view and never will be. Whatever view the touch's initial hit test associates it with, that is the view that will always be this touch's view throughout the gesture, and touch events will be sent only to that view. The default definition of hit testing is that the view that the initial touch is inside is the hit-test view, and that, by hypothesis, is not your view.

这篇关于如何响应开始在视图之外触摸并在iOS中拖动进入的触摸事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆