0

I'm making a little game for myself, and I want a void to fire for every 0.1 seconds that the UIButton is pressed down (e.g., if the button is pressed for 0.7 seconds, the void will fire 7 times within that timeframe).

Is this possisble with a UIButton or do I need to create subviews that will detect a touch? If I use a subview, how do I make the method fire for every 0.1 seconds that the UIButton is pressed for?

3
  • ^do the code within the void ([self DoVoid];) May 5, 2013 at 2:55
  • you mean method to fire/call May 5, 2013 at 2:59
  • yes, i want the method to call every 0.1 seconds that the UIButton is held down for :) May 5, 2013 at 3:00

1 Answer 1

0

You could add a selector for UIControlEventTouchDown that starts an NSTimer ticking at a 0.1 second interval and then stop it on UIControlEventTouchUpInside and/or UIControlEventTouchUpOutside. If you are concerned about stopping it while the user's finger is not over the control, you can hook up a cancel event to UIControlEventTouchDragExit as well.

The UIControl method to use is addTarget:action:forControlEvents:. Note that the last argument is a bitmask, so you can hook up all the cancel/stop scenarios in one line of code.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.