美文网首页
iOS UIGestureRecognizer手势

iOS UIGestureRecognizer手势

作者: 小傑 | 来源:发表于2016-11-01 10:40 被阅读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;
    }
}

相关文章

网友评论

      本文标题:iOS UIGestureRecognizer手势

      本文链接:https://www.haomeiwen.com/subject/tukmuttx.html