uilongpressgesturerecognizer只开火一次

时间:2016-06-14 13:01:44

标签: ios objective-c

我想添加一个手势,只有当一个人按下一秒左右时才会触发。不是水龙头,而是长按。如果我使用uilongpressgesturerecognizer它会一直射击,直到我松开手指。我怎么能解决这个问题。

4 个答案:

答案 0 :(得分:9)

创建和添加手势时设置minimumPressDuration,如下所示:

UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc]
                                                      initWithTarget:self action:@selector(handleLongPress:)];
longPress.minimumPressDuration = 1.0;

将您的代码编写为UIGestureRecognizerStateEnded状态,如下所示:

-(void)handleLongPress:(UILongPressGestureRecognizer *)Gesture{
    if (Gesture.state == UIGestureRecognizerStateEnded) {


       //Do any thing after long press ended,which will be 1.0 second as set above


    }
    else if (Gesture.state == UIGestureRecognizerStateBegan){



    }
}

答案 1 :(得分:0)

设置minimumPressDuration的{​​{1}}属性值。

答案 2 :(得分:0)

当按钮状态改变时,长按方法被调用一次(开始,结束) 如果你想要实现的东西,那就按照以下步骤进行。

//MARK:Button tap events
- (void)btnLongPress:(UILongPressGestureRecognizer*)gesture{
    switch (gesture.state) {
        case UIGestureRecognizerStateBegan:
        {
           //tmrForPoint is timer
            tmrForPoint = [NSTimer timerWithTimeInterval:0.5 target:self selector:@selector(btnAddTap) userInfo:nil repeats:YES];

            NSRunLoop * theRunLoop = [NSRunLoop mainRunLoop];
            [theRunLoop addTimer:tmrForPoint forMode:NSDefaultRunLoopMode];
        }
            break;
        case UIGestureRecognizerStateEnded:
        {
            [tmrForPoint invalidate];
            tmrForPoint = nil;
        }
            break;
        default:
            break;
    }
}

//Call this method every 0.5 Second
-(void)btnAddTap{
   NSLog("Method called")
}

答案 3 :(得分:0)

快速5

声明一个 @Override public void onClick(View v) { TextView tv = findViewById(R.id.rowTextView); tv.setPaintFlags(tv.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG); notifyDataSetChanged(); } }); } return convertView; } }

UILongPressGestureRecognizer

将其let longPressRecognizer = UILongPressGestureRecognizer(target: self, action: #selector(gestureAction(gesture:))) 设置为.minimumPressDuration或任意间隔。

将识别器1.0设置到ViewController并使用.delegate将其添加到视图中。

使用以下功能来处理手势:

.addGestureRecognizer()