倒计时

作者: 徐老茂 | 来源:发表于2015-12-03 22:10 被阅读297次

刚刚有朋友让我写个倒计时器,我就简单写了一个,比较简陋,大家可以在这个基础上写点更复杂的.好了,代码里的注释很详细,上代码.

#import "ViewController.h"

@interface ViewController ()
@property(nonatomic, strong)NSTimer *timer;//定时器
@property(nonatomic, assign)CGFloat timeValue;//时间值
@property(nonatomic, strong)UILabel *label;//用于显示时间
@property(nonatomic, strong)UIButton *button;//用于选择暂停或是继续倒计时
@end
static NSInteger flag = 0;
@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    _label = [[UILabel alloc]initWithFrame:CGRectMake(100, 100, 100, 100)];
    _label.font = [UIFont systemFontOfSize:50];
    [self.view addSubview:_label];
    
    _timeValue = 5.0;//初始化时间是5秒
    _label.text = [NSString stringWithFormat:@"%.1f",_timeValue];//将_timeValue的值显示再label上面
    _timer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(timeChanged) userInfo:nil repeats:YES];//这个定时器没隔0.1秒执行一次,并刷新_label的值
    
    /*添加一个button用于控制暂停或是继续倒计时*/
    _button = [UIButton buttonWithType:UIButtonTypeSystem];
    _button.frame = CGRectMake(200, 100, 200, 100);
    [_button setTitle:@"暂停" forState:UIControlStateNormal];
    [_button addTarget:self action:@selector(buttonAction) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_button];
    
    
}

-(void)buttonAction
{
    if (flag == 0) {
        [_timer setFireDate:[NSDate distantFuture]];//暂停NSTimer
        [_button setTitle:@"继续" forState:UIControlStateNormal];
        flag = 1;
    }
    else
    {
        [_timer setFireDate:[NSDate distantPast]];//继续NSTimer
        [_button setTitle:@"暂停" forState:UIControlStateNormal];
        flag = 0;
    }
}

-(void)timeChanged
{
    _timeValue -= 0.1;//让_timeValue值减1
    _label.text = [NSString stringWithFormat:@"%.1f",_timeValue];
    if (_timeValue <= 0.0) {
        //当时间到了0,让label的值为0.0,并且弹出一个UIAlertController让用户选择确定还是重来
        _label.text = @"0.0";
        [_timer setFireDate:[NSDate distantFuture]];//暂停NSTimer
        UIAlertController *alertControl = [UIAlertController alertControllerWithTitle:@"时间到" message:nil preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *stop = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleCancel handler:nil];
         UIAlertAction *goon = [UIAlertAction actionWithTitle:@"重来" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
             //点击重来时执行的操作,重置时间,并让NSTimer恢复
            _timeValue = 5.0;
            [_timer setFireDate:[NSDate distantPast]];
        }];
        [alertControl addAction:stop];
        [alertControl addAction:goon];
        [self presentViewController:alertControl animated:YES completion:nil];//让alertControl显示出来
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

效果图是这样的


Simulator Screen Shot 2015年12月3日 下午10.09.14.png

相关文章

  • 倒计时

    新年倒计时…中考倒计时…高考倒计时…告别学生倒计时…假期倒计时…睡觉倒计时…起床倒计时… 生活中,很多与我们息息相...

  • 倒计时不仅使我们焦躁,还可以使我们振奋啊

    中考倒计时,高考倒计时, 倒计时100天 倒计时50天 倒计时10天 倒计时5天 ........ 这个场景想必在...

  • 倒计时

    文/木子 开学倒计时了 爸妈回去倒计时了 教师节倒计时了 国庆节倒计时了 小儿12岁倒计时了 寒假倒计时了 元旦倒...

  • wordlist 13

    倒计时倒计时

  • wordlist 11

    倒计时倒计时

  • van-count-down 倒计时相关问题

    1. 倒计时重置问题 背景:业务需要在开启倒计时、暂停、恢复倒计时实时判断是否展示倒计时,并重置倒计时 问题:直接...

  • iOS开发-倒计时

    倒计时 倒计时60s 倒计时HH-MM-SS 1.倒计时60s 很多时候在点击按钮发送短信的时候需要倒计时读秒 2...

  • 10.27

    倒计时第一天,倒计时7,好想赶快到倒计时1。

  • 1小时倒计时

    倒计时 目前我们内部用的比较多的倒计时:一个小时的倒计时、12个小时的倒计时。 examinePassTime:审...

  • js中date倒计时

    获取倒计时之前,我们不妨先来看怎样获取当前时间吧!!! 获取倒计时 1、实现倒计时功能2、倒计时结束按钮可被点击,...

网友评论

    本文标题:倒计时

    本文链接:https://www.haomeiwen.com/subject/knjshttx.html