从一个UIView到另一个UIView的第一响应者

从一个UIView到另一个UIView的第一响应者

本文介绍了如何“转移”从一个UIView到另一个UIView的第一响应者?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个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:


  • UIView(Controller的视图)


    • 带框架的自定义视图(0,0,300,300)

    • 带框架(0,0,100,50)的UIButton

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

08-24 03:08