iOS事件传递

如果想让一个控件能有点击效果,那么该控件的所有父控件,及父控件以上的父控件,还有该控件本身,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(@"点击了黄色模块了");
    }];
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

友情链接更多精彩内容