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

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


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


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


以及UIControl类型。


尚方宝剑之说
浏览 628回答 3
3回答

开满天机

可能最好的方法是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;}我建议第一种方法最可靠(如果有可能获得对基础视图的引用)。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

iOS