倒计时的不同实现方式汇总

方法一:用NSTimer实现

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    secondsCountDown=60;
    _btn=[UIButton buttonWithType:UIButtonTypeCustom];
    self.btn.frame=CGRectMake(100, 50, 200, 30);
    self.btn.backgroundColor=[UIColor cyanColor];
    [self.btn setTitle:@"获取" forState:UIControlStateNormal];
    [self.btn addTarget:self action:@selector(btnClick) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.btn];

}
-(void)btnClick
{
     countDownTimer=[NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timeFireMethod) userInfo:nil repeats:YES];
}
-(void)timeFireMethod
{
    secondsCountDown--;
    [self.btn setTitle:[NSString stringWithFormat:@"%ld",(long)secondsCountDown] forState:UIControlStateNormal];
    if (secondsCountDown==0) {
        [countDownTimer invalidate];
        [self.btn setTitle:@"获取" forState:UIControlStateNormal];
        secondsCountDown=60;
    }
}

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

第二种方式:GCD

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    secondsCountDown=60;
    _btn=[UIButton buttonWithType:UIButtonTypeCustom];
    self.btn.frame=CGRectMake(100, 50, 200, 30);
    self.btn.backgroundColor=[UIColor cyanColor];
    [self.btn setTitle:@"获取" forState:UIControlStateNormal];
    [self.btn addTarget:self action:@selector(btnClick2) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.btn];

}
-(void)btnClick2
{
    __block int timeout=300;
    dispatch_queue_t queue=dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_source_t timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue);
    dispatch_source_set_timer(timer, dispatch_walltime(NULL, 0), 1.0 * NSEC_PER_SEC, 0);
    dispatch_source_set_event_handler(timer, ^{
        if (timeout<=0) {
            dispatch_source_cancel(timer);
            //            dispatch_release(timer);
            dispatch_async(dispatch_get_main_queue(), ^{
                
                [self.btn setTitle:@"获取" forState:UIControlStateNormal];
                
            });
        }else
        {
            int minutes=timeout/60;
            int second=timeout%60;
            NSString *strTime=[NSString stringWithFormat:[NSString stringWithFormat:@"%d分%.2d秒后获取验证码",minutes,second]];
            dispatch_async(dispatch_get_main_queue(), ^{
                
                [self.btn setTitle:strTime forState:UIControlStateNormal];
                
            });
            timeout--;
        }
    });
    dispatch_resume(timer);
}

  

原文地址:https://www.cnblogs.com/y16879w/p/6922859.html