关于UILongPressGestureRecognizer的一些细节(避免重复触发,结合tableview)

原创, 转载需注明出处

这篇文章涵盖UILongPressGestureRecognizer之外, 还包括对tableview点击位置捕获得到所在的indexpath
1.避免重复触发长按

1.添加gesture在要触发的控件上面

    // long press gesture
    UILongPressGestureRecognizer *lpgr = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleLongPressAtComment:)] ;
    lpgr.minimumPressDuration = 1.0f ;
    [_table addGestureRecognizer:lpgr] ;

2.长按需要注意一点的是不重复触发,解决方案在于判断
UIGestureRecognizerState
3.捕获点击的位置 [x locationInView:y]方法
4.得到indexpath [x indexPathForRowAtPoint:y]

- (void)handleLongPressAtComment:(UILongPressGestureRecognizer *)longPressRecognizer
{
    if (longPressRecognizer.state != UIGestureRecognizerStateBegan) return ; // except multiple pressed it !
    
    CGPoint p = [longPressRecognizer locationInView:_table] ;// get longpress pt
    NSIndexPath *indexPath = [_table indexPathForRowAtPoint:p] ; // get indexpath from table with point .    
    NSInteger row = indexPath.row ;
    NSLog(@"long press on commt at row %ld", (long)row) ;
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容