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

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

问题描述

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

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.

如果您有对视图下方的视图的引用要忽略:

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天全站免登陆