|
| 1 | +// |
| 2 | +// DemoVC3.m |
| 3 | +// BABaseProject |
| 4 | +// |
| 5 | +// Created by 博爱 on 16/5/6. |
| 6 | +// Copyright © 2016年 博爱之家. All rights reserved. |
| 7 | +// |
| 8 | + |
| 9 | +#import "DemoVC3.h" |
| 10 | + |
| 11 | +@interface DemoVC3 () |
| 12 | +{ |
| 13 | + NSInteger _time; |
| 14 | +} |
| 15 | +@property (nonatomic, strong) UIButton *timeButton; |
| 16 | +@property (nonatomic, strong) NSTimer *timer; |
| 17 | +@property (nonatomic, strong) UIButton *timeButton2; |
| 18 | + |
| 19 | +@end |
| 20 | + |
| 21 | +@implementation DemoVC3 |
| 22 | + |
| 23 | +- (void)viewDidLoad { |
| 24 | + [super viewDidLoad]; |
| 25 | + |
| 26 | + self.timeButton.hidden = NO; |
| 27 | + self.timeButton2.hidden = NO; |
| 28 | + |
| 29 | +} |
| 30 | + |
| 31 | +#pragma mark - ***** 第一种倒计时button |
| 32 | +- (UIButton *)timeButton |
| 33 | +{ |
| 34 | + if (!_timeButton) |
| 35 | + { |
| 36 | + _time = 29; |
| 37 | + self.timeButton = [UIButton buttonWithType:UIButtonTypeCustom]; |
| 38 | + self.timeButton.backgroundColor = [UIColor orangeColor]; |
| 39 | + [self.timeButton setTitle:@"获取验证码" forState:UIControlStateNormal]; |
| 40 | + self.timeButton.titleLabel.font = BA_FontSize(15); |
| 41 | + [self.timeButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; |
| 42 | + [self.timeButton addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside]; |
| 43 | + [self.timeButton setCornerRadius:5]; |
| 44 | + |
| 45 | + [self refreshButtonWidth]; |
| 46 | + [self.view addSubview:self.timeButton]; |
| 47 | + } |
| 48 | + return _timeButton; |
| 49 | +} |
| 50 | + |
| 51 | +- (void)refreshButtonWidth |
| 52 | +{ |
| 53 | + CGFloat width = 0; |
| 54 | + if (self.timeButton.enabled) |
| 55 | + { |
| 56 | + width = 100; |
| 57 | + } |
| 58 | + else |
| 59 | + { |
| 60 | + width = 120; |
| 61 | + } |
| 62 | + self.timeButton.center = CGPointMake(self.view.centerX, 100); |
| 63 | + self.timeButton.bounds = CGRectMake(0, 0, width, 40); |
| 64 | + |
| 65 | + // 每次刷新,保证区域正确 |
| 66 | + [self.timeButton setBackgroundImage:[UIImage imageWithColor:[UIColor orangeColor] size:self.timeButton.frame.size] forState:UIControlStateNormal]; |
| 67 | + [self.timeButton setBackgroundImage:[UIImage imageWithColor:[UIColor lightGrayColor] size:self.timeButton.frame.size] forState:UIControlStateDisabled]; |
| 68 | +} |
| 69 | + |
| 70 | +- (IBAction)btnAction:(UIButton *)sender |
| 71 | +{ |
| 72 | + sender.enabled = NO; |
| 73 | + [self refreshButtonWidth]; |
| 74 | + [sender setTitle:[NSString stringWithFormat:@"获取验证码(%zi)", _time] forState:UIControlStateNormal]; |
| 75 | + _timer = [NSTimer scheduledTimerWithTimeInterval:0.3 target:self selector:@selector(timeDown) userInfo:nil repeats:YES]; |
| 76 | +} |
| 77 | + |
| 78 | +- (void)timeDown |
| 79 | +{ |
| 80 | + _time --; |
| 81 | + if (_time == 0) |
| 82 | + { |
| 83 | + [self.timeButton setTitle:@"重新获取" forState:UIControlStateNormal]; |
| 84 | + self.timeButton.enabled = YES; |
| 85 | + [self refreshButtonWidth]; |
| 86 | + [_timer invalidate]; |
| 87 | + _timer = nil; |
| 88 | + _time = 29 ; |
| 89 | + return; |
| 90 | + } |
| 91 | + [self.timeButton setTitle:[NSString stringWithFormat:@"获取验证码(%zi)", _time] forState:UIControlStateNormal]; |
| 92 | +} |
| 93 | + |
| 94 | +#pragma mark - ***** 第二种倒计时button【推荐使用】 |
| 95 | +- (UIButton *)timeButton2 |
| 96 | +{ |
| 97 | + if (!_timeButton2) |
| 98 | + { |
| 99 | + _time = 29; |
| 100 | + _timeButton2 = [UIButton buttonWithType:UIButtonTypeCustom]; |
| 101 | + _timeButton2.backgroundColor = BA_Green_Color; |
| 102 | + _timeButton2.frame = CGRectMake(_timeButton.x, _timeButton.bottom + 10, 120, _timeButton.height); |
| 103 | + [_timeButton2 setTitle:@"获取验证码" forState:UIControlStateNormal]; |
| 104 | + _timeButton2.titleLabel.font = BA_FontSize(15); |
| 105 | + [_timeButton2 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; |
| 106 | + [_timeButton2 addTarget:self action:@selector(btnAction2:) forControlEvents:UIControlEventTouchUpInside]; |
| 107 | + [_timeButton2 setCornerRadius:5]; |
| 108 | + |
| 109 | + [self.view addSubview:_timeButton2]; |
| 110 | + } |
| 111 | + return _timeButton2; |
| 112 | +} |
| 113 | + |
| 114 | +- (void)btnAction2:(UIButton *)btn |
| 115 | +{ |
| 116 | + __block int timeout = 29; |
| 117 | + |
| 118 | + // 倒计时时间 |
| 119 | + dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0); |
| 120 | + dispatch_source_t _timer2 = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0,queue); |
| 121 | + dispatch_source_set_timer(_timer2,dispatch_walltime(NULL, 0),1.0*NSEC_PER_SEC, 0); |
| 122 | + |
| 123 | + // 每秒执行 |
| 124 | + dispatch_source_set_event_handler(_timer2, ^{ |
| 125 | + |
| 126 | + if(timeout <= 0) |
| 127 | + { |
| 128 | + // 倒计时结束,关闭 |
| 129 | + dispatch_source_cancel(_timer2); dispatch_async(dispatch_get_main_queue(), ^{ |
| 130 | + |
| 131 | + // 设置界面的按钮显示 根据自己需求设置 |
| 132 | + [btn setTitle:@"发送验证码" forState:UIControlStateNormal]; |
| 133 | + btn.userInteractionEnabled = YES; |
| 134 | + |
| 135 | + }); |
| 136 | + } |
| 137 | + else |
| 138 | + { |
| 139 | + int seconds = timeout % 60; |
| 140 | + NSString *strTime = [NSString stringWithFormat:@"%d", seconds]; |
| 141 | + if ([strTime isEqualToString:@"0"]) |
| 142 | + { |
| 143 | + strTime = [NSString stringWithFormat:@"%d", 60]; |
| 144 | + } |
| 145 | + dispatch_async(dispatch_get_main_queue(), ^{ |
| 146 | + |
| 147 | + // 设置界面的按钮显示 根据自己需求设置 |
| 148 | + // NSLog(@"____%@",strTime); |
| 149 | + [UIView beginAnimations:nil context:nil]; |
| 150 | + [UIView setAnimationDuration:1]; |
| 151 | + [btn setTitle:[NSString stringWithFormat:@"%@秒后重新发送",strTime] forState:UIControlStateNormal]; |
| 152 | + [UIView commitAnimations]; |
| 153 | + btn.userInteractionEnabled = NO; |
| 154 | + |
| 155 | + }); |
| 156 | + timeout--; |
| 157 | + } |
| 158 | + }); |
| 159 | + dispatch_resume(_timer2); |
| 160 | +} |
| 161 | + |
| 162 | +@end |
0 commit comments