倒计时实现两种方法-NSTimer/GCD

#import "ViewController.h"

@interface ViewController ()
@property (nonatomic ,strong)UIButton *btn;
@property (nonatomic ,assign)NSInteger secondsCountDown;
@property (nonatomic ,strong)NSTimer *countDownTimer;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.btn = [UIButton buttonWithType:UIButtonTypeCustom];
    self.btn.frame = CGRectMake(20, 100, 300, 100);
    self.btn.backgroundColor = [UIColor yellowColor];
    [self.btn setTitle:@"获取验证码" forState:UIControlStateNormal];
    
    //设置文字颜色
    [self.btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    
    //添加点击事件
    [self.btn addTarget:self action:@selector(startTimeGCD) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.btn];
}

1、NSTimer

//使用NSTimer实现倒计时功能
- (void)startTime {
    //设置倒计时总时长
    self.secondsCountDown = 5;
    self.countDownTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(startTimeNSTimer) userInfo:nil repeats:YES];
    [self.btn setTitle:[NSString stringWithFormat:@"%ld秒后重新发送",(long)self.secondsCountDown] forState:UIControlStateNormal];
}

//使用NSTimer实现倒计时
- (void)startTimeNSTimer {
    self.secondsCountDown -- ;
    self.btn.userInteractionEnabled = NO;
    [self.btn setTitle:[NSString stringWithFormat:@"%ld秒后重新发送",(long)self.secondsCountDown] forState:UIControlStateNormal];
    if (self.secondsCountDown == 0) {
        [self.countDownTimer invalidate];
        self.btn.userInteractionEnabled = YES;
        [self.btn setTitle:@"重新发送验证码" forState:UIControlStateNormal];
    }
}

2、GCD

//使用GCD实现倒计时
- (void)startTimeGCD {
    //在block内部不可以修改外部变量,需要添加__block进行修饰
    //设置倒计时总时长
    __block int timeout = 10;
    //创建队列(全局并发队列)
    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);
            //回到主线程更新UI
            dispatch_async(dispatch_get_main_queue(), ^{
                //设置界面的按钮显示 根据自己需求设置
                [self.btn setTitle:@"发送验证码" forState:UIControlStateNormal];
                self.btn.userInteractionEnabled = YES;
            });
        }else{
            int seconds = timeout % 60;
            NSString *strTime = [NSString stringWithFormat:@"%.2d", seconds];
            dispatch_async(dispatch_get_main_queue(), ^{
                //设置界面的按钮显示 根据自己需求设置
                //NSLog(@"____%@",strTime);
                //                [UIView beginAnimations:nil context:nil];
                //                [UIView setAnimationDuration:1];
                [self.btn setTitle:[NSString stringWithFormat:@"%@秒后重新发送",strTime] forState:UIControlStateNormal];
                //                [UIView commitAnimations];
                self.btn.userInteractionEnabled = NO;
            });
            timeout--;
        }
    });
    dispatch_resume(_timer);
}

 

posted @ 2017-03-13 15:59  FMDN  阅读(1221)  评论(0编辑  收藏  举报