raistlin
raistlin

Reputation: 4346

How to cancel already fired non-repeating NSTimer?

What I want:
1) 4 puzzle images in UIImageViews, which I can move around 2) after solving puzzle I want to show 2 images instead puzzles: first one just after puzzles are solved, second one 2 seconds after the first image...

What I have:
1) if you're solving really fast one puzzle after another you may fall into NSTimer interval, which will swap these 2 images too fast, like after 0.1 sec not 2.0

Is there any way to solve NSTimer, to not interfere with already fired one?

Solution(?): I was thinking about blocking interaction, so the second image will appear, but it seems like a poor workaround.

Upvotes: 0

Views: 294

Answers (2)

Naveen Thunga
Naveen Thunga

Reputation: 3675

Write setter method, Like this. Whenever you need to cancel timer

self.myTimer = nil;

- (void)setMyTimer:(NSTimer *)inTimer
{
   if (inTimer != mMyTimer)
  {
    [mMyTimer invalidate];
    [mMyTimer release];
    mMyTimer = [inTimer retain];
  }
}  

Hope this might work for you.

Upvotes: 1

Rok Jarc
Rok Jarc

Reputation: 18865

You should keep a reference to this timer (as a property or as an iVar).

Then simply call [myTimer invalidate]; myTimer = nil; when you want to cancel it.

EDIT:

If you are not keeping reference to your timer (if you're creating it from scratch inside code block) there's always a danger of creating multiple instances...

Upvotes: 2

Related Questions