UserInteraction enable for subview only

微笑、不失礼 提交于 2019-11-30 03:26:19

问题


I have a view and view.UserInteractionenabled = no and a button is added to the view. i need to click the button only . is it possible to enable interaction for button only.


回答1:


A view cannot receive touches unless userInteractionEnabled is YES for the view and all of its superviews up to the UIWindow object.

You can make a subclass of UIView to contain the button, and make it ignore touches outside the button by overriding hitTest:withEvent:. Example:

@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


来源:https://stackoverflow.com/questions/13414108/userinteraction-enable-for-subview-only

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!