iOS事件传递

作者: 码工人生 | 来源:发表于2020-01-06 21:41 被阅读0次

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

    相关文章

      网友评论

        本文标题:iOS事件传递

        本文链接:https://www.haomeiwen.com/subject/pullactx.html