How to response a touch event which begin to touch down outside of a view and drag enter it in iOS(如何响应开始在视图之外触摸并在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中拖动进入的触摸事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何响应开始在视图之外触摸并在iOS中拖动进入的触摸事件


- Android - 我如何找出用户有多少未读电子邮件? 2022-01-01
- MalformedJsonException:在第1行第1列路径中使用JsonReader.setLenient(True)接受格式错误的JSON 2022-01-01
- Android viewpager检测滑动超出范围 2022-01-01
- 用 Swift 实现 UITextFieldDelegate 2022-01-01
- Android - 拆分 Drawable 2022-01-01
- 在测试浓缩咖啡时,Android设备不会在屏幕上启动活动 2022-01-01
- 使用自定义动画时在 iOS9 上忽略 edgesForExtendedLayout 2022-01-01
- android 4中的android RadioButton问题 2022-01-01
- 如何检查发送到 Android 应用程序的 Firebase 消息的传递状态? 2022-01-01
- 想使用ViewPager,无法识别android.support.*? 2022-01-01