您的位置:首页 > 产品设计 > UI/UE

iOS UIButton倒计时方法——MZTimerLabel

2014-10-27 00:00 656 查看
先前发了一个关于dispatch方法来进行倒计时的,不过相对来说MZTimerLabel更好用一点。现在说一下MZTimerLabel的使用方法。

首先,因为这是一个库函数,所以必须要有这个库了,xcode不带这个库,使用前请先下载该库。

MZTimerLabel

下载下来之后先把两个头文件加载到工程里面





然后在需要用倒计时的函数里导入MZTimerLabel的库,并且设置代理,因为后面如果倒计时结束后要自动调用事件的话会调用MZTimerLabel的代理方法





把要倒计时的UIButton和storyboard的控件相连





现在来上一下倒计时函数的代码
@interface RegisterViewController ()<MZTimerLabelDelegate>
{
    UILabel *timer_show;//倒计时label
}

- (void)timeCount{//倒计时函数
    [_dynamicCode_btn setTitle:nil forState:UIControlStateNormal];//把按钮原先的名字消掉
    timer_show = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 85, 30)];//UILabel设置成和UIButton一样的尺寸和位置
    [_dynamicCode_btn addSubview:timer_show];//把timer_show添加到_dynamicCode_btn按钮上
    MZTimerLabel *timer_cutDown = [[MZTimerLabel alloc] initWithLabel:timer_show andTimerType:MZTimerLabelTypeTimer];//创建MZTimerLabel类的对象timer_cutDown
    [timer_cutDown setCountDownTime:60];//倒计时时间60s
    timer_cutDown.timeFormat = @"倒计时 ss";//倒计时格式,也可以是@"HH:mm:ss SS",时,分,秒,毫秒;想用哪个就写哪个
    timer_cutDown.timeLabel.textColor = [UIColor whiteColor];//倒计时字体颜色
    timer_cutDown.timeLabel.font = [UIFont systemFontOfSize:13.0];//倒计时字体大小
    timer_cutDown.timeLabel.textAlignment = NSTextAlignmentCenter;//剧中
    timer_cutDown.delegate = self;//设置代理,以便后面倒计时结束时调用代理
    _dynamicCode_btn.userInteractionEnabled = NO;//按钮禁止点击
    [timer_cutDown start];//开始计时
}

倒计时结束后自动调用代理方法

//倒计时结束后的代理方法
- (void)timerLabel:(MZTimerLabel *)timerLabel finshedCountDownTimerWithTime:(NSTimeInterval)countTime{
    [_dynamicCode_btn setTitle:@"发送验证码" forState:UIControlStateNormal];//倒计时结束后按钮名称改为"发送验证码"
    [timer_show removeFromSuperview];//移除倒计时模块
    _dynamicCode_btn.userInteractionEnabled = YES;//按钮可以点击
}

觉得还不错,可以调整的方法比较多,拓展性比较强。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: