如何响应触摸事件

如何响应触摸事件

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

问题描述

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

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.

推荐答案

你永远不能优雅地这样做,因为如果你的初始触地不在视图之外,那么它是与此观点无关,永远不会。无论触摸的初始命中测试与哪个视图相关联,即在整个手势中总是是该触摸视图的视图,并且触摸事件将仅发送到该视图。命中测试的默认定义是初始触摸内部的视图是命中测试视图,并且假设您的视图。

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中输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 10:51