口袋优选

WLCaptcheButton.m 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. //
  2. // WLCaptcheButton.m
  3. // WLButtonCountingDownDemo
  4. //
  5. // Created by wayne on 16/1/14.
  6. // Copyright © 2016年 ZHWAYNE. All rights reserved.
  7. // //https://github.com/zhwayne/WLButtonCountingDownDemo
  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.clipsToBounds = YES;
  31. self.layer.cornerRadius = 4;
  32. self.opaque = NO;
  33. [self addSubview:self.overlayLabel];
  34. }
  35. - (UILabel *)overlayLabel {
  36. if (!_overlayLabel) {
  37. _overlayLabel = [UILabel new];
  38. _overlayLabel.textColor = self.titleLabel.textColor;
  39. _overlayLabel.backgroundColor = self.backgroundColor;
  40. _overlayLabel.font = self.titleLabel.font;
  41. _overlayLabel.textAlignment = NSTextAlignmentCenter;
  42. _overlayLabel.alpha = 0;
  43. _overlayLabel.opaque = NO;
  44. _overlayLabel.font = [UIFont systemFontOfSize:13];
  45. }
  46. return _overlayLabel;
  47. }
  48. - (void)layoutSubviews {
  49. [super layoutSubviews];
  50. self.overlayLabel.frame = self.bounds;
  51. if ([[WLButtonCountdownManager defaultManager] countdownTaskExistWithKey:self.identifyKey task:nil]) {
  52. [self shouldCountDown];
  53. }
  54. }
  55. - (void)shouldCountDown {
  56. __weak __typeof(self) weakSelf = self;
  57. [[WLButtonCountdownManager defaultManager] scheduledCountDownWithKey:self.identifyKey timeInterval:60 countingDown:^(NSTimeInterval leftTimeInterval) {
  58. __strong __typeof(weakSelf) self = weakSelf;
  59. self.enabled = NO;
  60. self.titleLabel.alpha = 0;
  61. self.overlayLabel.alpha = 1;
  62. [self.overlayLabel setBackgroundColor:self.disabledBackgroundColor ?: self.backgroundColor];
  63. [self.overlayLabel setTextColor:self.disabledTitleColor ?: self.titleLabel.textColor];
  64. self.overlayLabel.text = [NSString stringWithFormat:@"%@s", @(leftTimeInterval)];
  65. } finished:^(NSTimeInterval finalTimeInterval) {
  66. __strong __typeof(weakSelf) self = weakSelf;
  67. self.enabled = YES;
  68. self.overlayLabel.alpha = 0;
  69. self.titleLabel.alpha = 1;
  70. [self.overlayLabel setBackgroundColor:self.backgroundColor];
  71. [self.overlayLabel setTextColor:self.titleLabel.textColor];
  72. }];
  73. }
  74. - (void)sendAction:(SEL)action to:(id)target forEvent:(UIEvent *)event {
  75. if (![[self actionsForTarget:target forControlEvent:UIControlEventTouchUpInside] count]) {
  76. return;
  77. }
  78. [super sendAction:action to:target forEvent:event];
  79. }
  80. - (void)fire {
  81. [self shouldCountDown];
  82. }
  83. @end