Repeat some code while a button is kept pressed

Discussion in 'iOS Programming' started by arubinst, Oct 10, 2008.

  1. arubinst macrumors 6502

    May 26, 2008
    Lausanne - Switzerland

    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,

  2. italiano40 macrumors 65816


    Oct 7, 2007
  3. tyr2 macrumors 6502a


    May 6, 2006
    Leeds, UK
    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?
  4. SqueegyX macrumors regular

    Mar 24, 2008
    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.
  5. arubinst thread starter macrumors 6502

    May 26, 2008
    Lausanne - Switzerland
    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!

Share This Page