Skip to content

一种通过GCD计时器封装的倒计时按钮,用于获取验证码的常见场景

License

Notifications You must be signed in to change notification settings

lovesinger/CountDownButton

Folders and files

NameName
Last commit message
Last commit date

Latest commit

6ac9d19 · Aug 8, 2019

History

6 Commits
Aug 8, 2019
Aug 8, 2019
Aug 8, 2019
Aug 8, 2019

Repository files navigation

CountDownButton

一种通过GCD计时器封装的倒计时按钮,用于获取验证码的常见场景

Installation

Manually
Copy CodeButton.h CodeButton.m to your project.

Usage

Code

//创建按钮,设置frame
    CodeButton* codeButton = [CodeButton buttonWithType:UIButtonTypeCustom];
    codeButton.frame = CGRectMake(0, 0, 100, 50);
    codeButton.center = self.view.center;
    [self.view addSubview:codeButton];
    
    //设置按钮样式,不设将使用默认样式
//    [codeButton setTitleColor:[UIColor colorWithRed:75/255.f green:123/255.f blue:255/255.f alpha:1] forState:UIControlStateNormal];
//    [codeButton setTitle:@"获取验证码" forState:UIControlStateNormal];
//    codeButton.titleLabel.font = [UIFont fontWithName:@"PingFangSC-Regular" size: 16];
    
    //点击按钮时的回调
    __weak typeof(codeButton) weakCodeButton = codeButton;
    codeButton.clickButtonBlock = ^(CodeButton *sender) {
        //点击按钮,先调用接口发送验证码
        //验证码发送成功之后,开始计时。可以分别在时间改变和倒计时结束时,设置按钮显示样式,不设将使用默认样式
        
        //下面为使用默认按钮样式写法
        [weakCodeButton startTimerWithTotalTime:59 andTimeChangeBlock:nil andTimeStopBlock:nil];
        
        //下面为自定义按钮样式的写法
//        [weakCodeButton startTimerWithTotalTime:59 andTimeChangeBlock:^(CodeButton *sender, NSString *lastTime) {
//            [sender setTitle:[NSString stringWithFormat:@"重新发送(%@)", lastTime] forState:UIControlStateNormal];
//            [sender setTitleColor:[UIColor colorWithRed:190/255.f green:190/255.f blue:190/255.f alpha:1] forState:UIControlStateNormal];
//            [sender setContentHorizontalAlignment:UIControlContentHorizontalAlignmentCenter];
//            [sender setUserInteractionEnabled:NO];
//
//        } andTimeStopBlock:^(CodeButton *sender) {
//            [sender setTitle:@"重新发送" forState:UIControlStateNormal];
//            [sender setTitleColor:[UIColor colorWithRed:75/255.0f green:123/255.0f blue:255/255.0f alpha:1] forState:UIControlStateNormal];
//            [sender setUserInteractionEnabled:YES];
//
//        }];
        
    };

License

This code is distributed under the terms and conditions of the MIT license.

Demo

演示gif

About

一种通过GCD计时器封装的倒计时按钮,用于获取验证码的常见场景

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published