我有一个 View 和 view.UserInteractionenabled = no 并且一个按钮被添加到 View 中。我只需要点击按钮。是否可以仅启用按钮的交互。
最佳答案
View 无法接收触摸,除非 userInteractionEnabled
对 View 及其所有父 View 直至 UIWindow
对象为 YES
。
您可以创建一个 UIView
的子类来包含该按钮,并通过覆盖 hitTest:withEvent:
使其忽略按钮外部的触摸。示例:
@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
关于iphone - 仅对 subview 启用 UserInteraction,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13414108/