我有一个 UIView 子类(CustomView
用于此问题),它自己处理所有 touches
事件(开始、移动、结束、取消).我还有一个 UIButton
,它是 CustomView
的兄弟,与它重叠.
I have a UIView subclass (CustomView
for purposes of this question) that has its own handling of all touches
events (Began, Moved, Ended, Cancelled). I also have a UIButton
that is a sibling of CustomView
that overlaps it.
例如,我的视图层次结构如下所示:
For example, my view hierarchy looks like this:
我希望 CustomView 在用户拖出 UIButton 后捕获触摸事件.通过调试日志,似乎 UIControlEventTouchDragExit
事件是要拦截的事件(尽管直到触摸距离按钮大约 100 像素时它才会触发,但这对这个问题并不重要).
I would like the CustomView to capture touch events once the user has dragged out of the UIButton. Through debug logging, it appears that the UIControlEventTouchDragExit
event is the one to intercept (though it doesn't fire until the touch is around 100 pixels away from the button, but that is not important to this question).
理想情况下,当拖动退出事件被触发时,按钮将停止接收触摸移动事件,CustomView 将收到一个 touchesBegan
事件(即使我需要以某种方式自己伪造一个),以及所有未来的触摸事件(touchesMoved
等)将发送到 CustomView.
Ideally, when the drag exit event is fired, the button would stop receiving touch move events, the CustomView would get a touchesBegan
event (even if I need to fake one myself somehow), and all future touch events (touchesMoved
, etc) would be sent to the CustomView.
我尝试了以下方法,但没有任何效果:
I have tried the following, and it doesn't have any effect:
-(void)btnTouchDragExit:(id)sender
{
UIButton * btn = sender;
[btn resignFirstResponder];
[customView becomeFirstResponder];
}
我认为底层的 UITouch
对象(在触摸事件中是一致的)没有被重新定位为指向 CustomView.怎样才能达到想要的效果?
I think the underlying UITouch
object (which is consistent across touch events) is not being retargeted to point at the CustomView. How can I achieve the desired effect?
只需使用 hitTest:withEvent: 来确定哪个视图应该"接收事件,然后将事件转发到适当的视图.在事件转到按钮之前,您可能需要做一些跳舞来拦截事件,但这就是 hitTest:withEvent: 的用途.
Just use hitTest:withEvent: to figure out what view "should" be receiving the event, then forward on the event to the appropriate view. You might have to do a little bit of dancing around to intercept events before they go to the button, but this is what hitTest:withEvent: was made for.
这篇关于如何“转移"从一个 UIView 到另一个 UIView 的第一响应者?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!