How can I use NSTimer with this simple while loop? - iphone

I have a -(void) method being executed and at one point it gets to a while loop that gets values from the accelerometer directly the moment it asks for them
I went throughout the documentation regarding the NSTimer class but I couldn't make sense of how exactly I am to use this object in my case :
e.g.
-(void) play
{
......
...
if(accelerationOnYaxis >0 && accelerationOnYaxis < 0.9 )
{
startTimer;
}
while(accelerationOnYaxis >0 && accelerationOnYaxis < 0.9)
{
if(checkTimer >= 300msec)
{
printOut_AccelerationStayedBetweenThoseTwoValuesForAtLeast300msecs;
break_Out_Of_This_Loop;
}
}
stopTimerAndReSetTimerToZero;
.....
more code...
....
...
}
Any help?

You cannot do it with NSTimer, because it needs your code to exit in order to fire. NSTimer uses the event loop to decide when to call you back; if your program holds the control in its while loop, there is no way for the timer to fire, because the code that checks if it's time to fire or not is never reached.
On top of that, staying in a busy loop for nearly a second and a half is going to deplete your battery. If you simply need to wait for 1.4s, you are better off calling sleepForTimeInterval:, like this:
[NSThread sleepForTimeInterval:1.4];
You can also use clock() from <time.h> to measure short time intervals, like this:
clock_t start = clock();
clock_t end = start + (3*CLOCKS_PER_SEC)/10; // 300 ms == 3/10 s
while(accelerationOnYaxis >0 && accelerationOnYaxis < 0.9)
{
if(clock() >= end)
{
printOut_AccelerationStayedBetweenThoseTwoValuesForAtLeast300msecs;
break_Out_Of_This_Loop;
}
}

NSTimer works a little different than you want. What you need is a counter for your timer, to get how many times it looped. If your counter gets on 14 (if it's an integer), you can invalidate it.
//start timer
NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:0.1
target:self
selector:#selector(play:)
userInfo:nil
repeats:YES];
//stop timer
[timer invalidate];
You can create your function without that while.
- (void)play {
......
...
counter++; //declare it in your header
if(counter < 14){
x++; //your integer needs to be declared in your header to keep it's value
} else {
[timer invalidate];
}
useValueofX;
}
Take a look at the documentation.

Related

How to call a countdown timer from 5 second and it should decrement to 1 second in iPhone?

