简单的记录获取验证码倒计时

方法一

 __block int time = 180;
    __block UIButton *verifybutton = sender;
    verifybutton.enabled = NO;
    UILabel *abl = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, kScreen_Width, 50)];
    abl.backgroundColor = [UIColor redColor];
    [self.view addSubview:abl];
    __block UILabel *label = abl;
    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(time<=0){ //倒计时结束,关闭
            dispatch_source_cancel(_timer);
            dispatch_async(dispatch_get_main_queue(), ^{
                //设置界面的按钮显示 根据自己需求设置
                verifybutton.backgroundColor = [UIColor colorWithHexString:@"FC740A"];
                [verifybutton setTitle:@"获取验证码" forState:UIControlStateNormal];
               verifybutton.enabled = YES;
            });
            NSLog(@"关闭了");
        }else{
            dispatch_async(dispatch_get_main_queue(), ^{
                //设置界面的按钮显示 根据自己需求设置
              verifybutton.backgroundColor = [UIColor grayColor];
                NSString *strTime = [NSString stringWithFormat:@"获取验证码(%d)",time];
                label.text = strTime;
                verifybutton.titleLabel.textColor = [UIColor whiteColor];
            });
            time--;
        }
    });
    dispatch_resume(_timer);

方法二

/**
 * 开始定时器
 */
- (void)starTimer {
    countTime = 180;
    timer = [NSTimer timerWithTimeInterval:0.8 target:self selector:@selector(upTotimer) userInfo:nil repeats:YES];
    [[NSRunLoop currentRunLoop] addTimer:timer forMode:NSDefaultRunLoopMode];
}
- (void)upTotimer {
    self.countDown.alpha = 1;
    self.clickgetCode.alpha = 0;
    self.countDown.text = [NSString stringWithFormat:@" (%ld)重新获取  ", countTime];
    self.countDown.textColor = [UIColor colorWithRed:0.00 green:0.86 blue:0.08 alpha:1.00];
    if (countTime == 0) {
        [timer invalidate];
        self.countDown.alpha = 0;
        self.clickgetCode.alpha = 1;
    }
    countTime--;
}

排序数组

- (NSComparisonResult)compareAge:(Person *)person {
    if (_age < person.age) {
        return NSOrderedAscending;//升序
    } else if (_age == person.age) {
        return NSOrderedSame;//
    } else {
        return NSOrderedDescending;
    }
}
- (NSComparisonResult)compareName:(Person *)person {
    return [self.name compare:person.name];
}

推荐阅读更多精彩内容