zoukankan      html  css  js  c++  java
  • 倒计时的不同实现方式汇总

    方法一:用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);
    }
    

      

  • 相关阅读:
    大一C语言结课设计之《学生信息管理系统》
    HDU1003 Max Sum(求最大字段和)
    ORACLE AUTOMATIC STORAGE MANAGEMENT翻译-第二章ASM Instance(4)完
    System.setProperty()
    分布式存储系统sheepdog
    c语言全局变量和局部变量问题汇总
    excel VLOOKUP函数的用法
    现有一些开源ESB总线的比較
    POJ 3624 Charm Bracelet
    H.264远程视频监控系统
  • 原文地址:https://www.cnblogs.com/y16879w/p/6922859.html
Copyright © 2011-2022 走看看