Using NSTimer in a Loop - iphone

I need to show a timer counting down from 30 to 0, several times. (30 to 0, start over at 30, etc) However when I placed it in a for-loop, instead of waiting till the timer invalidates to begin the next timer, the loop iterates through and several timers are created.
How can I form a loop that waits until the timer has invalidated?
- (IBAction)startCountdown:(id)sender {
NSNumber *rounds = [[NSNumber alloc]initWithInt:sliderRounds.value];
for (int i = rounds.intValue; i > 0; i--) {
[self timer];
}

You can use the scheduledTimerWithTimeInterval
[NSTimer scheduledTimerWithTimeInterval: 0.5
target: self
selector: #selector(handleTimer:)
userInfo: nil
repeats: NO];
And inside the handleTimer you create the next timer

Looks like you are calling some method called 'timer' on self. What does it do? Creates a new timer? Then this is why. When a timer is created, it doesn't block. Instead a method call is scheduled and executed once the timer timeout passes.

What you could do is the following:
Create a class that holds an NSTimer
This class would have a variable to hold how many times it needs to count
At the end of one timer (and really, the only one) firing, it decrements the count
You can have the NSTimer repeat by calling it with:
[NSTimer scheduledTimerWithTimeInterval:30.0f
target:self
selector:#selector(timerFired)
userInfo:nil
repeats:YES];
In a method called - (void)timerFired, you would decrement the count. If it reaches zero, you would stop the timer.
Hope this helps!

Related

Scroll UISlider Automatically

So currently I have a UISlider in a UIViewcontroller that is meant to start animations within subviews when the user slides.. Basically when the user slides I have this battery with a filling in it that fills the empty battery image with a bar to indicate power within a cell, and the user can slide to see the energy the battery has at certain times of the day.
At the moment, when the View loads I would like the UISlider to AUTOMATICALLY start sliding from the beginning of the slider and scroll to the end within, lets say 5 seconds.
I implemented a loop that cycles through all the values of the uislider using this loop
for (int i = 0; i < [anObject count] - 2; i++)
{
sleep(.25);
NSUInteger index = (NSUInteger)(slider.value + 0.5); // Round the number.
[slider setValue:index animated:YES];
}
[anObject count] - 2 is equal to 62 at this time of day but will change and increment every 15 seconds because I'm fetching data from a server.
But that aside, why doesn't this work? The loop?
EDIT:
So heres what I did with NSTIMER
[NSTimer timerWithTimeInterval:0.25 target:self selector:#selector(animateSlider) userInfo:nil repeats:NO];
and animateSlider looks like this:
- (void)animateSlider:(NSTimer *)timer
{
NSLog(#"Animating");
NSUInteger index = (NSUInteger)(slider.value + 0.5); // Round the number.
[slider setValue:index animated:YES];
}
But no luck... Why isn't NSTimer "firing"..... I remmeber vaguely there was a method that FIRES an nstimer method but not sure if that's needed...
EDIT:
Ahh it does need "Fire"....
NSTimer *timer = [NSTimer timerWithTimeInterval:0.25 target:self selector:#selector(animateSlider) userInfo:nil repeats:NO];
[timer fire];
But for some reason it only fires once.... Any ideas ?
"for some reason it only fires once..."
If you changed the NSTimer set up to this:
NSTimer *timer =
[NSTimer scheduledTimerWithTimeInterval:0.25
target:self
selector:#selector(animateSlider:)
userInfo:nil
repeats:YES];
This would schedule the timer on the current run loop immediately.
And since the "repeats" parameter is "YES", you'd then repeat the timer every quarter second, until you invalidate the timer (which you should do when the ending condition is reached, like when the slider reaches its destination).
P.S. You'd need to change the selector method declaration of your timer's target slightly. According to Apple's documentation, "The selector must correspond to a method that returns void and takes a single argument. The timer passes itself as the argument to this method."
So declare "animateSlider" like this instead:
- (void)animateSlider: (NSTimer *) theTimer;

Calling function with interval but until some time passed Iphone

I need to make a call every .025 seconds over a duration of 30 seconds. Here is my .025 second timer.
NSTimer* myTimer = [NSTimer scheduledTimerWithTimeInterval: 0.025 target: self
selector: #selector(GetScreen:) userInfo: nil repeats:YES ];
How can I restrict this to a duration of 30 seconds?
If you only want it to continue for 30 seconds then you would just make a member variable to increment until it hits 30 seconds then you turn off your timer:
CGFloat timerTotal_;
//initialize it where you create your timer
timerTotal_ = 0.0f;
//every time your GetScreen: is called
timerTotal_+= .025;
if(timerTotal_ > 30)
{
[myTimer invalidate];
}
If you are trying to wait until some condition has been met, don't use a timer to do that. Use a delegate pattern, or notification/observer or semaphore, etc.
If you want to call GetScreen: (which should probably be getScreen: by normal Obj-C convention) once, after 30s, use this:
[ NSTimer scheduledTimerWithTimeInterval:30.0 target:self selector:#selector( getScreen: ) userInfo:nil repeats:NO ] ;

NSTimer firing instantly

I'm trying to develop a game and running into a small issue with NSTimer, once a sprite appear it has a certain amount on time in my scene before fading out.
double CALC_TIME = 5;
[NSTimer scheduledTimerWithTimeInterval:CALC_TIME target:self selector:#selector(hideSprite) userInfo:nil repeats:NO];
I want hideSprite to be called after 5 seconds, but instead it's called instantly(Or near instant).
A possible solution:
I know I could do this by setting the timer to repeat, and having a bool firstCall that is set first time and then the next interval the fading is done, the timer is invalidated but I don't think this is good practice
Like this:
bool firstCall = false;
-(void)hideSprite{
if(!firstCall){
firstCall = true
}else{
//fade out sprite
//Invalidate NSTimer
//firstCall = false;
}
}
Thanks for your help!
I suspect something else is calling hideSprite. The code you have written will cause the timer to wait for five seconds before calling the selector hideSprite.
Provide a different selector (write a new test method which just does an NSLog) to the timer and see what happens. This will tell you a) whether the timer is indeed immediately firing and b) if something else is calling hideSprite.
[NSTimer scheduledTimerWithTimeInterval:CALC_TIME target:self selector:#selector(testTimer) userInfo:nil repeats:NO];
-(void) testTimer { NSLog(#"Timer - and only the timer - called me."); }
-(void) hideSprite {
NSLog(#"I definitely wasn't called by the timer.");
}
Quite often it's easier to just use something like
[UIView animateWithDuration: 0 delay: 5 options:0 animations: nil completion:
^{
// fade sprite
}];
(not sure if animations can be nil, but you get the idea).
Consider initializing your timer using the initWithFireDate:interval:target:selector:userInfo:repeats method. Here is a more detailed look at NSTimer.

(iphone) do I need to invalidate timer when repeats: no?

[NSTimer scheduledTimerWithTimeInterval: target: selector: userInfo: repeats:NO];
When repeats: is set to NO, do I need to invalidate the timer inside the specified selector?
Thank you
Edit
Another question, if it self invalidates,
How do you properly cancel a such timer?
Since invalidating already-invalidated timer would crash I assume?
maintain a pointer to the timer and set it to nil inside the selector that will get fired?
No, the timer will invalidate itself
#Eugene if you are using
[NSTimer scheduledTimerWithTimeInterval: target: selector: userInfo: repeats:YES];
then in the selector method you need to give a function like this one
- (void)timerFireMethod:(NSTimer*)theTimer
so when you want to invalidate it you can have a condition like this one
if(workDone == YES)
{
[theTimer invalidate];
}
But if you are using NO in the repeat option then the timer will invalidate itself.
You can maintain flag to save whether the timer has been fired or not.
eg.
BOOL gameOver = NO;
NSTimer * gameOverTimer;
-(void)startGame
{
gameOverTimer = [NSTimer scheduledTimerWithTimeInterval:600 target:self selector:#selector(stopLevel:) userInfo:nil repeats:NO];
// your code
}
-(void)stopLevel:(id)sender
{
gameOver = YES;
// your code
}
-(void)levelFinishedSuccesfully
{
// this method will get called if user finishes the level before your timer ends/stops the level. So the timer is valid and we need to invalidate it
if(!gameOver)
{
[gameOverTimer invalidate];
gameOverTimer = nil;
}
// your code
}
Hope this helps.
If repeats is YES, the timer will repeatedly reschedule itself until invalidated. If NO, the timer will be invalidated after it fires.
you are missing to add the timer source to your runloop
addTimer:forMode:

How can I create an NSTimer that repeats twice and then stops

I'd like to create an NSTimer that repeats twice then stops and invalidates itself. I'd rather not use a loop if that's possible. Any thoughts on how I could do this?
Create a static int inside your timer delegate function that is initialized to 0.
Increment it each time the delegate is called.
When the counter reaches the value you wish invalidate the timer.
This is something your timer's target should handle, not something the timer itself should handle. You can either install a repeating timer and have the target invalidate it the second time it fires, or you can install a one-shot timer, reinstall it after the first time it fires, and then not set it up again the second time.
Basically, you need a state machine state variable that can be accessed both from the routine that initializes the timer, and from the timer's target.
Set the state variable to allow the first call to the timer task to restart the timer, but in that call also set that state variable so that subsequent calls do not restart.
Note that this kind of state variable can be used for any number of timer task repetitions, by simply decrementing it.
State machines are pretty much how all (synchronous) digital chips and logic works.
I very much disagree with the Jeremy that this is something that the target should handle. In fact I disagree so much that I have created my own Timer class, based on NSTimer, that you can configure in detail.
- (void) doSomething: (Timer*) timer
{
NSLog(#"This is iteration %d", timer.currentIteration);
}
- (void) startDoingSomething
{
Timer* timer = [Timer new];
timer.interval = 5.0; // Fire every 5 seconds
timer.delay = 2.5; // Start firing after 2.5 seconds
timer.iterations = 3; // Only fire three times
timer.target = self;
timer.selector = #selector(doSomething:);
[timer schedule];
// Don't forget to release timer somewhere - the above is just an example
}
See http://github.com/st3fan/ios-utils
One solution might look similar to this.
Launching the timer
[NSTimer scheduledTimerWithTimeInterval:3 target:self selector:#selector(timerMethod:) userInfo:nil repeats:YES];
Handling the timer and repetitions
int repetitions = 2; //EDIT: remove static declaration (see below)
- (void)timerMethod:(NSTimer*)theTimer{
NSLog(#"Timer fired");
repetitions--;
if(repetitions == 0){
[theTimer invalidate];
NSLog(#"Timer stopped");
}
}
EDIT:
I removed the static modifier above to make a more generic example. The original intent of the static was to persist the timer across objects of similar type, a request that the OP did not make.