0
votes

I need to take different actions depending on the press duration of UILongPressGestureRecognizer. For example pressing for 0.5 seconds before moving the touch would fire a different action than pressing for 1 second before moving the touch.

Right now I'm thinking of two different ways to achieve this.

a) Add two separate gesture recognizers, G1 with minimumPressDuration of 0.5 and G2 with minimumPressDuration of 1, and somehow cancel the G1 in the UIGestureRecognizerDelegate if G2 succeeds.

b) Add a single gesture recognizer and implement custom book keeping and state management to enable choosing the appropriate action.

Which one is the preferred way and why?

1

1 Answers

0
votes

UILongPressGestureRecognizer has two state UIGestureRecognizerStateBegan and UIGestureRecognizerStateEnded you can measure the time between states and determine which method to call.

Edit: To handle this condition you can use a NSTimer like this.

if (gesture.state == UIGestureRecognizerStateBegan)
{
    self.timer = [NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:@selector(forOneSec:) userInfo:nil repeats:NO];

    [self.timer fire];
}

If touch is not moved, your long press method will be called after 1 second. If user stops touch event before 1 second you need to invalidate the timer and call the other method.