iOS quickly implement a record-keeping countdown button

iOS development after login, registration, password recovery page, etc. often need to achieve the countdown button, but in many cases the user clicks the button to start the countdown to the countdown again into the page, they can click again, the countdown records are not retained, although under normal circumstances the server will check again, but we can achieve even more rigorous.

ZXCountDownView support automatically save the countdown record, even if you exit the current controller, restart the App, the countdown remain, to support a number of different controllers share a countdown records, such as login, registration, password recovery page countdown to share a record, click on any button countdown record real-time synchronization.


ZXCountDownView

installation

By installing CocoaPods

pod 'ZXCountDownView'

Manual import

  • The ZXCountDownView dragged into the project.

Import header file

#import "ZXCountDownView.h"

Renderings


Demo

  • Set a countdown Label, and automatically record the progress of the countdown:
//第一个参数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];
  • Click for Button to set up a verification code, and automatically record the progress of the countdown:
//此处实现了一个倒计时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.start = NO;
        return;
    }
    NSLog(@"执行获取验证码操作!!");
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        //判断如果验证码请求失败,可重置倒计时按钮
        if(0){
            [self.getCheckCodeBtn resume];
        }
    });
}
  • You can not rely on UI controls, directly open a task countdown
ZXCountDownCore *countDownCore = [[ZXCountDownCore alloc]init];
[countDownCore setCountDown:10 mark:@"testCountDown" resBlock:^(long remainSec) {
    //每秒执行一次
    NSLog(@"remainSec--%ld",remainSec);
}];
//开始倒计时
[countDownCore startCountDown];
  • Enable or disable automatic storage countdown schedule:
//disableScheduleStore 是否不存储倒计时进度,默认为NO,即默认存储倒计时进度
obj.disableScheduleStore = YES;
obj.disableScheduleStore = NO;
  • Countdown control:
//开始倒计时
-(void)startCountDown;
//重新开始倒计时
-(void)reStartCountDown;
//结束倒计时
-(void)stopCountDown;
  • If you need to implement a number of different countdown view shared progress, such as login button get verification codes, sign up for a verification code button, retrieve the password to get Code button, you can simply set the same mark.

github address ZXCountDownView

Guess you like

Origin www.cnblogs.com/zxlee/p/10938331.html