美文网首页
iOS 按钮倒计时

iOS 按钮倒计时

作者: sj1910 | 来源:发表于2019-10-15 11:03 被阅读0次

使用倒计时获取验证码是app中最长遇到的,常用的方法有三种

屏幕快照 2019-10-15 上午10.40.26.png 屏幕快照 2019-10-15 上午10.40.35.png
NSTimer
[NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(handleTimer) userInfo:nil repeats:YES];
Thread
- (void)firstBtnAction:(UIButton  *)sender {
    //创建一个后台线程执行计时操作
    [self performSelectorInBackground:@selector(timerThread) withObject:nil];
}

- (void)timerThread {
    for (int i = TIMECOUNT; i >= 0 ; i--) {
        self.count--;
        //切换到主线程中更新UI
        [self performSelectorOnMainThread:@selector(updateFirstBtn) withObject:nil waitUntilDone:YES];
        sleep(1);
    }
}
GCD(我使用的)

创建一个UIButton的分类

@interface UIButton (countDown)

/**
 *  倒计时按钮
 *
 *  @param timeLine 倒计时总时间
 *  @param title    还没倒计时的title
 *  @param subTitle 倒计时中的子名字,如时、分
 */
- (void)startWithTime:(NSInteger)timeLine title:(NSString *)title countDownTitle:(NSString *)subTitle mainColor:(unsigned int)mainHex countColor:(unsigned int)countHex;

@end
#import "UIButton+countDown.h"

@implementation UIButton (countDown)

- (void)startWithTime:(NSInteger)timeLine title:(NSString *)title countDownTitle:(NSString *)subTitle mainColor:(unsigned int)mainHex countColor:(unsigned int)countHex{
    
    __weak typeof(self) weakSelf = self;
    //倒计时时间
    __block NSInteger timeOut = timeLine;
    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_async(dispatch_get_main_queue(), ^{
//                [weakSelf setBackgroundColor:RGBOF(mainHex)];
                [weakSelf setTitle:title forState:UIControlStateNormal];
                [weakSelf setTitleColor:kBaseBlueColor forState:(UIControlStateNormal)];
                weakSelf.userInteractionEnabled = YES;
            });
        } else {
            int allTime = (int)timeLine + 1;
            int seconds = timeOut % allTime;
            NSString *timeStr = [NSString stringWithFormat:@"%d", seconds];
            dispatch_async(dispatch_get_main_queue(), ^{
//                [weakSelf setBackgroundColor:RGBOF(countHex)];
                [weakSelf setTitle:[NSString stringWithFormat:@"%@%@",timeStr,subTitle] forState:UIControlStateNormal];
                [weakSelf setTitleColor:[UIColor grayColor] forState:(UIControlStateNormal)];
                weakSelf.userInteractionEnabled = NO;
            });
            timeOut--;
        }
    });
    dispatch_resume(_timer);
}

@end

相关文章

  • iOS快速实现一个保存记录的倒计时按钮

    iOS开发中在登录、注册、找回密码等页面经常需要实现倒计时按钮,但是很多情况下用户点击倒计时按钮开始倒计时之后,再...

  • iOS UIButton倒计时、指示器、粒子效果

    前言 分享三款按钮来使用, 倒计时按钮,指示器按钮,点赞粒子效果按钮 倒计时按钮 Property & API 简...

  • iOS 短信验证码倒计时按钮的实现

    个人博客: LiCheng的博客 引言: 验证码倒计时按钮的应用是非常普遍的,本文介绍了IOS实现验证码倒计时功能...

  • iOS按钮倒计时

    创建一个UIButton的类别,其他控件也可以使用这个方法:

  • iOS 倒计时按钮

    如果有帮助到你就给个赞吧 谢谢咯...添加到 单利类 或者button分类理都可以 返回之后再重新进来按钮倒计...

  • iOS 按钮倒计时

    使用倒计时获取验证码是app中最长遇到的,常用的方法有三种 NSTimer Thread GCD(我使用的) 创建...

  • iOS 短信验证码倒计时按钮的实现

    验证码倒计时按钮的应用是非常普遍的,本文介绍了IOS实现验证码倒计时功能,点击获取验证码,进入时间倒计时,感兴趣的...

  • 倒计时

    ios怎么在cell上添加倒计时 iOS中 简单易懂的秒杀倒计时/倒计时 iOS开发-三种倒计时的写法 iOS实现...

  • iOS获取倒计时按钮

    //验证码时间 @property (nonatomic,strong)NSTimer *timer; //开始计...

  • iOS开发倒计时按钮

    这里做一下记录 //倒计时时间 - 60S __blockNSIntegertimeOut =59; ...

网友评论

      本文标题:iOS 按钮倒计时

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