如何“转移"从一个 UIView 到另一个 UIView 的第一响应者?

2023-07-07移动开发问题
1

本文介绍了如何“转移"从一个 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(控制器的视图)
    • 带框架的CustomView (0, 0, 300, 300)
    • 带框架的 UIButton (0, 0, 100, 50)

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

    The End

相关推荐

硬件音量按钮更改应用程序音量
Hardware Volume buttons change in app volume(硬件音量按钮更改应用程序音量)...
2024-08-12 移动开发问题
10

Cocos2d - 如何检查不同层中对象之间的交集
Cocos2d - How to check for Intersection between objects in different layers(Cocos2d - 如何检查不同层中对象之间的交集)...
2024-08-12 移动开发问题
8

突出显示朗读文本(在 iPhone 的故事书类型应用程序中)
Highlight Read-Along Text (in a storybook type app for iPhone)(突出显示朗读文本(在 iPhone 的故事书类型应用程序中))...
2024-08-12 移动开发问题
9

Cocos2D + 仅禁用 Retina iPad 图形
Cocos2D + Disabling only Retina iPad Graphics(Cocos2D + 仅禁用 Retina iPad 图形)...
2024-08-12 移动开发问题
10

正确的 cocos2d 场景重启?
Proper cocos2d scene restart?(正确的 cocos2d 场景重启?)...
2024-08-12 移动开发问题
7

[ios.cocos2d+box2d]如何禁用自动旋转?
[ios.cocos2d+box2d]how to disable auto-rotation?([ios.cocos2d+box2d]如何禁用自动旋转?)...
2024-08-12 移动开发问题
7