Home > DeveloperSection > Forums > Timer and animation events trumping TouchesEnded events
naomi burke
naomi burke

Total Post:28

Posted on    September-14-2015 11:40 PM

 iPhone iPhone  Ios  Objective C 

 1 Reply(s)
 449  View(s)
Rate this:

I've implemented a tap-and-hold handler using an NSTimer that I first set in the TouchesBegan overload.

However, what I actually want is for an action to be continuously performed in quick-fire succession while the touch is being held. So, on timer expiry I call a handler to do the work, which then sets another timer and the cycle continues until the TouchesEnded comes in and cancels it, or another terminating condition is met.


This works fine, until my handler code triggers an animation to go off at the same time. Now we have animation events and timer events going off, and in all that we need to handle TouchesEnded as well.


Problem is that, if the animation is triggered, and I set my timer to less than 0.025 seconds, my TouchesEnded event doesn't come through until the timer cycle stops (the other terminating condition). Setting a slower timer, or not triggering the animation, make it work (TouchedEnded comes in straight away), but are not what I want.


Obviously this is all on the device (release build - no NSLogs) - in the sim it all works fine


Is there any way of setting the relative priorty of these events - or is it likely I'm missing something else obvious here?



I've worked around this in this instance by doing the continuous part without visual feedback until it's done (which from this users perspective is instant). I think this is ok for now.

Glen Martin
Glen Martin

Total Post:27

Posted on    September-15-2015 11:00 PM

Try writing your own Timer-type class by spawning off onto a thread.

Example:  BOOL continue = YES; //outside of your @implementation

-(void) showLoop



      [NSThread sleepForTimeInterval: .030];

      [self performSelectorOnMainThread:@selector(function name) waitUntilDone:YES];



and this would be started by [NSThread detatchNewThreadSelector:@selector(showLoop) toTarget:self withObject:nil]. This is not exactly threadsafe, but you can choose to wrap the boolean into a NSNumber and then do @synchronize on it if you so choose. Alternatively, after I wrote that little snippet I realized it would be better to do a check against the current NSTime instead of sleepForTimeInterval: but you get the point.

Don't want to miss updates? Please click the below button!

Follow MindStick