zoukankan      html  css  js  c++  java
  • NSTimer定时器的用法

    #import "ViewController.h"
    
    @interface ViewController ()
    {
        NSTimer *countDownTimer;
        int countDownTime;
    }
    @property (weak, nonatomic) IBOutlet UIButton *codeBtn;
    
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        // Do any additional setup after loading the view, typically from a nib.
    }
    
    - (void)didReceiveMemoryWarning {
        [super didReceiveMemoryWarning];
        // Dispose of any resources that can be recreated.
    }
    - (IBAction)getCodeBtnClick:(UIButton *)sender {
        
        [_codeBtn setEnabled:NO];
        if (countDownTimer)
        {
            [countDownTimer invalidate];
            countDownTimer=nil;
        }
        countDownTime=120;
        countDownTimer=[NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(countDown) userInfo:nil repeats:YES];
        
    }
    -(void)countDown
    {
        countDownTime--;
        if (countDownTime==0)
        {
            [countDownTimer invalidate];
            countDownTimer=nil;
            [_codeBtn setEnabled:YES];
            [_codeBtn setTitle:@"获取验证码" forState:UIControlStateNormal];
            return;
        }
        NSString *count=[NSString stringWithFormat:@"%d",countDownTime];
        NSMutableAttributedString *attributeString=[[NSMutableAttributedString alloc]initWithString:[NSString stringWithFormat:@"剩余%@秒",count]];
        [attributeString addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(2,count.length)];
        [_codeBtn setAttributedTitle:attributeString forState:UIControlStateDisabled];
        
    }

  • 相关阅读:
    js01
    js---18miniJquery
    js---17继承中方法属性的重写
    js---16继承
    js---16原型链
    js---15深拷贝浅拷贝 原型链
    js---14公有私有成员方法
    js---13 this call apply
    js---12对象创建方式,构造器,原型
    ESXi导出的CentOS7 ovf文件导入到workstation 无法打开GUI登录界面的问题解决方案
  • 原文地址:https://www.cnblogs.com/thbbsky/p/4250117.html
Copyright © 2011-2022 走看看