Giter Club home page Giter Club logo

countdownbutton's Introduction

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

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.