关于UILongPressGestureRecognizer的一

2015-09-15  本文已影响2345人  TEASON

原创, 转载需注明出处

这篇文章涵盖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) ;
}
上一篇 下一篇

猜你喜欢

热点阅读