美文网首页iOS开发相关
按钮倒计时的实现 iOS

按钮倒计时的实现 iOS

作者: LOVE婷 | 来源:发表于2017-09-12 10:33 被阅读0次

很多项目中,都会涉及到倒计时的功能, 发送完验证码开始倒计时,针对这个我个人封装了一个简单的方法来实现此功能, 话不多说,上干货!

首先我是在UIButton的一个延展类里面添加了一个方法 

/**

*  倒计时按钮

*

*  @param timeLine 倒计时总时间

*/

- (void)startCountDownWithTime:(NSInteger)timeLine;

然后在.m中实现

- (void)startCountDownWithTime:(NSInteger)timeLine{

NSString *subTitle = @"重新发送";

UIColor *defultColor = UGUColorFromRGB_0x(0xed145b);

UIColor *runningColor = UGUColorFromRGB_0x(0xd5d5d5);

//倒计时时间

__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(), ^{

[self setTitle:subTitle forState:UIControlStateNormal];

self.userInteractionEnabled = YES;

self.layer.borderColor = [defultColor CGColor];

[self setTitleColor:defultColor forState:UIControlStateNormal];

});

} else {

self.userInteractionEnabled = YES;

int allTime = (int)timeLine + 1;

int seconds = timeOut % allTime;

NSString *timeStr = [NSString stringWithFormat:@"%0.1d", seconds];

dispatch_async(dispatch_get_main_queue(), ^{

[self setTitle:[NSString stringWithFormat:@"%@%@",timeStr,@"s"] forState:UIControlStateNormal];

self.layer.borderColor = [runningColor CGColor];

[self setTitleColor:runningColor forState:UIControlStateNormal];

self.userInteractionEnabled = NO;

});

timeOut--;

}

});

dispatch_resume(_timer);

}

以上就是全部代码,怎么样 ,是不是感觉很简单呢?

希望可以帮到大家 ~~~~

相关文章

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

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

  • RAC 实现按钮倒计时

    RAC 实现按钮倒计时

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

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

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

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

  • 按钮倒计时的实现 iOS

    很多项目中,都会涉及到倒计时的功能, 发送完验证码开始倒计时,针对这个我个人封装了一个简单的方法来实现此功能, 话...

  • 倒计时

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

  • 倒计时

    Requirements 实现按钮倒计时: 基于GCD的倒计时 github链接:https://github.c...

  • iOS中倒计时按钮的实现

    我们经常会遇到按钮上进行倒计时的情况,比如获取验证码的时候。本文将讲解如何实现倒计时按钮。为了让大家清楚地知道本文...

  • 商品促销倒计时效果实现

    效果图展示 所有的知识点总结如下: 轮播图实现:swiper插件 倒计时功能的实现 倒计时结束,抢购按钮变为可点击...

  • js中date倒计时

    获取倒计时之前,我们不妨先来看怎样获取当前时间吧!!! 获取倒计时 1、实现倒计时功能2、倒计时结束按钮可被点击,...

网友评论

    本文标题:按钮倒计时的实现 iOS

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