1

Hi i have a Button i want hold this button to write something but i don't know how can i recognize hold button , can you help me ? thank you

Mc.Lover
  • 4,813
  • 9
  • 46
  • 80

2 Answers2

6

TouchDownInside event triggered, start a NStimer. TouchUpInside event triggered, cancel the timer. Make the timer call your method to execute if the user holds the button : the timer delay will be the amount of time required to recognize hold.

Andiih
  • 12,285
  • 10
  • 57
  • 88
5

You can also use UILongPressGestureRecognizer.

In your initialization method (e.g. viewDidLoad), create a gesture recognizer and attach it to your button:

UILongPressGestureRecognizer *gesture = [[UILongPressGestureRecognizer alloc] 
  initWithTarget:self 
  action:@selector(myButtonLongPressed:)];
// you can control how many seconds before the gesture is recognized  
gesture.minimumPressDuration = 2; 
// attach the gesture to your button
[myButton addGestureRecognizer:gesture];
[gesture release];

The event handler myButtonLongPressed: should look like this:

- (void) myButtonLongPressed:(UILongPressGestureRecognizer *)gesture
{
  // Button was long pressed, do something
}

Note that UILongPressGestureRecognizer is a continuous event recognizer. While the user is still holding down the button, myButtonLongPressed: will be called multiple times. If you just want to handle the first call, you can check the state in myButtonLongPressed::

if (gesture.state == UIGestureRecognizerStateBegan) {
  // Button was long pressed, do something
}
Community
  • 1
  • 1
Shiki
  • 16,688
  • 7
  • 32
  • 33