Giter Site home page Giter Site logo

smilezxlee / zxcountdownview Goto Github PK

View Code? Open in Web Editor NEW
58.0 3.0 14.0 3.11 MB

【杀不死的倒计时】一个简单易用的倒计时View,常用于快速创建点击获取验证码按钮,支持各种自定义样式。(特点:重新进入当前页面或重启程序倒计时不会重置,仍将继续执行,且退出程序后的时间也会被自动计算在内)

License: MIT License

Objective-C 96.97% Ruby 1.88% C 1.15%
countdown countdown-timer

zxcountdownview's Introduction

ZXCountDownView

License MIT  CocoaPods  CocoaPods  Support 

安装

通过CocoaPods安装

pod 'ZXCountDownView'

手动导入

  • 将ZXCountDownView拖入项目中。

导入头文件

#import "ZXCountDownView.h"

效果图

Image text


Demo

ZXCountDownLabel

  • 在控制器的viewDidLoad方法中设置一个倒计时Label,重启App后仍然保持倒计时进度:
//第一个参数40即为倒计时时间为40秒,第二个参数mark用于标记区分当前倒计时任务和其他倒计时任务,确保与其他任务不重名即可,block第一个参数即为剩余秒数,block返回值即为显示在Label上的文字。(此处实现了一个倒计时40秒,且显示”还剩40、39、38...秒哦“的Label)
[self.scheduleStoreLabel setCountDown:40 mark:@"ScheduleStoreLabel" resTextFormat:^NSString *(long remainSec) {
    if(remainSec > 30){
        weakSelf.scheduleStoreLabel.backgroundColor = [UIColor orangeColor];
    }else{
        weakSelf.scheduleStoreLabel.backgroundColor = [UIColor redColor];
    }
    //显示剩余几分几秒
    NSString *timeformatStr = [NSDate getDateStrWithSec:remainSec dateFormat:@"mm分ss秒"];
    return timeformatStr;
}];
  • 开始倒计时
[self.scheduleStoreLabel startCountDown];

ZXCountDownBtn

  • 与ZXCountDownLabel类似

ZXAutoCountDownBtn

  • 在控制器的viewDidLoad方法中设置一个点击获取验证码的Button,且点击后自动开启倒计时,重启App后仍然保持倒计时进度:
//此处实现了一个倒计时20秒,且显示“还剩20、19、18...秒后重试”的Btn,且退出重新进入当前控制器或重启App不受影响。
[self.getCheckCodeBtn enableAutoCountDown:20 mark:@"GetCheckCodeBtn" resTextFormat:^NSString *(long remainSec) {
    return [NSString stringWithFormat:@"%ld秒后重发",remainSec];
}];
#pragma mark 点击了获取验证码按钮
- (IBAction)getCheckCodeAction:(id)sender {
    //判断如果手机号码不合法,可不触发倒计时
    if(0){
        self.getCheckCodeBtn.terminateCountDown = YES;
        return;
    }
    //如果需要过2秒再执行倒计时
    if(0){
        self.getCheckCodeBtn.terminateCountDown = YES;
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [self.getCheckCodeBtn startCountDown];
            NSLog(@"执行获取验证码操作!!");
        });
        return;
    }
    NSLog(@"执行获取验证码操作!!");
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        //判断如果验证码请求失败,可重置倒计时按钮
        if(0){
            [self.getCheckCodeBtn resume];
        }
        
    });
}

ZXCountDownCore

  • 您也可以不依赖UI控件,直接开启一个倒计时任务
ZXCountDownCore *countDownCore = [[ZXCountDownCore alloc]init];
[countDownCore setCountDown:10 mark:@"testCountDown" resBlock:^(long remainSec) {
    //每秒执行一次
    NSLog(@"remainSec--%ld",remainSec);
}];
//开始倒计时
[countDownCore startCountDown];
  • 【注意:】countDownCore对象必须使用强指针引用,否则会立即被销毁,导致倒计时无效

Other

  • 启用或禁用自动存储倒计时进度:
//disableScheduleStore 是否不存储倒计时进度,默认为NO,即默认存储倒计时进度,若存储倒计时进度,则重启App或重新进入当前控制器,倒计时将无缝继续执行
view.disableScheduleStore = YES;
view.disableScheduleStore = NO;
  • 通用倒计时控制:
//开始倒计时
[view startCountDown];
///暂停倒计时
[view pauseCountDown];
//重新开始倒计时
[view reStartCountDown];
//结束倒计时
[view stopCountDown];
///关闭倒计时
[view invalidateTimer];
  • 禁止倒计时停止时恢复到最初的状态(文字、文字颜色、文字背景色),默认为否,若为否,则remainSec == 0时设置的状态将无效
view.disableResumeWhenEnd = YES;
  • 获取当前倒计时view的状态
ZXCountViewStatus countViewStatus = view.countViewStatus;
//countViewStatus有以下枚举:
//ZXCountViewStatusEnded = 0x00,    // 倒计时View倒计时未开始或已结束
//ZXCountViewStatusPaused = 0x01,    // 倒计时View已暂停
//ZXCountViewStatusRunning = 0x02,    // 倒计时View正在倒计时

注意

  • 若需要实现多个不同的倒计时view共用进度,例如登录获取验证码按钮,注册获取验证码按钮,找回密码获取验证码按钮,只需设置相同mark即可。
  • ZXCountDownView中倒计时结束默认会将UI控件设置回最初的状态,例如刚开始倒计时按钮文字为“点击获取验证码”,倒计时结束您需要设置为“重新获取”,则您需要设置btn.disableResumeWhenEnd = YES,禁止自动将空间设置回最初的状态,并且在resTextFormat中判断remainSec == 0时将按钮设置为“重新获取”即可。
  • 若倒计时view执行了暂停操作且倒计时view自动储存进度开启,则重新初始化倒计时view时,它将仍然处于暂停状态。

zxcountdownview's People

Contributors

smilezxlee avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

zxcountdownview's Issues

纯代码创建问题

这个支持纯代码创建吗?自己写了下,无法设置字体颜色,能否提供一个纯代码创建的代码?万分感谢

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.