倒计时

本文介绍了一种在iOS应用中实现倒计时功能的方法,通过使用GCD(Grand Central Dispatch)创建定时器来更新按钮文本和标签内容,支持不同时间间隔的倒计时场景。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

+ (void)countDownWithBtn:(UIButton *)btn{
    
    __block  NSInteger _surplusSecond = 59;
     dispatch_queue_t globalQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
     dispatch_queue_t mainQueue = dispatch_get_main_queue();
     dispatch_source_t timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, globalQueue);
     dispatch_source_set_timer(timer, DISPATCH_TIME_NOW, 1.0 * NSEC_PER_SEC, 0.0 * NSEC_PER_SEC);
     dispatch_source_set_event_handler(timer, ^{
        if (_surplusSecond <=0) {
            dispatch_source_cancel(timer);
            dispatch_async(mainQueue, ^{
                btn.enabled = YES;
                btn.titleLabel.text = @"获取验证码";
                [btn setTitle:@"获取验证码" forState:UIControlStateNormal];
                _surplusSecond = 59;
                                            });
        }else{
             _surplusSecond--;
            
            dispatch_async(mainQueue, ^{
                NSString *btnInfo = [NSString stringWithFormat:@"%ldS", (long)(_surplusSecond + 1)];
                btn.titleLabel.text = btnInfo;
            [btn setTitle:btnInfo forState:UIControlStateNormal];
                btn.enabled = NO;
               
                                                });
            

        }
    });

    dispatch_source_set_cancel_handler(timer, ^{ //计时器取消处理器;调用 dispatch_source_cancel 时执行
                 NSLog(@"Cancel Handler");
             });
     dispatch_resume(timer);
}

+ (void)countDownWithLabel:(QFAttributedLabel *)label andTimestamp:(NSString *)time {
    __block  NSInteger _surplusSecond = [time integerValue]/1000;
//    __block  NSInteger _surplusSecond = 3600;
    dispatch_queue_t globalQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_queue_t mainQueue = dispatch_get_main_queue();
    dispatch_source_t timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, globalQueue);
    dispatch_source_set_timer(timer, DISPATCH_TIME_NOW, 60.0 * NSEC_PER_SEC, 0.0 * NSEC_PER_SEC);
    dispatch_source_set_event_handler(timer, ^{
        if (_surplusSecond <=0) {
            
            dispatch_source_cancel(timer);
            dispatch_async(mainQueue, ^{
                [[NSNotificationCenter defaultCenter] postNotificationName:@"countDown" object:self userInfo:nil];
            });
        }else{
            _surplusSecond = _surplusSecond-60;
            
            dispatch_async(mainQueue, ^{
                
                NSString *day = [NSString stringWithFormat:@"%ld", _surplusSecond/(3600*24)];
                NSString *hour = [NSString stringWithFormat:@"%ld", _surplusSecond%(3600*24)/3600];
                NSString *minu = [NSString stringWithFormat:@"%ld", _surplusSecond%(3600*24)%3600/60];
                
                [label setText:[NSString stringWithFormat:@"%@天%@小时%@分",day, hour, minu] WithFont:[UIFont fontWithName:@"" size:12] AndColor:orangeColor];
                [label setKeyWordTextArray:@[@"天", @"小时", @"分"] WithFont:[UIFont systemFontOfSize:10] AndColor:UIColorFromRGB(0x19191a)];
                
            });
            
            
        }
    });
    
    dispatch_source_set_cancel_handler(timer, ^{ //计时器取消处理器;调用 dispatch_source_cancel 时执行
        NSLog(@"Cancel Handler");
    });
    dispatch_resume(timer);
}

 

转载于:https://my.oschina.net/u/2519763/blog/742387

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值