您的位置:首页 > 产品设计 > UI/UE

子View不响应touch事件,让父View响应touch事件

2014-08-06 22:49 423 查看
在工作中遇到一个button上有一个uiImageview控件,但是功能要求,点击图片的时候,和button执行相同的功能

首先想到方法是 button上addTarget,然后在uiImageview上加一个手势,同时指向同一个方法,太傻逼

经过查资料,发现,直接将uiImageview对象的userInteractionEnabled属性,直接设置为NO,则touch事件就直接传到父View

<span style="font-size:18px;">  UIButton *btn = [[UIButton alloc]init];
[btn setFrame:CGRectMake(40 , 40 , 200, 40)];
btn.layer.borderWidth = 2;
[viewParent addSubview:btn];
[btn addTarget:self action:@selector(hello:) forControlEvents:UIControlEventTouchUpInside];
UIView *subView = [[UIView alloc]init];
[subView setFrame:CGRectMake(0, 0, 20, 20)];
subView.layer.borderWidth = 2;
subView.userInteractionEnabled = NO;
[btn addSubview:subView];</span>


类似这样,当你点击subview的时候,就可以运行hello:方法
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  uibutton uitouch 事件