5

I'm making a calculator app and am supplying my own keypad with UIButtons. I have a delete key and everything works except that the user has to keep pressing the delete key over and over again if they want to delete all.

I was wondering if there is a way to delete everything when the button is held for more than 2 seconds.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
OnkaPlonka
  • 1,232
  • 10
  • 19

3 Answers3

4

The simplest way of implementing this would be attaching a long press gesture recognizer to your [Delete] button.

Xcode lets you attach long press gesture recognizer in the interface builder. Add it to your button, configure the duration of long press, and connect the handler to IBOutlet in the same way that you connect other UI events.

If you would rather do it in code, this answer shows you how.

Community
  • 1
  • 1
Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
2

Use your own timer function to handle this

-(IBAction)buttonHit {
    //here start timer that fires for every 2 seconds and handle deletion method in that
}

-(IBAction)buttonReleased {
   //Stop timer...
}
Guru
  • 21,652
  • 10
  • 63
  • 102
1

In your subclassed UIButton, you might want to watch the "touchesBegan: withEvent:" UIResponder method and if it passes a certain threshold of time, then start deleting like crazy (that is, until the "touchesEnded: withEvent" method gets called).

Michael Dautermann
  • 88,797
  • 17
  • 166
  • 215