WLCaptcheButton.m 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. //
  2. // WLCaptcheButton.m
  3. // WLButtonCountingDownDemo
  4. //
  5. // Created by wayne on 16/1/14.
  6. // Copyright © 2016年 ZHWAYNE. All rights reserved.
  7. //
  8. #import "WLCaptcheButton.h"
  9. #import "WLButtonCountdownManager.h"
  10. @interface WLCaptcheButton ()
  11. @property (nonatomic, strong) UILabel *overlayLabel;
  12. @end
  13. @implementation WLCaptcheButton
  14. - (instancetype)initWithCoder:(NSCoder *)aDecoder {
  15. if (self = [super initWithCoder:aDecoder]) {
  16. [self initialize];
  17. }
  18. return self;
  19. }
  20. - (instancetype)init {
  21. if (self = [super init]) {
  22. [self initialize];
  23. }
  24. return self;
  25. }
  26. - (void)dealloc {
  27. NSLog(@"***> %s [%@]", __func__, _identifyKey);
  28. }
  29. - (void)initialize {
  30. self.identifyKey = NSStringFromClass([self class]);
  31. self.clipsToBounds = YES;
  32. self.layer.cornerRadius = 4;
  33. if (!self.countdownTime) {
  34. self.countdownTime = 60;
  35. }
  36. [self addSubview:self.overlayLabel];
  37. }
  38. - (UILabel *)overlayLabel {
  39. if (!_overlayLabel) {
  40. _overlayLabel = [UILabel new];
  41. _overlayLabel.textColor = self.titleLabel.textColor;
  42. _overlayLabel.backgroundColor = self.backgroundColor;
  43. _overlayLabel.font = self.titleLabel.font;
  44. _overlayLabel.textAlignment = NSTextAlignmentCenter;
  45. _overlayLabel.hidden = YES;
  46. }
  47. return _overlayLabel;
  48. }
  49. - (void)layoutSubviews {
  50. [super layoutSubviews];
  51. self.overlayLabel.frame = self.bounds;
  52. // if ([[WLButtonCountdownManager defaultManager] countdownTaskExistWithKey:self.identifyKey task:nil]) {
  53. // [self shouldCountDown];
  54. // }
  55. }
  56. - (void)shouldCountDown {
  57. self.enabled = NO;
  58. [self setTitle:@" " forState:UIControlStateNormal];
  59. self.overlayLabel.hidden = NO;
  60. self.overlayLabel.text = @"获取验证码";
  61. [self.overlayLabel setBackgroundColor:self.disabledBackgroundColor ?: self.backgroundColor];
  62. [self.overlayLabel setTextColor:self.disabledTitleColor ?: self.titleLabel.textColor];
  63. __weak __typeof(self) weakSelf = self;
  64. [[WLButtonCountdownManager defaultManager] scheduledCountDownWithKey:self.identifyKey timeInterval:self.countdownTime countingDown:^(NSTimeInterval leftTimeInterval) {
  65. // __strong __typeof(weakSelf) self = weakSelf;
  66. weakSelf.overlayLabel.text = [NSString stringWithFormat:@"%@", @(leftTimeInterval)];// 秒后重试
  67. } finished:^(NSTimeInterval finalTimeInterval) {
  68. // __strong __typeof(weakSelf) self = weakSelf;
  69. weakSelf.enabled = YES;
  70. weakSelf.overlayLabel.hidden = YES;
  71. [weakSelf setTitle:@"获取验证码" forState:UIControlStateNormal];
  72. [weakSelf.overlayLabel setBackgroundColor:weakSelf.backgroundColor];
  73. [weakSelf.overlayLabel setTextColor:weakSelf.titleLabel.textColor];
  74. }];
  75. }
  76. - (void)sendAction:(SEL)action to:(id)target forEvent:(UIEvent *)event {
  77. if (![[self actionsForTarget:target forControlEvent:UIControlEventTouchUpInside] count]) {
  78. return;
  79. }
  80. [super sendAction:action to:target forEvent:event];
  81. }
  82. - (void)fire {
  83. [self shouldCountDown];
  84. }
  85. @end