IOS给tableview的cell添加长按手势执行两次(UILongPressGestureRecognizer)

来源:互联网 发布:格子 玻尔兹曼 软件 编辑:程序博客网 时间:2024/06/05 00:13

这里我们为tableview添加长按手势

UILongPressGestureRecognizer *longPressGr = [[UILongPressGestureRecognizeralloc] initWithTarget:selfaction:@selector(longPressAction:)];

    longPressGr.minimumPressDuration = 0.5f;

    longPressGr.numberOfTouchesRequired =1;

    [_tableView addGestureRecognizer:longPressGr];

    [longPressGr release];


这时我们会发现每次按住tableView并且松开的时候, longPressAction: 这个方法会执行2次

- (void)longPressAction:(UILongPressGestureRecognizer *)longPress

{

    if (longPress.state ==UIGestureRecognizerStateBegan) {

        CGPoint point = [longPress locationInView:_tableView];

        

        NSIndexPath *indexPath = [_tableViewindexPathForRowAtPoint:point]; // 可以获取我们在哪个cell上长按

        

        if (indexPath != nil) {

            NSLog(@"%ld", indexPath.row);

        }

        

    }

}



0 0
原创粉丝点击