Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get functionality of Long Press gesture in iOS below 3.2

UILongPressGesture is available in ios ver 3.2 and later. But i am trying to develop application for maximum compatibility and hence targeting ios ver2.0

Can anyone please guide me on how to accomplish long press gesture in ios v2.0

like image 302
Amit Avatar asked Dec 30 '10 20:12

Amit


2 Answers

For a single finger, it's pretty simple: Start a timer in the touchesBegan method and trigger an action when the timer fires. Cancel the timer if you get a touchesEnded before it fires. Here's an implementation that uses the performSelector:withObject:afterDelay: method.

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    [self performSelector:@selector(fireLongPress)
               withObject:nil
               afterDelay:LONG_PRESS_THRESHOLD];
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    [NSObject cancelPreviousPerformRequestsWithTarget:self];
}

- (void)fireLongPress {
    // do what you want to do
}

You'll probably also want to kill the timer if the finger moves too far.

With multitouch, it's a bit more complicated. You'll have to keep track of which touch is which, and decide what to do e.g. when one finger has pressed long enough but the other hasn't (or figure out what UILongPressGestureRecognizer does).

like image 108
Daniel Dickison Avatar answered Sep 28 '22 17:09

Daniel Dickison


Implement the touches... methods in your view. If a certain amount of time passes between touchesBegan:withEvent: and touchesEnded:withEvent: without any touchesMoved:withEvent: events, you have a long press.

like image 28
Ole Begemann Avatar answered Sep 28 '22 18:09

Ole Begemann