Repeat some code while a button is kept pressed

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

  1. arubinst macrumors 6502

    Joined:
    May 26, 2008
    Location:
    Lausanne - Switzerland
    #1
    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
     
  2. italiano40 macrumors 65816

    italiano40

    Joined:
    Oct 7, 2007
    Location:
    NY
  3. tyr2 macrumors 6502a

    tyr2

    Joined:
    May 6, 2006
    Location:
    Leeds, UK
    #3
    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

    Joined:
    Mar 24, 2008
    #4
    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

    Joined:
    May 26, 2008
    Location:
    Lausanne - Switzerland
    #5
    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