博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
NSTimer定时器的用法
阅读量:7014 次
发布时间:2019-06-28

本文共 1498 字,大约阅读时间需要 4 分钟。

#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];    }

转载于:https://www.cnblogs.com/thbbsky/p/4250117.html

你可能感兴趣的文章