CountDown Timer ios教程?

我正在做一个考试正在进行的应用程序,所以考试开始时,应该从那开始。 例如30分钟,应该像29:59那样减less。

我怎样才能实现呢?

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

此代码用于创build倒数计时器。

.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]; } 

希望这可以帮助你。

Interesting Posts