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(@"點擊了黃色模塊了");
    }];
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

友情鏈接更多精彩內(nèi)容