iOS中倒计时按钮的实现

作者: MooneyWang | 来源:发表于2016-06-22 16:13 被阅读2259次

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

countdown.gif

如图我使用了两种方法实现,一种是利用分类,另一种是利用继承。这边我只讲解继承,分类的核心代码基本同继承。如果你们想要直接看代码的话,请点击 这里

创建一个MJCountDownButton类继承自UIButton,.h中的代码如下


#import <UIKit/UIKit.h>

@class MJCountDownButton;

typedef void(^Completion)(MJCountDownButton *countDownButton);

@interface MJCountDownButton : UIButton

/**
 *  @author 王梦杰, 16-06-22 14:06:00
 *
 *  开始倒计时
 *
 *  @param startTime  倒计时时间
 *  @param unitTitle  倒计时时间单位(如:s)
 *  @param completion 倒计时结束执行的Block
 */
- (void)countDownFromTime:(NSInteger)startTime unitTitle:(NSString *)unitTitle completion:(Completion)completion;

@end

.m中的代码如下:

#import "MJCountDownButton.h"
#import "UIImage+Color.h"

@implementation MJCountDownButton

- (void)countDownFromTime:(NSInteger)startTime unitTitle:(NSString *)unitTitle completion:(Completion)completion {
    __weak typeof(self) weakSelf = self;
    // 剩余的时间(必须用__block修饰,以便在block中使用)
    __block NSInteger remainTime = startTime;
    // 获取全局队列
    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);
    // 每隔1s钟执行一次
    dispatch_source_set_timer(timer, DISPATCH_TIME_NOW, 1.0 * NSEC_PER_SEC, 0 * NSEC_PER_SEC);
    // 在queue中执行event_handler事件
    dispatch_source_set_event_handler(timer, ^{
        if (remainTime <= 0) { // 倒计时结束
            dispatch_source_cancel(timer);
            // 回到主线程
            dispatch_async(dispatch_get_main_queue(), ^{
                weakSelf.enabled = YES;
                completion(weakSelf);
            });
        } else {
            NSString *timeStr = [NSString stringWithFormat:@"%ld", remainTime];
            // 回到主线程更新UI
            dispatch_async(dispatch_get_main_queue(), ^{
                [weakSelf setTitle:[NSString stringWithFormat:@"%@%@",timeStr,unitTitle] forState:UIControlStateDisabled];
                [weakSelf setBackgroundImage:[UIImage createImageWithColor:[UIColor lightGrayColor]] forState:UIControlStateDisabled];
                weakSelf.enabled = NO;
            });
            remainTime--;
        }
    });
    dispatch_resume(timer);
}

@end

这边还利用到一个UIImge的分类方法,该分类的作用是通过一个颜色对象返回一张图片对象,这个比较实用。

+ (UIImage *)createImageWithColor:(UIColor *)color {
    // 画布大小
    CGRect rect = CGRectMake(0.0f, 0.0f, 1.0f, 1.0f);
    // 在当前画布上开启绘图上下文
    UIGraphicsBeginImageContext(rect.size);
    // 画笔
    CGContextRef context = UIGraphicsGetCurrentContext();
    // 设置画笔颜色
    CGContextSetFillColorWithColor(context, [color CGColor]);
    // 填充画布
    CGContextFillRect(context, rect);
    // 取得画布中的图片
    UIImage *theImage = UIGraphicsGetImageFromCurrentImageContext();
    // 结束绘图上下文
    UIGraphicsEndImageContext();
    return theImage;
}

文末再次附上源码地址:去下载
顺手点个星的都是好同志。

相关文章

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

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

  • 倒计时

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

  • iOS中倒计时按钮的实现

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

  • RAC 实现按钮倒计时

    RAC 实现按钮倒计时

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

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

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

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

  • 按钮倒计时的实现 iOS

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

  • 倒计时

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

  • 倒计时按钮

    给按钮加倒计时的代码 1、声明属性 2、在viewDidLoad中设置按钮初状态 3、在按钮触发方法中触发倒计时计...

  • 自定义TabBar

    基本自带TabBar来实现自定义 在iOS原生的tabBar中,能够实现按钮的点击事件,能够实现视图控制器的切换等...

网友评论

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

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