iOS 关于单击手势与cell点击冲突的解决方案

2019-07-22  本文已影响0人  Cary9396

问题展示:

view上添加tableView,给view添加单击手势,点击cell却走的是手势方法.(如何让点击cell时手势方法不响应呢?)

解决方案:

OC代码:

 UITapGestureRecognizer *tap=[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapClick)];
   tap.delegate=self;   
   [self.view addGestureRecognizer:tap];

   #pragma mark-手势代理,解决和tableview点击发生的冲突
   -(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
      if ([NSStringFromClass([touch.view class]) isEqualToString:@"UITableViewCellContentView"]) {//判断如果点击的是tableView的cell,就把手势给关闭了
         return NO;//关闭手势
      }//否则手势存在
      return YES;
    }

swift代码:

        let tap = UITapGestureRecognizer(target: self, action: #selector(removeView))
        self.addGestureRecognizer(tap)
        tap.delegate = self

    func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldReceive touch: UITouch) -> Bool {
        
        if String(describing: touch.view?.classForCoder) == "UITableViewCellContentView" {
           return false
        } 
       else {
           return true
        }
    }
上一篇下一篇

猜你喜欢

热点阅读