| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108 |
- //
- // WLCaptcheButton.m
- // WLButtonCountingDownDemo
- //
- // Created by wayne on 16/1/14.
- // Copyright © 2016年 ZHWAYNE. All rights reserved.
- //
- #import "WLCaptcheButton.h"
- #import "WLButtonCountdownManager.h"
- @interface WLCaptcheButton ()
- @property (nonatomic, strong) UILabel *overlayLabel;
- @end
- @implementation WLCaptcheButton
- - (instancetype)initWithCoder:(NSCoder *)aDecoder {
- if (self = [super initWithCoder:aDecoder]) {
- [self initialize];
- }
-
- return self;
- }
- - (instancetype)init {
- if (self = [super init]) {
- [self initialize];
- }
-
- return self;
- }
- - (void)dealloc {
- NSLog(@"***> %s [%@]", __func__, _identifyKey);
- }
- - (void)initialize {
- self.identifyKey = NSStringFromClass([self class]);
- self.clipsToBounds = YES;
- self.layer.cornerRadius = 4;
-
- if (!self.countdownTime) {
- self.countdownTime = 60;
- }
- [self addSubview:self.overlayLabel];
- }
- - (UILabel *)overlayLabel {
- if (!_overlayLabel) {
- _overlayLabel = [UILabel new];
- _overlayLabel.textColor = self.titleLabel.textColor;
- _overlayLabel.backgroundColor = self.backgroundColor;
- _overlayLabel.font = self.titleLabel.font;
- _overlayLabel.textAlignment = NSTextAlignmentCenter;
- _overlayLabel.hidden = YES;
- }
-
- return _overlayLabel;
- }
- - (void)layoutSubviews {
- [super layoutSubviews];
- self.overlayLabel.frame = self.bounds;
-
- // if ([[WLButtonCountdownManager defaultManager] countdownTaskExistWithKey:self.identifyKey task:nil]) {
- // [self shouldCountDown];
- // }
- }
- - (void)shouldCountDown {
- self.enabled = NO;
- [self setTitle:@" " forState:UIControlStateNormal];
- self.overlayLabel.hidden = NO;
- self.overlayLabel.text = @"获取验证码";
- [self.overlayLabel setBackgroundColor:self.disabledBackgroundColor ?: self.backgroundColor];
- [self.overlayLabel setTextColor:self.disabledTitleColor ?: self.titleLabel.textColor];
-
- __weak __typeof(self) weakSelf = self;
- [[WLButtonCountdownManager defaultManager] scheduledCountDownWithKey:self.identifyKey timeInterval:self.countdownTime countingDown:^(NSTimeInterval leftTimeInterval) {
- // __strong __typeof(weakSelf) self = weakSelf;
- weakSelf.overlayLabel.text = [NSString stringWithFormat:@"%@", @(leftTimeInterval)];// 秒后重试
- } finished:^(NSTimeInterval finalTimeInterval) {
- // __strong __typeof(weakSelf) self = weakSelf;
- weakSelf.enabled = YES;
- weakSelf.overlayLabel.hidden = YES;
- [weakSelf setTitle:@"获取验证码" forState:UIControlStateNormal];
- [weakSelf.overlayLabel setBackgroundColor:weakSelf.backgroundColor];
- [weakSelf.overlayLabel setTextColor:weakSelf.titleLabel.textColor];
- }];
- }
- - (void)sendAction:(SEL)action to:(id)target forEvent:(UIEvent *)event {
- if (![[self actionsForTarget:target forControlEvent:UIControlEventTouchUpInside] count]) {
- return;
- }
-
- [super sendAction:action to:target forEvent:event];
- }
- - (void)fire {
- [self shouldCountDown];
- }
- @end
|