2016-06-14 144 views
3

我想添加一个手势,只有当一个人已经按下一秒钟左右才会触发。不是轻拍,而是长按。如果我使用uilongpressgesturerecognizer它会持续发射,直到我释放我的手指。我怎样才能解决这个问题。uilongpressgesturerecognizer只触发一次

+1

你用什么样的姿势来看这个?如果您打开'gestureRecognizer.state'并只查找'.Ended'状态,那么每长按一次(长按结束时)将只发生一次。 – keithbhunter

回答

5

设置,当你创建并添加手势如下minimumPressDuration

-(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){ 



    } 
} 
+0

谢谢像魅力一样工作。 – TheHellOTrofasdasd

0

设定值minimumPressDuration财产UILongPressGestureRecognizer

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

编写代码在UIGestureRecognizerStateEnded状态如下:

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") 
}