倒计时计时器ios教程



我正在制作一个考试正在进行的应用程序,所以当考试开始时,时间应该从它开始。例如30分钟,它应该减少到29:59。

我如何实现这个?

有没有人能给我一个例子或一个简单的一步一步的教程,我可以遵循?

此代码用于创建一个倒计时计时器。

.h文件代码

@interface UIMyContoller : UIViewController {
NSTimer *timer;
    IBOutlet UILabel *myCounterLabel;
}
@property (nonatomic, retain) UILabel *myCounterLabel;
-(void)updateCounter:(NSTimer *)theTimer;
-(void)countdownTimer;
@end

.m文件代码

@implementation UIMyController
@synthesize myCounterLabel;
int hours, minutes, seconds;
int secondsLeft;
- (void)viewDidLoad {
    [super viewDidLoad];
    secondsLeft = 16925;
    [self countdownTimer];
}
- (void)updateCounter:(NSTimer *)theTimer {
    if(secondsLeft > 0 ) {
        secondsLeft -- ;
        hours = secondsLeft / 3600;
        minutes = (secondsLeft % 3600) / 60;
        seconds = (secondsLeft %3600) % 60;
        myCounterLabel.text = [NSString stringWithFormat:@"%02d:%02d:%02d", hours, minutes, seconds];
    } else {
        secondsLeft = 16925;
    }
}
-(void)countdownTimer {
    secondsLeft = hours = minutes = seconds = 0;
    if([timer isValid]) {
        [timer release];
    }
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];  
    timer = [NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:@selector(updateCounter:) userInfo:nil repeats:YES];
    [pool release];
}

最新更新