Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIButton Long Press Event

I want to emulate a long a press button, how can I do this? I think a timer is needed. I see UILongPressGestureRecognizer but how can I utilize this type?

like image 411
Andrea Avatar asked May 30 '11 17:05

Andrea


People also ask

What is long press on Mac?

Overview. Long-press (also known as press-and-hold) gestures detect one or more fingers (or a stylus) touching the screen for an extended period of time. You configure the minimum duration required to recognize the press and the number of times the fingers must be touching the screen.

How does long press work?

Many times, touch & hold lets you take action on something on your screen. For example, to move an app icon on your home screen, touch & hold, then drag it to the new location. Sometimes touch & hold is called a "long press."

How do you use UILongPressGestureRecognizer?

UILongPressGestureRecognizer is a concrete subclass of UIGestureRecognizer . The user must press one or more fingers on a view and hold them there for a minimum period of time before the action triggers. While down, the userʼs fingers canʼt move more than a specified distance or the gesture fails.


1 Answers

You can start off by creating and attaching the UILongPressGestureRecognizer instance to the button.

UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPress:)]; [self.button addGestureRecognizer:longPress]; [longPress release]; 

And then implement the method that handles the gesture

- (void)longPress:(UILongPressGestureRecognizer*)gesture {     if ( gesture.state == UIGestureRecognizerStateEnded ) {          NSLog(@"Long Press");     } } 

Now this would be the basic approach. You can also set the minimum duration of the press and how much error is tolerable. And also note that the method is called few times if you after recognizing the gesture so if you want to do something at the end of it, you will have to check its state and handle it.

like image 114
Deepak Danduprolu Avatar answered Sep 19 '22 10:09

Deepak Danduprolu