美文网首页
iOS 倒计时 读秒定时器

iOS 倒计时 读秒定时器

作者: Mi_Manchi_ | 来源:发表于2022-08-04 16:13 被阅读0次
// 开启倒计时效果
-(void)openCountdown{
    __block NSInteger time = 59; //倒计时时间
    
    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(), ^{
                //设置按钮的样式
                [self.getCodeBtn setTitle:@"重新发送" forState:UIControlStateNormal];
                [self.getCodeBtn setTitleColor:COLOR_INPUTING forState:UIControlStateNormal];
                self.getCodeBtn.userInteractionEnabled = YES;
            });
        }else{
            
            int seconds = time % 60;
            dispatch_async(dispatch_get_main_queue(), ^{
                //设置按钮显示读秒效果
                [self.getCodeBtn setTitle:[NSString stringWithFormat:@"%.2d秒后可重发", seconds] forState:UIControlStateNormal];
                [self.getCodeBtn setTitleColor:COLOR(204) forState:UIControlStateNormal];
                self.getCodeBtn.userInteractionEnabled = NO;
            });
            time--;
        }
    });
    dispatch_resume(_timer);
}

相关文章

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

    级别: ★★☆☆☆标签:「iOS 验证码后台倒计时」「NSTimer后台运行」「iOS 定时器后台运行」作者: ...

  • GCD Timer

    倒计时 定时器 timer 循环执行定时器

  • 读秒倒计时

    前些日子,项目中用到一个倒计时读秒的功能。其实实现起来很简单,就是通过改变 label 的transform。经过...

  • 无标题文章

    iOS NSTimer使用详解-开启、关闭、移除 定时器定时器详解ios定时器关闭定时器NSTimer 1、要使用...

  • 安卓快速开发框架(十)XBaseAndroid倒计时,定时器

    倒计时 定时器

  • 定时器

    定时器弹窗 定时器基本用法 定时器动画 时钟 倒计时 变量的作用域

  • js定时器

    定时器弹窗 定时器的基本用法 定时器动画 时钟 倒计时 变量的作用域

  • iOS开发-倒计时

    倒计时 倒计时60s 倒计时HH-MM-SS 1.倒计时60s 很多时候在点击按钮发送短信的时候需要倒计时读秒 2...

  • 定时器

    1定时器 写法: 倒计时:

  • iOS自定义按钮 - 发送验证码按钮

    效果图 倒计时定时器 使用的是GCD定时器 -- GCD定时器 防止恶意点击 其中设置了倒计时时间记录,无论是p...

网友评论

      本文标题:iOS 倒计时 读秒定时器

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