ios 倒数计时器ios教程?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17145112/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 23:58:06  来源:igfitidea点击:

CountDown Timer ios tutorial?

iosnstimercountdowntimer

提问by zeeshan shaikh

I'm making an application where the exams is going on, so when exam start the, time should start with that. For example 30 minutes and it should reduce like 29:59.

我正在申请进行考试,所以当考试开始时,时间应该从那个开始。例如 30 分钟,它应该像 29:59 一样减少。

How can I implement this?

我该如何实施?

Can anyone please give me a sample example or a easy step by step tutorial that i can follow?

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

回答by Adrian P

This code is used to create a countdown timer.

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

Code for .h file.

.h 文件的代码。

@interface UIMyContoller : UIViewController {

NSTimer *timer;
    IBOutlet UILabel *myCounterLabel;
}

@property (nonatomic, retain) UILabel *myCounterLabel;

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

@end

Code for .m file.

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

Hope this helps you out.

希望这可以帮助你。