in my application i have an UILabel showing a time updated every second.
I have also draggable objects in the screen.
When I hide the label or i stop the timer everything is perfect but when i start the timer the animation performance of dragging objects go down.
I put the UILabel updating in a separate thread but no luck.
I need help friends :)
This is my code:
self.timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:#selector(onTimer:) userInfo:nil repeats:YES];
-(void) onTimer:(NSTimer *)timer;
{
timeInterval ++;
int hours = (int)timeInterval / 3600;
int minutes = (timeInterval %3600)/ 60;
int seconds = ((timeInterval%3600)%60);
NSString *timeDiff = [NSString stringWithFormat:#"%d:%02d:%02d", hours,minutes,seconds];
[NSThread detachNewThreadSelector:#selector(setText:) toTarget:self.time withObject:timeDiff];
}
Related
I am very new in Iphone Development. I have an issue . I am using a NSTimer which update a UiLabel in every second. now i have two problem :
when my app goes in background and after it when i open app . app goes hangs.
if i goes next or back on other ui screen then wen i comes on timer screen then my label again shows 0.
can anyone help me.
code which i am using :
timer = [NSTimer scheduledTimerWithTimeInterval: 1.0 target:self selector:#selector(updateCountdown) userInfo:nil repeats: YES];
-(void) updateCountdown
{
secondsLeft--;
//nits testing
if(secondsLeft == 1)
{
[self.view addSubview:recipePage6View.view];
}
if (secondsLeft<0)
{
[timer invalidate];
timer=nil;
lblDisplayTimer.text =#"00:00:00";
}
else
{
hours = secondsLeft / 3600;
minutes = (secondsLeft % 3600) / 60;
seconds = (secondsLeft %3600) % 60;
lblDisplayTimer.text = [NSString stringWithFormat:#"%02d:%02d:%02d", hours, minutes, seconds];
//lblDisplayTimer.text = [NSString stringWithFormat:#"%02d:%02d",minutes,seconds];
}
}
You need to set up a special background timer task and not have an NSTimer on the main run loop which gets suspended in background. The documentation on how to do this is here
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;
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];
I'm looking to make a UILabel display the current progress of a timer. Right now to get the current time left I call [timer1 timeLeft]; which returns an int. In this way I can update the label ONCE, at one instant. In what way can I update the label (mainLabel) constantly so that it is always displaying the current timer progress while being somewhat resource efficient?
Thanks for all your help!
Use following code for Countdown timer.
dblElapsedSeconds=0.0; //Declare this in header
tmrElapsedTime = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:#selector(updateElapsedTime) userInfo:nil repeats:YES]; //Declare timer variable in header
-(void)updateElapsedTime
{
dblElapsedSeconds += 1;
//double seconds = [[NSDate date] timeIntervalSinceDate:self.startTime];
int hours,minutes, lseconds;
hours = dblElapsedSeconds / 3600;
minutes = (dblElapsedSeconds - (hours*3600)) / 60;
lseconds = fmod(dblElapsedSeconds, 60);
[lblTimeElapsed setText:[NSString stringWithFormat:#"%02d:%02d",minutes, lseconds]];
}
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];