Latest 1.0
Homepage https://github.com/CaiWanFeng/CQCountDownButton
License MIT
Platforms ios 8.0
Authors

你或许想不到,一个简单的倒计时按钮还能这样玩。。。

详情:

功能:

  • 支持纯代码frame布局和自动布局,也支持xib和storyboard
  • 提供block与delegate两种回调方式

导入:

  • 直接把CQCountDwonButton文件夹拖到你的项目里
  • 也可以使用CocoaPods:pod 'CQCountDownButton'

用法:

  • block版本:

__weak typeof(self) weakSelf = self;

[self.countDownButton configDuration:10 buttonClicked:^{
    //========== 按钮点击 ==========//
    [weakSelf.countDownButton startCountDown];
} countDownStart:^{
    //========== 倒计时开始 ==========//
    NSLog(@"倒计时开始");
} countDownUnderway:^(NSInteger restCountDownNum) {
    //========== 倒计时进行中 ==========//
    NSString *title = [NSString stringWithFormat:@"%ld秒后重试", restCountDownNum];
    [weakSelf.countDownButton setTitle:title forState:UIControlStateNormal];
} countDownCompletion:^{
    //========== 倒计时结束 ==========//
    [weakSelf.countDownButton setTitle:@"点击获取验证码" forState:UIControlStateNormal];
    NSLog(@"倒计时结束");
}];
  • delegate版本:

// 遵循协议
@interface CQDelegateViewController () <CQCountDownButtonDataSource, CQCountDownButtonDelegate>

// 设置代理
self.countDownButton.dataSource = self;
self.countDownButton.delegate = self;

#pragma mark - CQCountDownButton DataSource

// 设置倒计时总时间
- (NSInteger)startCountDownNumOfCountDownButton:(CQCountDownButton *)countDownButton {
    return 10;
}

#pragma mark - CQCountDownButton Delegate

// 倒计时按钮点击时回调
- (void)countDownButtonDidClick:(CQCountDownButton *)countDownButton {
    [SVProgressHUD showWithStatus:@"正在获取验证码..."];
    // 模拟数据请求
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        int a = arc4random() % 2;
        if (a == 0) {
            // 获取成功
            [SVProgressHUD showSuccessWithStatus:@"验证码已发送"];
            // 获取到验证码后开始倒计时
            [self.countDownButton startCountDown];
        } else {
            // 获取失败
            [SVProgressHUD showErrorWithStatus:@"获取失败,请重试"];
            [self.countDownButton endCountDown];
        }
    });
}

// 倒计时开始的回调
- (void)countDownButtonDidStartCountDown:(CQCountDownButton *)countDownButton {
    NSLog(@"倒计时开始");
}

// 倒计时进行中的回调
- (void)countDownButtonDidInCountDown:(CQCountDownButton *)countDownButton withRestCountDownNum:(NSInteger)restCountDownNum {
    NSString *title = [NSString stringWithFormat:@"%ld秒后重试", restCountDownNum];
    [self.countDownButton setTitle:title forState:UIControlStateNormal];
}

// 倒计时结束时的回调
- (void)countDownButtonDidEndCountDown:(CQCountDownButton *)countDownButton {
    [self.countDownButton setTitle:@"点击获取验证码" forState:UIControlStateNormal];
    NSLog(@"倒计时结束");
}

Latest podspec

{
    "name": "CQCountDownButton",
    "version": "1.0",
    "summary": "A simple and easy use countdown button.",
    "description": "u5b9eu7528u53c8u597du7528u7684u5012u8ba1u65f6buttonuff0cu53efu7528u4e8eu83b7u53d6u9a8cu8bc1u7801u7b49u573au666fu3002",
    "homepage": "https://github.com/CaiWanFeng/CQCountDownButton",
    "license": "MIT",
    "authors": {
        "caiqiang": "[email protected]"
    },
    "social_media_url": "https://www.jianshu.com/u/4212f351f6b5",
    "platforms": {
        "ios": "8.0"
    },
    "source": {
        "git": "https://github.com/CaiWanFeng/CQCountDownButton.git",
        "tag": "1.0"
    },
    "source_files": [
        "CQCountDownButton",
        "CQCountDownButton/**/*.{h,m}"
    ]
}

Pin It on Pinterest

Share This