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.
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.
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...
}
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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With