开发者

detecting long tap on iPhone

开发者 https://www.devze.com 2023-01-21 15:40 出处:网络
I am working on an i开发者_如何学JAVAPhone app which requires me to check if the button has been tapped & held pressed for 6 seconds & then fire an action which is playing some sort of sound.

I am working on an i开发者_如何学JAVAPhone app which requires me to check if the button has been tapped & held pressed for 6 seconds & then fire an action which is playing some sort of sound.

How should I detect this 6 second tap?

On the other hand the user can also keep on tapping button for 6 seconds & then the same action should fire.

What should I do with multiple taps, how would I know that all the taps fall under the 6 second bracket?


For a six second long press, use a UILongPressGestureRecognizer with its minimumPressDuration property set to 6.

Write your own gesture recognizer (say, LongTappingGestureRecognizer) for continuous tapping for a given period; it shouldn't be too tricky. Give it a property like UILongPressGestureRecognizer's minimumPressDuration (say, minimumTappingDuration) and a property (say, maximumLiftTime) that determines how long a finger can be lifted off before it's not considered to be a long tapping gesture.

  • When it first receives touchesBegan:withEvent:, record the time.
  • When it receives touchesEnded:withEvent:, start an NSTimer (the lift timer) that sends the gesture recognizer a cancel message (e.g. cancelRecognition) after maximumLiftTime.
  • When it receives touchesBegan:withEvent: when there's a start time, cancel the lift timer (if any).
  • The cancelRecognition will transition to the failed state.

There are various strategies for handling recognizing when the end of the gesture is reached, after minimumTappingDuration. One is to check in both the touchesBegan:withEvent: and touchesEnded:withEvent: handlers if the difference between the current time and the start time is >= minimumTappingDuration. The problem with this is that it will take longer than minimumTappingDuration to recognize the gesture if the user is tapping slowly and hir finger is down when the minimumTappingDuration is reached. Another approach is to start another NSTimer (the recognition timer) when the first touchesBegan:withEvent: is received, one that will cause transition to the recognized state and that is cancelled in cancelRecognition. The tricky thing here is what to do if the finger is lifted when the timer fires. The best approach might be a combination of the two, ignoring the recognition timer if the finger is lifted.

There's more to the details, but that's the gist. Basically, it's a long press recognizer that lets the user lift hir finger off the screen for brief periods. You could potentially use just the tapping recognizer and skip the long press recognizer.


I realize this is quite dated question, however answer should be pretty simple.

In your View controller 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];

Then in your 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];*/
    }
}


Here is my solution.

- (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");
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号