NSTimer

作者: L柠_檬 | 来源:发表于2016-08-19 11:46 被阅读15次
目录
    1.1 时钟
    1.2 倒计时60秒
#import "ViewController.h"

@interface ViewController ()

@property (nonatomic ,strong)UILabel *timelabel;
@property (nonatomic ,strong)NSString *Str;
@end

@implementation ViewController

- (void)viewDidLoad
{
    
    [super viewDidLoad];
    
    self.timelabel=[[UILabel alloc]initWithFrame:CGRectMake(20, 100, 300, 30)];
    
    NSDateFormatter *dateFormatter=[[NSDateFormatter alloc]init];
    
    [dateFormatter setDateFormat:@"YYYY-MM-dd hh-mm-ss"];
    
    self.Str=[dateFormatter stringFromDate:[NSDate date]];
    
    self.timelabel.text=self.Str;
    
    [self.view addSubview:self.timelabel];
    
    [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timeAdd) userInfo:self repeats:YES];
    
    
}

- (void)timeAdd
{
    //NSDateFormatter 需要封装单例,每次初始化非常耗时
    NSDateFormatter *dateFormatter=[[NSDateFormatter alloc]init];
    [dateFormatter setDateFormat:@"YYYY-MM-dd hh-mm-ss"];
    self.Str=[dateFormatter stringFromDate:[NSDate date]];
    self.timelabel.text=self.Str;
}

1.2 倒计时60秒

#import "ViewController.h"

@interface ViewController ()

@property (nonatomic , strong) UIButton *authButton;

@property (nonatomic , assign) int num;

@end


@implementation ViewController


- (void)viewDidLoad {
    
    [super viewDidLoad];
    
    self.authButton=[UIButton buttonWithType:UIButtonTypeCustom];
    self.authButton.frame =CGRectMake(220, 80, 90, 40);
    [self.authButton setBackgroundColor:[UIColor orangeColor]];
    [self.authButton setTitle:@"获取验证码" forState:UIControlStateNormal];
    self.authButton.titleLabel.textAlignment = NSTextAlignmentCenter;
    [self.authButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    self.authButton.titleLabel.font =[UIFont systemFontOfSize:15];
    [self.authButton addTarget:self action:@selector(authBtnDown:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.authButton];
    
}


- (void)authBtnDown:(UIButton *)button
{
    //不能直接调用FirstThread
    [self performSelectorInBackground:@selector(FirstThread) withObject:nil];
    
}


- (void)FirstThread
{
    
    if (![NSThread isMainThread]) {
        
        self.num=59;
        NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timeChange:)
                                                        userInfo:nil repeats:YES];
        self.authButton.enabled=NO;
        [self.authButton setBackgroundColor:[UIColor grayColor]];
        
        [[NSRunLoop currentRunLoop]addTimer:timer forMode:NSDefaultRunLoopMode];
        
        [[NSRunLoop currentRunLoop]run];
        
    }
    
}


- (void)timeChange:(NSTimer *)tim
{
    
    if (self.num > 0) {
        
        self.authButton.titleLabel.text = [NSString stringWithFormat:@"%d",self.num];
        
        self.num = self.num - 1;
        
    }else{
        
        [tim invalidate];
        
        tim=nil;
        
        self.authButton.enabled = YES;
        
        [self.authButton setBackgroundColor:[UIColor orangeColor]];
        
    }
    
}

相关文章

网友评论

      本文标题:NSTimer

      本文链接:https://www.haomeiwen.com/subject/abjbsttx.html