View Full Version : Repeat some code while a button is kept pressed
arubinst
Oct 10, 2008, 12:17 PM
Hi,
I need a button to respond in the following way. When the user presses and releases, an action is executed. When the user presses this same button, but keeps pressing, the same action is executed repeatedly until the button is finally released.
Any ideas how to do this?
Thanks in advance,
Arubinst
italiano40
Oct 10, 2008, 12:20 PM
i would use a while loop
tyr2
Oct 10, 2008, 12:33 PM
You could use an NSTimer that checks the state of the button when it fires, if it is still active then it executes the relevant code again?
SqueegyX
Oct 10, 2008, 12:58 PM
You need a timer. Bind an action to the "Touch Down Inside" event of the button which turns on the timer. Bind an action to "Touch up Inside" which turns off the timer.
You may need to bind a few more of the buttons events to the timer turn off method. In case the the user touches the buttons, then drags outside and lets up for instance.
arubinst
Oct 10, 2008, 01:41 PM
Thank you guys. The timer seems to be the way to go! I got it working but now I want to take this to the next level.
I'm now trying to combine two timers (one fires the other), so that simply pressing the button and releasing makes one thing happen but keeping the button pressed for more than a second starts the cyclic repetition of an event.
I've never used NSTimers before, so this is a nice exercise. Again, thanks for your help.
Edit: I got it running the way I wanted it!
vBulletin® v3.8.6, Copyright ©2000-2012, Jelsoft Enterprises Ltd.