如何忽略触摸事件并将它们传递到另一个子视图的UIControl对象? [英] How to ignore touch events and pass them to another subview's UIControl objects?

查看:198
本文介绍了如何忽略触摸事件并将它们传递到另一个子视图的UIControl对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个自定义的UIViewController的UIView占据屏幕的一角,但大部分是透明的,除了它的部分,有一些按钮和东西。由于该视图上对象的布局,视图的框架可以覆盖它下面的一些按钮。我想要能够忽略任何触摸该视图,如果他们没有触摸任何重要的东西,但我似乎只能传递实际的触摸事件(touchesEnded / nextResponder的东西)。如果我有一个UIButton或类似的没有使用touchesEnded,我如何传递触摸事件呢?

I have a custom UIViewController whose UIView takes up a corner of the screen, but most of it is transparent except for the parts of it that have some buttons and stuff on it. Due to the layout of the objects on that view, the view's frame can cover up some buttons beneath it. I want to be able to ignore any touches on that view if they aren't touching anything important on it, but I seem to only be able to pass along actual touch events (touchesEnded/nextResponder stuff). If I have a UIButton or something like that which doesnt use touchesEnded, how do I pass the touch event along to that?

我不能只是手动找出按钮选择器来调用,因为这个自定义的ViewController可以用于许多不同的视图。我基本上需要一个方法来调用这个:

I can't just manually figure out button selector to call, because this custom ViewController can be used on many different views. I basically need a way to call this:

[self.nextResponder touchesEnded:touches withEvent:event];

在UIControl类型。

on UIControl types as well.

推荐答案

可能最好的方法是重写 hitTest:withEvent:在你想要忽略的触摸视图。根据视图层次结构的复杂性,有几种简单的方法来实现这一点。

Probably the best way to do this is to override hitTest:withEvent: in the view that you want to be ignoring touches. Depending on the complexity of your view hierarchy, there are a couple of easy ways to do this.

如果你有一个对视图下面的视图的引用忽略: / p>

If you have a reference to the view underneath the view to ignore:

- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
{
    UIView *hitView = [super hitTest:point withEvent:event];

    // If the hitView is THIS view, return the view that you want to receive the touch instead:
    if (hitView == self) {
        return otherView;
    }
    // Else return the hitView (as it could be one of this view's buttons):
    return hitView;
}

如果您没有对视图的引用:

If you don't have a reference to the view:

- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
{
    UIView *hitView = [super hitTest:point withEvent:event];

    // If the hitView is THIS view, return nil and allow hitTest:withEvent: to
    // continue traversing the hierarchy to find the underlying view.
    if (hitView == self) {
        return nil;
    }
    // Else return the hitView (as it could be one of this view's buttons):
    return hitView;
}

我建议第一种方法是最强大的获取对底层视图的引用)。

I would recommend the first approach as being the most robust (if it's possible to obtain a reference to the underlying view).

这篇关于如何忽略触摸事件并将它们传递到另一个子视图的UIControl对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