I am trying to use the current time in my movie player to increment a sliders value in my iPad app.
slider.maximumValue=10;
slider.minimumValue=0;
How can I increment slider.value from 0 to 10 in a specific time period?
You can use an NSTimer to repeatedly add 1 to your slider value if you know the specific time period beforehand:
NSTimer *aTimer = [NSTimer scheduledTimerWithTimeInterval:(timePeriod/slider.maximumValue)
target:self selector:#selector(timerFired:) userInfo:nil repeats:YES];
- (void)timerFired:(NSTimer*)theTimer {
[slider setValue:slider.value + 1.0];
if(slider.value == slider.maximumValue) {
[theTimer invalidate];
}
}
Related
I want to invoke a method 24 times but between each invocation I want an interval of 1 sec,
now am using
[NSTimer scheduledTimerWithTimeInterval:1 target:self selector:#selector(loadNumber) userInfo:nil repeats:YES];
but how to count invocations, I want to invalidate timer after 24 count can I invalidate that.
Create an instance variable for an int, it doesn't matter what you call it. Set it to "0" before you call the timer.
someInt = 0;
[NSTimer scheduledTimerWithTimeInterval:1 target:self selector:#selector(loadNumber:) userInfo:nil repeats:YES];
In the action that the time calls check the value of this number to make sure the action should be performed and increment the number.
- (void)loadNumber:(NSTimer *)sender
{
if (someInt <= 24) {
//do something
someInt ++;
}else{
[sender invalidate];
someInt = 0;
}
}
You should set up a counter in loadNumber, which will invalidate your NSTimer after your desired repeat counts.
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
I want to display an image for 5-6 secs and then link a slider to it to show increasing time. That is say if time is 0 seconds, slider is at extreme left and if Timer is 1 seconds slider moves appropriately towards right and if Timer is 3 seconds than slider is in the middle and as time increases gradually 4 -5 seconds it moves right and at 6 seconds its on its extreme right. Can you guide me on this
Use below one
timerForSlider = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:#selector(updateSlider) userInfo:nil repeats:YES];//timerForSlider in .h file
-(void)updateSlider
{
[slider setValue:slider.value+1];
if(slider.value==6)
{
[timerForSlider invalidate];
}
}
In XIB set slider minimumVal =0; MaximumVal=6; initialVal = 0;
this is timer callback (selector):
-(void) timer {
//dSliderValue - step of slider moveing. set it to 2
if (slider.value+dSliderValue <= 60) [slider setValue:slider.value+dSliderValue];
else {
[t invalidate];
//any other code here, on timer stopping
}
}
this is timer (NSTimer *t). call it on start image showing:
[slider setMaximumValue:60];
[slider setValue:0];
dSliderValue = 2;
t = [NSTimer scheduledTimerWithTimeInterval:0.2 target:self selector:#selector(timer) userInfo:nil repeats:YES];
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.
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];