仅将触摸事件转发到被触摸的视图

发布于 2024-12-19 05:27:03 字数 907 浏览 1 评论 0 原文

我有一个带有文本字段、按钮和表格的 UIWindow。 我希望能够跟踪屏幕上的所有触摸,然后将它们转发到被触摸的元素。

我已在 sendEvent 的信息="nofollow">Apple文档但我仍然不明白:

  1. 如何使用hitTest来检索被触摸的元素
  2. 如何转发触摸

这就是我所拥有的 迄今为止。

- (void) sendEvent:(UIEvent *)event
{

    for (UITouch *touch in [event allTouches])
    {
        /* Get coordinates of touch */
        CGPoint point = [touch locationInView:self];

        /* Get subview being touched */
        /* something like this???
           UIView *receiver = [self hitTest:point withEvent:event];            
         */

        /* Forward touch event to right view */
        /* how??? */

    }

    [super sendEvent:(UIEvent *)event];
}

谢谢。

I have a UIWindow with a text field, a button, and a table.
I would like to be able to track all the touches on the screen and then forward them to the element being touched.

I have read about overriding sendEvent in the Apple documentation but I still do not understand:

  1. How to use hitTest to retrieve the element being touched
  2. How to forward touches

This is what I have so far.

- (void) sendEvent:(UIEvent *)event
{

    for (UITouch *touch in [event allTouches])
    {
        /* Get coordinates of touch */
        CGPoint point = [touch locationInView:self];

        /* Get subview being touched */
        /* something like this???
           UIView *receiver = [self hitTest:point withEvent:event];            
         */

        /* Forward touch event to right view */
        /* how??? */

    }

    [super sendEvent:(UIEvent *)event];
}

Thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

七度光 2024-12-26 05:27:03

我不确定这是最好的解决方案,但我正在关注此处发布的内容。

基本上我有一个覆盖整个空间的 UIView 子类。这样的类包含对所有可以触摸的元素的引用。 (我希望有一种方法可以避免这种情况)

这是标题中的代码

 @interface SubclassUIView : UIView {       
    UITextField *text;
    UITableView *table;
    UIButton        *button;
    UIToolbar       *toolbar;
}

这是实现:

- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
    CGPoint tableHit = [table convertPoint:point fromView:self];
    CGPoint buttonHit = [button convertPoint:point fromView:self];
    CGPoint toolbarHit = [toolbar convertPoint:point fromView:self];
    CGPoint messageHit = [text convertPoint:point fromView:self];

    if ([table pointInside:tViewHit withEvent:event]) return table;
    else if ([button pointInside:buttonHit withEvent:event]) return button;
    else if ([toolbar pointInside:toolbarHit withEvent:event]) return toolbar;
    else if ([text pointInside:messageHit withEvent:event]) return text;

    return [super hitTest:point withEvent:event];
}

I am not sure this is the best solution but I am following what has been posted here.

Basically I have a subclass of UIView covering the entire space. Such class contains a reference to ALL the elements that can be touched. (I wish there was a way to avoid that)

This is the code in the header

 @interface SubclassUIView : UIView {       
    UITextField *text;
    UITableView *table;
    UIButton        *button;
    UIToolbar       *toolbar;
}

And this is the implementation:

- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
    CGPoint tableHit = [table convertPoint:point fromView:self];
    CGPoint buttonHit = [button convertPoint:point fromView:self];
    CGPoint toolbarHit = [toolbar convertPoint:point fromView:self];
    CGPoint messageHit = [text convertPoint:point fromView:self];

    if ([table pointInside:tViewHit withEvent:event]) return table;
    else if ([button pointInside:buttonHit withEvent:event]) return button;
    else if ([toolbar pointInside:toolbarHit withEvent:event]) return toolbar;
    else if ([text pointInside:messageHit withEvent:event]) return text;

    return [super hitTest:point withEvent:event];
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文