iphone - 仅对 subview 启用 UserInteraction

标签 iphone objective-c ios ipad subview

我有一个 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/

相关文章:

ios - 如何在 Interface Builder 中显示替代语言? (Xcode5)

ios - UICollectionViewCell 未显示

ios - CoreBluetooth 状态保存问题 : willRestoreState not called in iOS 7. 1

ios - 在 iOS 上使用 HTTP post 发送图像和文本

ios - 无法快速使用 Protocol 扩展 ObjC 类

objective-c - UIButton状态已选择

ios - CLLocationManager 最佳实践

iPhone SDK - orkut API

iPhone模拟器失败

iPhone创建精简版