码迷,mamicode.com
首页 > 其他好文 > 详细

GCD 定时器操作

时间:2016-06-26 00:32:12      阅读:146      评论:0      收藏:0      [点我收藏+]

标签:

var seconds = 10 //倒计时时间
        let queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)
        let timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0,queue);
        dispatch_source_set_timer(timer,dispatch_walltime(nil, 0),1 * NSEC_PER_SEC, 0); //每秒执行
        dispatch_source_set_event_handler(timer) { 
            if(seconds<=0){ //倒计时结束,关闭
                dispatch_source_cancel(timer);
                dispatch_async(dispatch_get_main_queue(), {
                    //设置界面的按钮显示 根据自己需求设置
                    btn.setTitleColor(UIColor.blackColor(), forState:.Normal)
                    btn.setTitle("获取验证码", forState:.Normal)
                    btn.titleLabel?.font = defaultFont14
                    btn.userInteractionEnabled = true
                    });
            }else{
                
                dispatch_async(dispatch_get_main_queue(), {
                    UIView.beginAnimations(nil, context: nil)
                    UIView.setAnimationDuration(1)
                })
                dispatch_async(dispatch_get_main_queue(), {
                    //设置界面的按钮显示 根据自己需求设置
                    UIView.beginAnimations(nil, context: nil)
                    UIView.setAnimationDuration(1)
                    btn.setTitleColor(UIColor.orangeColor(), forState:.Normal)
                    btn.setTitle("\(seconds)秒后重新发送", forState:.Normal)
                    btn.titleLabel?.font = UIFont.systemFontOfSize(11)
                    UIView.commitAnimations()
                    btn.userInteractionEnabled = false
                
                })
               seconds -= 1

        }
            
    }
    dispatch_resume(timer)

 10秒定时器。整理备忘

GCD 定时器操作

标签:

原文地址:http://www.cnblogs.com/alexmercer/p/5617110.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!