假如想让一个控件能有点击效果,那么该控件的所有父控件,及父控件以上的父控件,还有该控件本身,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(@"点击了黄色模块了"); }];}