I have an application in which I want that when I run my application initially it should show a screen for 2 seconds that displays warning about the app and just after 2 seconds a countdown should start from 5 seconds that should decrement from 5 second to 1 second.
There are two ways:
[NSTimer timerWithTimeInterval: .....]
[self performSelector: withObject: afterDelay:]
so if i got your point you want to display a warning screen and after 2 seconds you want to show the user count down start from 5 and end at 1 .. well this can done easy by using a timer and counter as following :
define a NSTimer and start it once the warning view is shown .. your definition will be like this:
timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:#selector(timerFires) userInfo:nil repeats:YES];
[timer fire];
define a global integer and set its initial Value to 7 (lets suppose you name it counter).
-in timerFires selector you decreament counter by 1 and check its value when its equal to 5 start to show its vlaue on UILabel for example and when its 1 invalidate the timer and do you what you want at this point .. the timerFires will be like this:
- (void)timerFires
{
counter --;
if(counter ==5)
{
//show its Value
}
if(counter ==1)
{
[timer invalidate];
timer = nil;
//Do other stuff
}
}
[NSTimer scheduledTimerWithTimeInterval:60 target:self selector:#selector(updateFunction) userInfo:NULL repeats:YES];
In updateFunction you can write your code for count down.
You can also set "repeats:NO" and to call the updateFunction at the end of itself if it is necessary

Increase timeInterval of a Timer [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Change the time interval of a Timer
So I have a timer:
timer=[NSTimer scheduledTimerWithTimeInterval:2 target:self selector:#selector(createNewImage) userInfo:nil repeats:YES];
And I would like that the timer decrease every ten seconds, that scheduledTimerWithTimeInterval goes to 1.5 after ten seconds then to 1.0 after 10 seconds... Is it possible to do this, and if it is possible, how can I do it?
You can't modify a timer after you created it. To change the timer interval, invalidate the old timer and create a new one with the new interval.
You don't have to use several timers, all you have to do is add a variable to use for the time interval, then create a method that invalidates the timer, changes the variable and starts the timer again. For instance you could make a method that starts the timer.
int iTimerInterval = 2;
-(void) mTimerStart {
timer = [NSTimer scheduledTimerWithTimeInterval:iTimerInterval target:self selector:#selector(createNewImage) userInfo:nil repeats:YES];
}
-(void) mTimerStop {
[timer invalidate];
iTimerInterval = iTimerInterval + 5;
[self mTimerStart];
}
This would be the simple way to decrease the timer interval and keep the timer going, but I would personally prefer using the one below, because it makes sure that the timer has only ran once, that way it will not duplicate the instance, forcing your app to become glitchy and it also makes things easier for you.
int iTimerInterval = 2;
int iTimerIncrementAmount = 5;
int iTimerCount;
int iTimerChange = 10; //Variable to change the timer after the amount of time
bool bTimerRunning = FALSE;
-(void) mTimerToggle:(BOOL)bTimerShouldRun {
if (bTimerShouldRun == TRUE) {
if (bTimerRunning == FALSE) {
timer = [NSTimer scheduledTimerWithTimeInterval:iTimerInterval target:self selector:#selector(mCreateNewImage) userInfo:nil repeats:YES];
bTimerRunning = TRUE;
}
} else if (bTimerShouldRun == FALSE) {
if (bTimerRunning == TRUE) {
[timer invalidate];
bTimerRunning = FALSE;
}
}
}
-(void) mCreateNewImage {
//Your Code Goes Here
if (iTimerCount % iTimerChange == 0) { //10 Second Increments
iTimerInterval = iTimerInterval + iTimerIncrementAmount; //Increments by Variable's amount
[self mTimerToggle:FALSE]; //Stops Timer
[self mTimerToggle:TRUE]; //Starts Timer
}
iTimerCount ++;
}
-(void) mYourMethodThatStartsTimer {
[self mTimerToggle:TRUE];
}
I didn't finish all of the coding, but this is most of what you will need. Just change a few things and you'll be good to go!
You will need a set or sequence of timers, one for each different time interval. Stop/invalidate one timer and start the next timer when you want to change to the next time interval in your time sequence.

NSTimer - if statement not working within timer - CountDown Timer

I am working on a count down timer and am having trouble getting the if statement to stop the timer when the count is less that 0. Any guidance on solving this would be greatly appreciated. Thanks in advance for your help..
-(void) startCountdown{
time = 90;
//NSLog[#"Time Left %d Seconds", time];
//This timer will call the function updateInterface every 1 second
myTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:#selector(updateInterface:) userInfo:nil repeats:YES];
}
-(void) updateInterface:(NSTimer*)theTimer{
if(time >= 0){
time --;
CountDownText.text = [NSString stringWithFormat:#"%d", time];
NSLog(#"Time Left %d Seconds", time);
}
else{
CountDownText.text =#"Times Up!";
NSLog(#"Times Up!");
// Timer gets killed and no longer calls updateInterface
[myTimer invalidate];
}
}
I tested your code and it worked perfectly and the timer stopped at -1. So my best guess is that time might be declared as unsigned value, so it never becomes less than zero.
Looks like your countdown won't stop until it gets to -1 (instead of 0) because you're checking for greater-than-or-equal to zero and then decrementing (and then displaying).
Either decrement first and then check if time is greater-than zero:
-(void) updateInterface:(NSTimer*)theTimer{
time --;
if(time > 0){
CountDownText.text = ...
or check if time is greater-than 1:
-(void) updateInterface:(NSTimer*)theTimer{
if(time > 1){
time --;
CountDownText.text = ...

Countdown timer iphone sdk?

I have a countdown timer in my game and I'm trying to figure out how to make it so that it shows two decimal places and records with 2 decimal places in my table. Right now it counts down as a whole number and records as a whole number. Any ideas?
-(void)updateTimerLabel{
if(appDelegate.gameStateRunning == YES){
if(gameVarLevel==1){
timeSeconds = 100;
AllowResetTimer = NO;
}
timeSeconds--;
timerLabel.text=[NSString stringWithFormat:#"Time: %d", timeSeconds];
}
countdownTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:#selector(updateTimerLabel) userInfo:nil repeats:YES];
To have sub-second updates, the timer's interval needs to be < 1. But the precision of NSTimer is just around 50 ms, so scheduledTimerWithTimeInterval:0.01 will not work.
Moreover, the timer can be delayed by various activities, so using timeSeconds will lead to inaccurate timing. The usual way is compare the NSDate now with the date when the timer starts. However, as this code is for a game, the current approach may cause less frustration to players esp. if the program or background processes consumes lots of resources.
The first thing to do is to convert the countdownTimer to sub-second interval.
countdownTimer = [NSTimer scheduledTimerWithTimeInterval:0.67 target:self selector:#selector(updateTimerLabel) userInfo:nil repeats:YES];
Then, don't count down the time by seconds, but centiseconds:
if(appDelegate.gameStateRunning == YES){
if(gameVarLevel==1){
timeCentiseconds = 10000;
AllowResetTimer = NO;
}
}
timeCentiseconds -= 67;
Finally, divide by 100 in the output:
timerLabel.text=[NSString stringWithFormat:#"Time: %d.%02d", timeCentiseconds/100, timeCentiseconds%100];
Alternatively, use a double:
double timeSeconds;
...
if(appDelegate.gameStateRunning == YES){
if(gameVarLevel==1){
timeSeconds = 100;
AllowResetTimer = NO;
}
}
timeSeconds -= 0.67;
timerLabel.text=[NSString stringWithFormat:#"Time: %.2g", timeSeconds];

for loop execution doesn't permit touches on iPhone

Ok. I know the title may be confusing.
Logic That I have implemented is something like this.
There is an detector in application ( like bike speedometer - moving arrow )
When user taps start scan button - first method executes.
NowStartMovements decides random rotations & random number to stop
There are 1 to 10 numbers on detector.
Every thing works fine up to now.
Following code is error free.
Arrow moves perfectly & stops at proper position ( decided randomly )
But the problem is " I have implemented for loop for the movements "
So, while executing for loop, User interaction isn't enabled.
I have also added the code that I have implemented.
-(IBAction)ScanStart:(id)sender
{
btnScan.enabled=NO; stopThroughButtons=NO; shouldNeedleGoRightSide=YES; currentNeedleValue=1; nxtNeedleValue=2;
[NSTimer scheduledTimerWithTimeInterval:0 target:self selector:#selector(nowStartMovements) userInfo:nil repeats:NO];
}
-(void)nowStartMovements{
totalRotations=arc4random()%9; if(totalRotations<3) totalRotations+=3;
currentRotation=0;stopValue=arc4random()%11; if(stopValue<1)stopValue=1;
int totalMovements=(totalRotations-1)*10 + ( (totalRotations%2==0)?10-stopValue:stopValue ), i;
for(i=0;i<totalMovements;i++){
if (stopThroughButtons) return;
[NSThread detachNewThreadSelector:#selector(moveNeedle) toTarget:self withObject:nil];
usleep(200000);
}
}
-(void)moveNeedle{
spinAnimation = [CABasicAnimation animationWithKeyPath:#"transform.rotation.z"];
double fromValue=[[arrayOfFloatValues objectAtIndex:currentNeedleValue-1] doubleValue];
double toValue=[[arrayOfFloatValues objectAtIndex:nxtNeedleValue-1] doubleValue];
spinAnimation.duration=0.2;
spinAnimation.fromValue=[NSNumber numberWithFloat:fromValue];
spinAnimation.toValue = [NSNumber numberWithFloat:toValue];
[imgNideel.layer addAnimation:spinAnimation forKey:#"spinAnimation"];
[NSThread detachNewThreadSelector:#selector(MoveActualNeedle) toTarget:self withObject:nil];
}
-(void)MoveActualNeedle{
if(shouldNeedleGoRightSide){
if(currentNeedleValue<9) { currentNeedleValue++; nxtNeedleValue++;}
else { shouldNeedleGoRightSide=NO; currentNeedleValue=10; nxtNeedleValue=9;
}
imgNideel.transform=CGAffineTransformMakeRotation([[arrayOfFloatValues objectAtIndex:currentNeedleValue-1] doubleValue]);
} else {
if(currentNeedleValue>2){ currentNeedleValue--; nxtNeedleValue--;}
else { shouldNeedleGoRightSide=YES; currentNeedleValue=1; nxtNeedleValue=2;
}
imgNideel.transform=CGAffineTransformMakeRotation([[arrayOfFloatValues objectAtIndex:currentNeedleValue-1] doubleValue]);
}
}
You will need to rewrite your logic so it is the Timer that is doing the sleeping, not the usleep. Rewrite your function so that every iteration of the repeatable timer does what is in the for loop.
The problem is the for loop is sleeping on the main thread. If you use a timer and set repeats to YES, then this essesntially does the for/sleep pattern you are doing. When you want to stop it call [timer invalidate];
Ideally, you'd use a timer to schedule the needle movements. The quickest solution to your existing code is this:
In StartScan, change -scheduledTimerWithTimeInterval: to -performSelectorInBackground:
In nowStartMovements, change -detachNewThreadSelector: to -performSelectorOnMainThread:
This way, the usleep happens on a background thread and doesn't block the main thread. The UI will be frozen as long as the main thread is blocked.