美文网首页
关于UILongPressGestureRecognizer的一

关于UILongPressGestureRecognizer的一

作者: TEASON | 来源:发表于2015-09-15 13:52 被阅读2345次

    原创, 转载需注明出处

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

    相关文章

      网友评论

          本文标题:关于UILongPressGestureRecognizer的一

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