gcd倒计时

@interface ViewController ()
{
    dispatch_source_t _timer;
}
@property (weak, nonatomic) IBOutlet UILabel *timeLab;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)alertBtnClick:(id)sender {

    UIButton *_codeBtn =(UIButton *)sender;

    __block int timeout=59; //倒计时时间
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    //typedispatch源可处理的事件
    _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(), ^{
                //设置界面的按钮显示 根据自己需求设置
                _codeBtn.hidden = NO;
                _timeLab.hidden = YES;
                [_codeBtn setTitle:@"获取验证码" forState:UIControlStateNormal];
            });
        }else{
            int seconds = timeout % 60;
            NSString *strTime = [NSString stringWithFormat:@"%.2d", seconds];
            dispatch_async(dispatch_get_main_queue(), ^{
                //设置界面的按钮显示 根据自己需求设置
                //NSLog(@"____%@",strTime);
                _timeLab.hidden = NO;
                _codeBtn.hidden = YES;
                _timeLab.text = [NSString stringWithFormat:@"%@秒后重新获取",strTime];
            });
            timeout--;
        }
    });
    //立即执行
    dispatch_resume(_timer);

}
-(void)viewWillDisappear:(BOOL)animated{

    [super viewWillDisappear:animated];
    if(_timer){
        dispatch_source_cancel(_timer);
    }
}
时间: 2024-10-11 00:57:15

gcd倒计时的相关文章

GCD 倒计时

今天在Code4App上看了一个GCD倒计时的Demo,觉得不错代码贴出来备用 -(void)startTime{ __block int timeout = 30; //倒计时时间 dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0); dispatch_source_t _timer = dispatch_source_create(DISPATCH_SOURCE_TYPE

iOS GCD倒计时

GCD倒计时的好处在于不用考虑是否定时器无法释放的问题,runloop的问题,还有精度更加高 使用GCD创建定时器方法 -(void)startCountDown:(NSInteger)maxTime{ __block int time = 0; dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0); dispatch_source_t timer = dispatch_sou

App 启动加载广告页面思路

需求 很多app(如淘宝.美团等)在启动图加载完毕后,还会显示几秒的广告,一般都有个跳过按钮可以跳过这个广告,有的app在点击广告页之后还会进入一个广告页面,点击返回进入首页.今天我们就来开发一个广告页面,效果如下. 思路 1.广告页加载思路.广告页的内容要实时显示,在无网络状态或者网速缓慢的情况下不能延迟加载,或者等到首页出现了再加载广告页.所以这里我不采用网络请求广告接口获取图片地址,然后加载图片的方式,而是先将图片异步下载到本地,并保存图片名,每次打开app时先根据本地存储的图片名查找沙盒

iOS技术博客(文摘)链接地址

  objc系列译文(5.1):认识 TextKit 手把手教你配置苹果APNS推送服务 如何使用iOS Addressbook UIApplication深入研究 GCD倒计时 那些不能错过的Xcode插件 iOS开发系列--音频播放.录音.视频播放.拍照.视频录制 Mantle 初步使用 CABasicAnimation animationWithKeyPath Types 插件管理Alcatraz

使用 GCD 实现倒计时效果

效果如下: ViewController.h 1 #import <UIKit/UIKit.h> 2 3 @interface ViewController : UIViewController 4 @property (assign, nonatomic) NSInteger surplusSecond; 5 6 @property (strong, nonatomic) IBOutlet UILabel *lblMessage; 7 @property (strong, nonatomic

使用GCD创建倒计时

__block int timeout=300; //倒计时时间 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,dispa

ios 使用gcd 显示倒计时

__block int timeout = 60;//倒计时时间 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, dispa

GCD实现验证码倒计时

1 - (void)viewDidLoad { 2 [super viewDidLoad]; 3 [_timeButton addTarget:self action:@selector(startTime) forControlEvents:UIControlEventTouchUpInside]; 4 } 5 -(void)startTime{ 6 __block int timeout=30; //倒计时时间 7 dispatch_queue_t queue = dispatch_get_

使用GCD验证码倒计时

__block int timeout = 60; dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0); dispatch_source_t source_t = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue); dispatch_source_set_timer(source_t, dis