UserInteraction仅为子视图启用
我有一个视图和view.UserInteractionenabled = no,一个button被添加到视图。 我只需要点击button。 是否有可能只启用button的交互。
视图不能接收触摸,除非userInteractionEnabled
对于视图及其所有的视图直到UIWindow
对象为YES
。
你可以让UIView
的子类包含这个button,并通过覆盖hitTest:withEvent:
来让它忽略button之外的触摸。 例:
@interface MyView : UIView @property (nonatomic, strong) IBOutlet UIButton *button; @end @implementation MyView - (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event { UIView *subview = [super hitTest:point withEvent:event]; return subview == self.button ? subview : nil; } @end