Fengson
Fengson

Reputation: 4912

Detect NOT touching for 3 seconds in iOS?

I am making a game with Sprite Kit.
I would like to give user some hints when he hasn't touched a screen for 3 seconds. What would be the best way to do this?

I was thinking about creating a BOOL value and change it in touchesBegin, then setting an interval which would trigger a method unless touching was detected.

Anyone has a better idea?
Thanks

Upvotes: 1

Views: 121

Answers (1)

Michael Dautermann
Michael Dautermann

Reputation: 89509

I think your idea is on the right track to start with.

But instead of using a "BOOL" value (which only tells you TRUE or FALSE), why not use two NSTimeInterval ivars (instance variables) to mark the last time a pair of touchesBegan and touchesEnded (or touchesCancelled) gets called.

If the value of touchesBegan is greater than touchesEnded, then an active touch is going on.

If the value of touchesEnded is more than 3 seconds beyond the current time (and the time of touchesBegan is still before touchesEnded), then it's time to show your hint.

Upvotes: 1

Related Questions