zoukankan      html  css  js  c++  java
  • 倒计时实现两种方法-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);
    }
  • 相关阅读:
    数据结构与算法----->算法----->高级排序算法:基数排序
    数据结构与算法----->算法----->高级排序算法:希尔排序
    数据结构与算法----->算法----->高级排序算法:快速排序
    数据结构与算法----->算法----->简单排序算法:冒泡、选择、插入排序
    数据结构与算法----->数据结构----->图
    数据结构与算法----->数据结构----->红-黑树
    数据结构与算法----->数据结构----->2-3-4树以及外部存储
    数据结构与算法----->数据结构----->二叉树
    数据结构与算法----->数据结构----->堆
    myeclipse2014配置多个同版本的Tomcat
  • 原文地址:https://www.cnblogs.com/fengmin/p/6543289.html
Copyright © 2011-2022 走看看