UITapGestureRecognizer会屏蔽掉Button的点击事件

来源:互联网 发布:淘宝静物摄影赚钱 编辑:程序博客网 时间:2024/04/30 13:29

前几天在做项目的时候,遇到这个一个问题,在一个视图也就是UIView上添加一个手势,然后又在这个View上添加一个UIButton,然后给按钮添加事件,运行项目的时候我发现,不管是点击按钮还是视图上的别的地方执行的都是手势所拥有的方法,后来到网上找才发现是手势把按钮的方法给屏蔽了,那怎么解决了正确的方法是给手势设置代理,然后在代理中进行判断如果点击事件是由Button执行的,那就不执行手势,那么系统会调用按钮所拥有的方法。具体的如下:

UIView* showListView=[[UIView alloc] i nitWithFrame:[[UIScreen mainScreen]  bounds]];  UITapGestureRecognizer* showTap=[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(showGes:)]; showTap.delegate=self;//设置代理 [showListView addGestureRecognizer:showTap];  [showTap release];

UIButton* btn=[UIButton buttonWithType:UIButtonTypeCustom];btn.frame=CGRectMake(100.0,200.0,100.0,35.0);[btn addTarget:self action:@selector(coverViewChoose:) forControlEvents:UIControlEventTouchUpInside];[showListView addSubView:btn];

最重要的就是下面了:
#pragma mark--#pragma mark--UIGestureRecognizerDelegate-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch{    if([touch.view isKindOfClass:[UIButton class]])    {        return NO;    }    return YES;}
关键就是这个代理方法了,如果在代理中不做判断,手势将会将按钮的方法给屏蔽掉