为了账号安全,请及时绑定邮箱和手机立即绑定

如何忽略触摸事件并将其传递给另一个子视图的UIControl对象?

如何忽略触摸事件并将其传递给另一个子视图的UIControl对象?

iOS
尚方宝剑之说 2019-12-21 12:58:19
我有一个自定义的UIViewController,其UIView占据了屏幕的一角,但是除了其中有一些按钮和内容的部分以外,其余大部分都是透明的。由于该视图上对象的布局,该视图的框架可以掩盖其下方的一些按钮。我希望能够忽略该视图上的任何触摸,如果它们没有触摸任何重要的东西,但是我似乎只能传递实际的触摸事件(touchesEnded / nextResponder的东西)。如果我有一个UIButton或类似的不使用touchesEnded的东西,如何将touch事件传递给它?我不能只是手动找出要调用的按钮选择器,因为此自定义ViewController可以用于许多不同的视图。我基本上需要一种方法来称呼它:[self.nextResponder touchesEnded:touches withEvent:event];以及UIControl类型。
查看完整描述

3 回答

?
开满天机

TA贡献1786条经验 获得超12个赞

可能最好的方法是hitTest:withEvent:在要忽略触摸的视图中重写。根据视图层次结构的复杂性,有两种简单的方法可以实现此目的。


如果在该视图下有对该视图的引用,则可以忽略:


- (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;

}

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


- (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;

}

我建议第一种方法最可靠(如果有可能获得对基础视图的引用)。


查看完整回答
反对 回复 2019-12-21
  • 3 回答
  • 0 关注
  • 624 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信