2010-10-25 42 views
5

我正在研究一个iPhone应用程序,它需要我检查按钮是否被敲击&按住6秒钟&然后触发正在播放某种声音的动作。在iPhone上检测长按

我应该如何检测这6秒的水龙头?

另一方面,用户也可以继续点击按钮6秒&然后同样的动作应该触发。

我应该怎么做多个水龙头,我怎么知道所有的水龙头都在6秒的支架下?

回答

17

为6秒长按,使用UILongPressGestureRecognizerminimumPressDuration属性设置为6

写你自己的gesture recognizer(比如说,LongTappingGestureRecognizer)连续攻为规定的周期;它不应该太棘手。给它一个属性,如UILongPressGestureRecognizerminimumPressDuration(比如说,minimumTappingDuration)和一个属性(比如说,maximumLiftTime),它决定了手指可以在不被认为是一个长敲手势之前被解除多长时间。

  • 当它第一次收到touchesBegan:withEvent:时,记录时间。
  • 当它收到touchesEnded:withEvent:时,请在maximumLiftTime之后启动一个NSTimer(提升计时器),以向手势识别器发送取消消息(例如cancelRecognition)。
  • 当有开始时间时收到touchesBegan:withEvent:时,取消升降计时器(如果有)。
  • cancelRecognition将转换为failed state

有处理时达到的姿态结束,minimumTappingDuration之后识别不同的策略。一种是如果当前时间和开始时间之间的差异大于等于minimumTappingDuration,则检入touchesBegan:withEvent:touchesEnded:withEvent:处理程序。这样做的问题在于,如果用户正在慢慢地敲击并且在到达minimumTappingDuration时hir手指处于关闭状态,则需要比minimumTappingDuration更长的时间来识别手势。另一种方法是在收到第一个touchesBegan:withEvent:时启动另一个NSTimer(识别计时器),一个将导致过渡到recognized state,并且在cancelRecognition中被取消。棘手的事情是如果定时器启动时手指抬起该怎么办。最好的方法可能是两者的组合,如果手指抬起,忽略识别计时器。

还有更多的细节,但这是主要的。基本上,这是一个长按识别器,可让用户在短时间内将手指从屏幕上抬起。您可能只使用窃听识别器并跳过长按识别器。

0

这是我的解决方案。

- (IBAction) micButtonTouchedDownAction { 
    self.micButtonTimer = [NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(micButtonAction:) userInfo:nil repeats:YES]; 
    self.micButtonReleased = FALSE; 
} 

- (IBAction) micButtonTouchedUpInsideAction { 
    self.micButtonReleased = TRUE; 
} 

- (IBAction) micButtonTouchedUpOutsideAction { 
    self.micButtonReleased = TRUE; 
} 

- (void) micButtonAction:(NSTimer *)timer { 
    [self.micButtonTimer invalidate]; 
    self.micButtonTimer = nil; 

    if(self.micButtonReleased) { 
     NSLog(@"Tapped"); 
    } 
    else { 
     NSLog(@"Touched"); 
    } 
} 
9

我意识到这是相当过时的问题,但答案应该很简单。

在您的视图控制器viewDidLoad中

//create long press gesture recognizer(gestureHandler will be triggered after gesture is detected) 
UILongPressGestureRecognizer* longPressGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(gestureHandler:)]; 
//adjust time interval(floating value CFTimeInterval in seconds) 
[longPressGesture setMinimumPressDuration:6.0]; 
//add gesture to view you want to listen for it(note that if you want whole view to "listen" for gestures you should add gesture to self.view instead) 
[self.m_pTable addGestureRecognizer:longPressGesture]; 
[longPressGesture release]; 

然后在你的gestureHandler

-(void)gestureHandler:(UISwipeGestureRecognizer *)gesture 
{ 
    if(UIGestureRecognizerStateBegan == gesture.state) 
    {//your code here 

    /*uncomment this to get which exact row was long pressed 
    CGPoint location = [gesture locationInView:self.m_pTable]; 
    NSIndexPath *swipedIndexPath = [self.m_pTable indexPathForRowAtPoint:location];*/ 
    } 
} 
+0

FWIW,OP描述应确认两种情况。一个是单个长按(这个代码检测)。另一个是在六秒钟内重复点击(这个代码没有检测到,我想?)。无论如何,上面的代码对大多数人都很有用,他们只是想知道如何设置长按的最小长度要求。 – ToolmakerSteve 2016-02-05 16:01:40