iOS获取验证码按钮的简单实现

开发中,获取验证码的按钮应该比较常见,但大多app的验证码按钮都有一个小小的bug,开发者并不会在意,那就是:

当点击按钮后,按钮进入倒计时,此时用户选择不留在页面等待,跳转到其他页面,然后收到验证码后,再返回页面进行输入。那么,此时的按钮是否依旧在进行倒计时呢???

今天我就写了一个封装,解决一下这个bug。思想很简单:单例!
老规矩,没图说**,丢效果图:

sms1.gif
sms2.gif

然后是具体实现的代码,拷贝就可以了😄

//
//  SmsButtonHandle.h
//  My Tool
//
//  Created by wuwj on 2016/10/25.
//  Copyright © 2016年 wuwj. All rights reserved.
//

#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
@interface SmsButtonHandle : NSObject
//验证码按钮单例封装
+ (instancetype)sharedSmsBHandle;

- (UIButton *)buttonWithFrame:(CGRect)frame title:(NSString *)title action:(SEL)action superVC:(UIViewController *)superVC;

- (void)startTimer;
@end
//
//  SmsButtonHandle.m
//  My Tool
//
//  Created by wuwj on 2016/10/25.
//  Copyright © 2016年 wuwj. All rights reserved.
//

#import "SmsButtonHandle.h"
#import "BaseDefinition.h"
@implementation SmsButtonHandle{
    NSTimeInterval timeInterval;
    UIButton *smsButton;
    NSString *authTitle;
    NSTimer *timer;
    BOOL isFired;
}

+ (instancetype)sharedSmsBHandle{
    static dispatch_once_t onceToken;
    static SmsButtonHandle *handle;
    
    dispatch_once(&onceToken, ^{
        handle = [[SmsButtonHandle alloc] init];
    });
    return handle;
}

- (UIButton *)buttonWithFrame:(CGRect)frame title:(NSString *)title action:(SEL)action superVC:(UIViewController *)superVC{
    [[SmsButtonHandle sharedSmsBHandle] timeInterval];
    smsButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
    smsButton.frame = frame;
    [smsButton addTarget:superVC action:action forControlEvents:(UIControlEventTouchUpInside)];
    if ([[SmsButtonHandle sharedSmsBHandle] timeInterval] < 59) {
        [[SmsButtonHandle sharedSmsBHandle] startTimer];
    }else{
        [smsButton setTitle:title forState:(UIControlStateNormal)];
    }
    [[SmsButtonHandle sharedSmsBHandle] authTitle:title];
    [smsButton setTitleColor:[UIColor blackColor] forState:(UIControlStateNormal)];
    
    return smsButton;
}

- (NSString *)authTitle:(NSString *)title{
    if (!authTitle) {
        authTitle = title;
    }
    return authTitle;
}

- (NSTimeInterval)timeInterval{
    if (!timeInterval) {
        timeInterval = 59;
    }
    return timeInterval;
}

- (NSTimer *)timer{
    if (!timer) {
        timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timerAction) userInfo:nil repeats:YES];
        [timer setFireDate:[NSDate distantFuture]];
    }
    return timer;
}

#pragma mark - Timer Action
- (void)timerAction{
    if (timeInterval >= 1) {
        [smsButton setTitle:[authTitle stringByAppendingString:[NSString stringWithFormat:@"(%@)",@(timeInterval--)]] forState:UIControlStateNormal];
        smsButton.backgroundColor = [UIColor lightGrayColor];
        smsButton.userInteractionEnabled = NO;
    }else{
        [self stopTimer];
        timeInterval = 59;
        authTitle = @"重新发送";
        smsButton.backgroundColor = UIColorFromRGB(0xe45e39);
        [smsButton setTitle:authTitle forState:UIControlStateNormal];
        smsButton.userInteractionEnabled = YES;
    }
}

#pragma mark - Private Method
- (void)startTimer{
    isFired = YES;
    [self.timer setFireDate:[NSDate date]];
}

- (void)stopTimer{
    if (timer) {
        [timer setFireDate:[NSDate distantFuture]];
        isFired = NO;
    }
}

- (void)endTimer{
    if (timer) {
        timeInterval = 59;
        isFired = NO;
        [timer invalidate];
        timer = nil;
    }
}

@end

之后的调用就十分简单方便了

- (void)viewWillAppear:(BOOL)animated{
    self.view.backgroundColor = [UIColor whiteColor];
    
    UIButton *button = [[SmsButtonHandle sharedSmsBHandle] buttonWithFrame:(CGRectMake(100, 100, 200, 100)) title:@"发送短信" action:@selector(buttonAction) superVC:self];
    [self.view addSubview:button ];
}
- (void)buttonAction{
    NSLog(@"按钮事件");
//此处可以先调接口,成功后再调此方法
    [[SmsButtonHandle sharedSmsBHandle] startTimer];
}

写个文章,浏览器都一直炸,不写了,跑路!!

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 175,062评论 25 709
  • iOS 的倒计时有多种实现细节,Cocoa Touch 为我们提供了 NSTimer 类和 GCD 的dispat...
    waylen阅读 11,699评论 36 44
  • 小A,一个从小性格腼腆,见人就脸红的女孩子。高考那年在网吧查了成绩,出网吧时哭的跌跌撞撞。 十四年前,小...
    dd66cfb56e74阅读 2,885评论 0 0
  • 2017年11月18日 周六 零点(17日24点)左右看完了《三十二》。 有几句话还是要讲的:一个无法正视和...
    徐宸灏阅读 858评论 1 0
  • [cp]#晨读# 让以前的事都过去吧,和以前的世界一刀两断,再不想听到它的任何情况,任何消息,到一个新的世界,新的...
    靖哥哥家的黄蓉阅读 1,144评论 0 0