开发者

iPhone adding a timer to an app

开发者 https://www.devze.com 2023-01-02 14:54 出处:网络
How would I go about adding a simple 2 minute timer to 开发者_运维问答my app in almost the exact same way that the clock app does?I just want the user to click start and have the timer start displayin

How would I go about adding a simple 2 minute timer to 开发者_运维问答my app in almost the exact same way that the clock app does? I just want the user to click start and have the timer start displaying the timer counting down from 2:00 and beep when it hits 0:00.


I have created some basic code for generating a timer.

This method will be called when user selects start timer:

-(void)startTimer{

timer = [NSTimer timerWithTimeInterval:1 target:self selector:@selector(countdown) userInfo:nil repeats:YES];//Timer with interval of one second
[[NSRunLoop mainRunLoop] addTimer:timer forMode:NSDefaultRunLoopMode];
}

This method will be called when the timer is triggered:

-(void)countdown{

NSLog(@"Countdown : %d:%d",minutesValue,secondsValue);//Use this value to display on your UI Screen
[self countdownSeconds];//Decrement the time by one second
}

This method will be called to decrement time by one minute:

-(void)countdownMinutes{

if(minutesValue == 0)
    [self stopTimer]; 
else
    --minutesValue;
}

This method will be called to decrement the time by one second:

-(void)countdownSeconds{

if(secondsValue == 0 )
{
    [self countdownMinutes];
    secondsValue = 59;
}
else
{
    --secondsValue;
}
}

This method is called when timer reaches zero:

-(void)stopTimer{

[timer invalidate]; //Stops the Timer and removes from runloop
NSLog(@"Countdown completed"); // Here you can add your beep code to notify
}

One important thing "timer","minutesValue" and "secondsValue" are instance variables.


I wrote this yesterday, you may find it helpful. It's a method for pulling hours, minutes, and seconds out of a NSTimeInterval (which is a struct double representing the number of seconds between two times--in this case, NSDate self.expires and [NSDate date], i.e. right now). This is happening inside a custom table cell view. At the end we update three UILabels on a little stopwatchy display thing.

-(void)updateTime
{
    NSDate *now = [NSDate date];
    NSTimeInterval interval = [self.expires timeIntervalSinceDate:now];
    NSInteger theHours = floor(interval / 3600);
    interval = interval - (theHours * 3600);
    NSInteger theMinutes = floor(interval / 60);
    interval = interval - (theMinutes * 60);
    NSInteger theSeconds = floor(interval);

    NSLog(@"%d hours, %d minutes, %d seconds", theHours, theMinutes, theSeconds);

    self.hours.text = [NSString stringWithFormat:@"%02d", theHours];
    self.minutes.text = [NSString stringWithFormat:@"%02d", theMinutes];
    self.seconds.text = [NSString stringWithFormat:@"%02d", theSeconds];

}

Then elsewhere I set a timer to call this method once a second. Timers aren't guaranteed to run at their exact specific time, which is why you can't just count down some static variable, or you run the risk of accumulating errors over time. Instead you actually have to do new time math each call.

Be sure you keep a pointer to your timer and invalidate it when your viewcontroller goes away!


This may be exactly what you want Orientation aware clock tutorial


You could use NSTimer.

0

精彩评论

暂无评论...
验证码 换一张
取 消