CountDown Timer ios教程?

zee*_*ikh 27 nstimer ios countdowntimer

我正在进行考试的应用程序,所以当考试开始时,时间应该从那开始.例如30分钟它应该像29:59一样减少.

我该如何实现呢?

任何人都可以给我一个示例或一步一步的简单教程,我可以遵循?

Adr*_*n P 67

此代码用于创建倒数计时器.

.h文件的代码.

@interface UIMyContoller : UIViewController {

NSTimer *timer;
    IBOutlet UILabel *myCounterLabel;
}

@property (nonatomic, retain) UILabel *myCounterLabel;

-(void)updateCounter:(NSTimer *)theTimer;
-(void)countdownTimer;

@end
Run Code Online (Sandbox Code Playgroud)

.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];
}
Run Code Online (Sandbox Code Playgroud)

希望这可以帮助你.