UITapGestureRecognizer会屏蔽掉Button的点击事件

来源:互联网 发布:淘宝客户关系维护流程 编辑:程序博客网 时间:2024/04/30 14:28

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

[html] view plaincopyprint?
  1. UIView* showListView=[[UIView alloc] i nitWithFrame:[[UIScreen mainScreen]  bounds]];  
  2.   UITapGestureRecognizer* showTap=[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(showGes:)];  
  3.  showTap.delegate=self;//设置代理  
  4.  [showListView addGestureRecognizer:showTap];  
  5.   [showTap release];  

[html] view plaincopyprint?
  1. UIButton* btn=[UIButton buttonWithType:UIButtonTypeCustom];  
  2. btn.frame=CGRectMake(100.0,200.0,100.0,35.0);  
  3. [btn addTarget:self action:@selector(coverViewChoose:) forControlEvents:UIControlEventTouchUpInside];  
  4. [showListView addSubView:btn];  

最重要的就是下面了:
[html] view plaincopyprint?
  1. #pragma mark--  
  2. #pragma mark--UIGestureRecognizerDelegate  
  3. -(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch  
  4. {  
  5.     if([touch.view isKindOfClass:[UIButton class]])  
  6.     {  
  7.         return NO;  
  8.     }  
  9.     return YES;  
  10. }  
关键就是这个代理方法了,如果在代理中不做判断,手势将会将按钮的方法给屏蔽掉
0 0
原创粉丝点击