如果想讓一個控件能有點擊效果,那么該控件的所有父控件,及父控件以上的父控件,還有該控件本身,userInteractionEnabled 一定要為YES;而該控件的子控件userInteractionEnabled 不管是YES還是NO都不影響。

1578317797751.jpg
代碼如下
/// 事件點擊層級
- (void)testOne{
self.redView = [[UIView alloc]init];
[self.view addSubview:self.redView];
[self.redView mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.right.equalTo(self.view);
make.height.mas_equalTo(300);
make.top.equalTo(self.view.mas_top).offset(84);
}];
self.redView.backgroundColor = [UIColor redColor];
self.redView.userInteractionEnabled = YES;//此處必須為YES
self.yellowView = [[UIView alloc]init];
[self.redView addSubview:self.yellowView];
[self.yellowView mas_makeConstraints:^(MASConstraintMaker *make) {
make.center.equalTo(self.redView);
make.width.height.mas_equalTo(200);
}];
self.yellowView.backgroundColor = [UIColor yellowColor];
self.yellowView.userInteractionEnabled = YES;//此處必須為YES
self.cyanView = [[UIView alloc]init];
[self.yellowView addSubview:self.cyanView];
[self.cyanView mas_makeConstraints:^(MASConstraintMaker *make) {
make.center.equalTo(self.yellowView);
make.height.mas_equalTo(100);
make.width.mas_equalTo(100);
}];
self.cyanView.backgroundColor = [UIColor cyanColor];
self.cyanView.userInteractionEnabled = YES;//此處也可以為NO或者YES
[self addEventMethod];
}
- (void)addEventMethod{
[self.yellowView bk_whenTapped:^{
NSLog(@"點擊了黃色模塊了");
}];
}