最好的方法可能是在您希望忽略接触的视图中覆盖hitTest:withEvent:
.根据视图层次 struct 的复杂程度,有几种简单的方法可以做到这一点.
如果要忽略视图下方的视图引用,请执行以下操作:
- (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;
}
我建议第一种方法是最健壮的(如果有可能获得对底层视图的引用).