倒计时功能的实现

来源:互联网 发布:最红网络歌曲36首 编辑:程序博客网 时间:2024/05/18 03:26

0行代码实现倒计时功能。

该demo 的实现过程如下,注意提示框使用了MBProgressHUD,在这里对作者表示感谢,需要自己导入到项目。

示例GIF:


.h 文件

#import <UIKit/UIKit.h>

@interface CCPCountDownButton :UIButton

/**验证码倒计时的时长 */

@property (nonatomic,assign)NSInteger durationOfCountDown;

//原始字体颜色

@property (nonatomic,strong)UIColor *originalColor;

//倒计时字体颜色

@property (nonatomic,strong)UIColor *processColor;

@end

-----------------------------------------------------------------

.m 文件

#import "CCPCountDownButton.h"

#import "MBProgressHUD.h"

@interfaceCCPCountDownButton ()

/** 保存起始状态下的title */

@property (nonatomic,copy)NSString *originalTitle;

/**保存倒计时的时长 */

@property (nonatomic,assign)NSInteger tempDurationOfCountDown;

/** 定时器对象 */

@property (nonatomic,strong)NSTimer *ccpCountDownTimer;

/**避免开始计时时快速连续点击显示问题 */

@property (nonatomic,assign)int count;

@end

@implementation CCPCountDownButton

/**

 *   注意事项:

 *  XIB,SB,或者是在代码中创建Button的时候,Button的样式要设置成为 Custom样式,否则在倒计时过程中 ButtonTittle会闪动.

 */

- (void)setTitle:(NSString *)title forState:(UIControlState)state {

    [super setTitle:titleforState:state];

    // 倒计时过程中title的改变不更新originalTitle

    if (self.tempDurationOfCountDown ==self.durationOfCountDown) {

        self.originalTitle = title;

        

        self.originalColor =self.titleLabel.textColor;

    }

}


- (void)setDurationOfCountDown:(NSInteger)durationOfCountDown {

    _durationOfCountDown = durationOfCountDown;

    self.tempDurationOfCountDown =_durationOfCountDown;

}


- (void)setOriginalColor:(UIColor *)originalColor {

    

    _originalColor = originalColor;

   

    [selfsetTitleColor:originalColorforState:UIControlStateNormal];

}


- (void)setProcessColor:(UIColor *)processColor {

    

    _processColor = processColor;

    

}


- (instancetype)initWithFrame:(CGRect)frame {

    if (self = [superinitWithFrame:frame]) {

        

        if (self.originalColor) {

            

            [selfsetTitleColor:self.originalColorforState:UIControlStateNormal];

        } else {

            

            //默认颜色红色

            [selfsetTitleColor:[UIColorredColor]forState:UIControlStateNormal];

        }

        

        self.count =0;

        // 设置默认的倒计时时长为60

        self.durationOfCountDown =60;

        // 设置button的默认标题为获取验证码

        [selfsetTitle:@"获取验证码"forState:UIControlStateNormal];

    }

    returnself;

}

- (instancetype)initWithCoder:(NSCoder *)aDecoder {

    if (self = [superinitWithCoder:aDecoder]) {

       

        if (self.originalColor) {

            [selfsetTitleColor:self.originalColorforState:UIControlStateNormal];

        } else {

            //默认颜色红色

            [selfsetTitleColor:[UIColorredColor]forState:UIControlStateNormal];

            

        }


        self.count =0;

        // 设置默认的倒计时时长为60

        self.durationOfCountDown =60;

        // 设置button的默认标题为获取验证码

        [selfsetTitle:@"获取验证码"forState:UIControlStateNormal];

    }

    returnself;

}


- (BOOL)beginTrackingWithTouch:(UITouch *)touch withEvent:(UIEvent *)event {

    self.count ++;

   //若正在倒计时,不响应点击事件

    if (self.tempDurationOfCountDown !=self.durationOfCountDown||self.count !=1) {

        

        self.count =0;

        [self HUD];

        return NO;

    }

   //若未开始倒计时,响应并传递点击事件,开始倒计时

    [selfstartCountDown];

    return [superbeginTrackingWithTouch:touchwithEvent:event];

}


//创建定时器,开始倒计时

- (void)startCountDown {

    

    // 创建定时器

    self.ccpCountDownTimer = [NSTimertimerWithTimeInterval:1target:selfselector:@selector(updateCCPCountDownButtonTitle)userInfo:nilrepeats:YES];

   //将定时器添加到当前的RunLoop中(自动开启定时器)

    [[NSRunLoopcurrentRunLoop]addTimer:self.ccpCountDownTimerforMode:NSRunLoopCommonModes];

}

//更新CCPCountDownButtontitle为倒计时剩余的时间

- (void)updateCCPCountDownButtonTitle {

    if (self.tempDurationOfCountDown ==0) {

        // 设置CCPCountDownButtontitle为开始倒计时前的title

        [selfsetTitle:self.originalTitleforState:UIControlStateNormal];

        [selfsetTitleColor:self.originalColorforState:UIControlStateNormal];

        // 恢复CCPCountDownButton开始倒计时

        self.tempDurationOfCountDown =self.durationOfCountDown;

        [self.ccpCountDownTimerinvalidate];

        self.count =0;

    } else {

        // 设置CCPCountDownButtontitle为当前倒计时剩余的时间

        [selfsetTitle:[NSStringstringWithFormat:@"重新发送(%zds)",self.tempDurationOfCountDown--]forState:UIControlStateNormal];

        

        

        if (self.processColor) {

            

            [selfsetTitleColor:self.processColorforState:UIControlStateNormal];

            

        } else {

            //默认颜色蓝色

            [selfsetTitleColor:[UIColorblueColor]forState:UIControlStateNormal];


        }

    }

}


- (void)HUD {

    MBProgressHUD *hud = [MBProgressHUDshowHUDAddedTo:self.superview.windowanimated:YES];

    // Set the annular determinate mode to show task progress.

    hud.mode =MBProgressHUDModeText;

    hud.label.text = [NSStringstringWithFormat:@",%lds之内请勿重复操作",(long)self.durationOfCountDown];

//    NSLocalizedString(@",60s之内请勿重复操作", @"HUD message title");

    // Move to bottm center.

    hud.offset =CGPointMake(0.f,MBProgressMaxOffset);

    [hud hideAnimated:YESafterDelay:2.f];

}


- (void)dealloc {

    

    [self.ccpCountDownTimerinvalidate];

}

@end


使用详情见DEMO。
DEMO地址:https://github.com/IMCCP/CCPCountdownDemo

如果该demo对你有帮助,请 Star 一下吧!


0 0
原创粉丝点击