iOS UIGestureRecognizer手势

2016-11-01  本文已影响32人  小傑

1.UILongPressGestureRecognizer(长按手势)

1.关于长按手势调用方法执行2次的解决办法

//由于长按手势调用方法会在UIGestureRecognizerStateBegan,UIGestureRecognizerStateEnded各执行一次,所以区分开这2个状态就能避免调用2次
UILongPressGestureRecognizer *longs = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(saveWithPhone:)];
 [self addGestureRecognizer:longs];

- (void)saveWithPhone:(UILongPressGestureRecognizer *)longs
{
    
    if (longs.state == UIGestureRecognizerStateBegan)
    {
        //在这写需要执行的代码
    } else if (longs.state == UIGestureRecognizerStateEnded)
    {
        
    }
}

2.多个手势同时使用

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
    return YES;
}
  1. 这个能够避免cell点击和UITableView的点击事件的冲突
    注意:给cell添加手势,在手势的范围区域里,它会处理手势,而不是cell的点击事件,在范围外它会处理cell的点击事件
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
    
    // 若为UITableViewCellContentView(即点击了tableViewCell),则不截获Touch事件
    if ([NSStringFromClass([touch.view class]) isEqualToString:@"UITableViewCellContentView"]) {
        return NO;
    }else
    {
        return YES;
    }
}
上一篇 下一篇

猜你喜欢

热点阅读