Skip to content

Commit fe3c469

Browse files
committed
新增DemoVC3:点击button倒计时,两种比较常用的获取倒计时验证码的样式!
1 parent cab50d8 commit fe3c469

28 files changed

Lines changed: 677 additions & 261 deletions

File tree

BABaseProject.xcodeproj/project.pbxproj

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
C42A6AE41CDAE10500D534EE /* BATabBarButton.m in Sources */ = {isa = PBXBuildFile; fileRef = C42A6AE01CDAE10500D534EE /* BATabBarButton.m */; };
3636
C42A6AE51CDAE10500D534EE /* BABadgeView.m in Sources */ = {isa = PBXBuildFile; fileRef = C42A6AE21CDAE10500D534EE /* BABadgeView.m */; };
3737
C42A6AE81CDAE10D00D534EE /* BATabBarController.m in Sources */ = {isa = PBXBuildFile; fileRef = C42A6AE71CDAE10D00D534EE /* BATabBarController.m */; };
38+
C430AB761CDC8A360086DDD6 /* DemoVC3.m in Sources */ = {isa = PBXBuildFile; fileRef = C430AB751CDC8A360086DDD6 /* DemoVC3.m */; };
3839
C46C5AB51CDB4A58002F707F /* NSObject+BAAutoSizeWithWH.m in Sources */ = {isa = PBXBuildFile; fileRef = C46C5AB41CDB4A58002F707F /* NSObject+BAAutoSizeWithWH.m */; };
3940
C46C5AB91CDB75B9002F707F /* BAHomeViewModel.m in Sources */ = {isa = PBXBuildFile; fileRef = C46C5AB81CDB75B9002F707F /* BAHomeViewModel.m */; };
4041
C4EB5AA51CD8B04B003A7094 /* main.m in Sources */ = {isa = PBXBuildFile; fileRef = C4EB5AA41CD8B04B003A7094 /* main.m */; };
@@ -196,7 +197,6 @@
196197
C4EB5C991CD8B58B003A7094 /* BAPassword.m in Sources */ = {isa = PBXBuildFile; fileRef = C4EB5C751CD8B589003A7094 /* BAPassword.m */; };
197198
C4EB5C9A1CD8B58B003A7094 /* BASystemSound.m in Sources */ = {isa = PBXBuildFile; fileRef = C4EB5C771CD8B589003A7094 /* BASystemSound.m */; };
198199
C4EB5C9B1CD8B58B003A7094 /* BATouchID.m in Sources */ = {isa = PBXBuildFile; fileRef = C4EB5C791CD8B589003A7094 /* BATouchID.m */; };
199-
C4EB5C9E1CD8BDAE003A7094 /* Info.plist in Resources */ = {isa = PBXBuildFile; fileRef = C4EB5C9D1CD8BDAE003A7094 /* Info.plist */; };
200200
C4F6B0B01CDC28630070304C /* BAAutoSizeWithWH.m in Sources */ = {isa = PBXBuildFile; fileRef = C4F6B0AF1CDC28630070304C /* BAAutoSizeWithWH.m */; };
201201
/* End PBXBuildFile section */
202202

@@ -271,6 +271,8 @@
271271
C42A6AE21CDAE10500D534EE /* BABadgeView.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = BABadgeView.m; sourceTree = "<group>"; };
272272
C42A6AE61CDAE10D00D534EE /* BATabBarController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = BATabBarController.h; sourceTree = "<group>"; };
273273
C42A6AE71CDAE10D00D534EE /* BATabBarController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = BATabBarController.m; sourceTree = "<group>"; };
274+
C430AB741CDC8A360086DDD6 /* DemoVC3.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = DemoVC3.h; sourceTree = "<group>"; };
275+
C430AB751CDC8A360086DDD6 /* DemoVC3.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = DemoVC3.m; sourceTree = "<group>"; };
274276
C46C5AB31CDB4A58002F707F /* NSObject+BAAutoSizeWithWH.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "NSObject+BAAutoSizeWithWH.h"; sourceTree = "<group>"; };
275277
C46C5AB41CDB4A58002F707F /* NSObject+BAAutoSizeWithWH.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "NSObject+BAAutoSizeWithWH.m"; sourceTree = "<group>"; };
276278
C46C5AB71CDB75B9002F707F /* BAHomeViewModel.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = BAHomeViewModel.h; sourceTree = "<group>"; };
@@ -603,6 +605,7 @@
603605
children = (
604606
C40852B51CDA08F10004A671 /* DemoVC1 */,
605607
C40852B91CDA09020004A671 /* DemoVC2 */,
608+
C430AB731CDC8A170086DDD6 /* DemoVC3 */,
606609
);
607610
path = DemoVC;
608611
sourceTree = "<group>";
@@ -625,6 +628,15 @@
625628
path = DemoVC2;
626629
sourceTree = "<group>";
627630
};
631+
C430AB731CDC8A170086DDD6 /* DemoVC3 */ = {
632+
isa = PBXGroup;
633+
children = (
634+
C430AB741CDC8A360086DDD6 /* DemoVC3.h */,
635+
C430AB751CDC8A360086DDD6 /* DemoVC3.m */,
636+
);
637+
path = DemoVC3;
638+
sourceTree = "<group>";
639+
};
628640
C46C5AB61CDB75B9002F707F /* ViewModel */ = {
629641
isa = PBXGroup;
630642
children = (
@@ -1520,7 +1532,6 @@
15201532
C4EB5AB31CD8B04B003A7094 /* LaunchScreen.storyboard in Resources */,
15211533
C40852A11CD9CD860004A671 /* Lato-Thin.ttf in Resources */,
15221534
C4EB5C201CD8B251003A7094 /* test3.jpg in Resources */,
1523-
C4EB5C9E1CD8BDAE003A7094 /* Info.plist in Resources */,
15241535
C408529E1CD9CD860004A671 /* Lato-Bold.ttf in Resources */,
15251536
C4EB5C1B1CD8B251003A7094 /* pic8.jpg in Resources */,
15261537
C4EB5BFE1CD8B251003A7094 /* 18.jpg in Resources */,
@@ -1703,6 +1714,7 @@
17031714
C4EB5BC81CD8B250003A7094 /* BAShareManage.m in Sources */,
17041715
C4EB5C841CD8B58B003A7094 /* NSString+BAKit.m in Sources */,
17051716
C4EB5C861CD8B58B003A7094 /* BACustomModalTransition.m in Sources */,
1717+
C430AB761CDC8A360086DDD6 /* DemoVC3.m in Sources */,
17061718
C4EB5C7E1CD8B58B003A7094 /* NSDictionary+BAKit.m in Sources */,
17071719
C42A6AE41CDAE10500D534EE /* BATabBarButton.m in Sources */,
17081720
C4EB5C801CD8B58B003A7094 /* NSMutableArray+BAKit.m in Sources */,
Binary file not shown.

BABaseProject.xcworkspace/xcuserdata/boai.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@
5252
moduleName = "BABaseProject"
5353
usesParentBreakpointCondition = "Yes"
5454
urlString = "file:///Users/boai/%E5%8D%9A%E7%88%B1%E8%B5%84%E6%96%99/GitHub/BABaseProject/BABaseProject/BAMainCode/BATools(%E5%B7%A5%E5%85%B7%E7%B1%BB)/BACategory(%E5%88%86%E7%B1%BB)/NSObject+Network.m"
55-
timestampString = "484191233.084347"
55+
timestampString = "484220029.336635"
5656
startingColumnNumber = "9223372036854775807"
5757
endingColumnNumber = "9223372036854775807"
5858
startingLineNumber = "147"
@@ -67,7 +67,7 @@
6767
moduleName = "BABaseProject"
6868
usesParentBreakpointCondition = "Yes"
6969
urlString = "file:///Users/boai/%E5%8D%9A%E7%88%B1%E8%B5%84%E6%96%99/GitHub/BABaseProject/BABaseProject/BAMainCode/BATools(%E5%B7%A5%E5%85%B7%E7%B1%BB)/BACategory(%E5%88%86%E7%B1%BB)/NSObject+Network.m"
70-
timestampString = "484191233.084502"
70+
timestampString = "484220029.336819"
7171
startingColumnNumber = "9223372036854775807"
7272
endingColumnNumber = "9223372036854775807"
7373
startingLineNumber = "147"

BABaseProject/BAMainCode/BABaseData(基类)/BABaseVC/BABaseViewController.m

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,8 @@ - (void)viewDidLoad {
6363
#pragma mark - ***** 基本设置
6464
- (void)setupBase
6565
{
66-
66+
self.view.backgroundColor = BA_White_Color;
67+
6768

6869
}
6970

@@ -81,10 +82,6 @@ - (void)setVCBgColor:(UIColor *)vcBgColor
8182
{
8283
self.view.backgroundColor = vcBgColor;
8384
}
84-
else
85-
{
86-
self.view.backgroundColor = BA_White_Color;
87-
}
8885
}
8986

9087
#pragma mark - ***** navi设置

BABaseProject/BAMainCode/BACode/Home(首页)/Controller/BAHomeViewController.m

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,8 @@ - (void)creatData
8888
{
8989
[self addCell:@"DemoVC ** 1 ** 测试AFN数据请求" content:@"封装AFN,实现简单的get和post请求,可以自定义请求头,可以解析json数据、xml数据、data数据,可以获取缓存数据。" class:@"DemoVC1"];
9090
[self addCell:@"DemoVC ** 2 ** 清理缓存" content:@"清理网络请求缓存和图片缓存,可清理系统缓存!" class:@"DemoVC2"];
91+
[self addCell:@"DemoVC ** 3 ** 点击button倒计时" content:@"两种比较常用的获取倒计时验证码的样式!" class:@"DemoVC3"];
92+
9193
}
9294

9395
- (void)addCell:(NSString *)title content:(NSString *)content class:(NSString *)className
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
//
2+
// DemoVC3.h
3+
// BABaseProject
4+
//
5+
// Created by 博爱 on 16/5/6.
6+
// Copyright © 2016年 博爱之家. All rights reserved.
7+
//
8+
9+
#import "BABaseViewController.h"
10+
11+
@interface DemoVC3 : BABaseViewController
12+
13+
@end
Lines changed: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
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

BABaseProject/BAMainCode/BATools(工具类)/BATool(工具类)/BA_Kit/BACustom/UIKit/UIImage+BAKit.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,16 @@
318318
*/
319319
+ (UIImage *)imageWithColor:(UIColor *)color;
320320

321+
/*!
322+
* 图片背景颜色
323+
*
324+
* @param color Color value
325+
* @param size size
326+
*
327+
* @return Return an UIImage instance
328+
*/
329+
+ (UIImage *)imageWithColor:(UIColor *)color size:(CGSize)size;
330+
321331
+ (instancetype)imageWithStretchableName:(NSString *)imageName;
322332

323333
/*! 获得的就是一个圆形的图片 */

BABaseProject/BAMainCode/BATools(工具类)/BATool(工具类)/BA_Kit/BACustom/UIKit/UIImage+BAKit.m

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -832,6 +832,23 @@ - (UIImage *)boxBlurImageWithBlur:(CGFloat)blur
832832
return returnImage;
833833
}
834834

835+
/*!
836+
* 图片背景颜色
837+
*
838+
* @param color Color value
839+
* @param size size
840+
*
841+
* @return Return an UIImage instance
842+
*/
843+
+ (UIImage *)imageWithColor:(UIColor *)color size:(CGSize)size
844+
{
845+
CGRect rect = CGRectMake(0.0f, 0.0f, size.width, size.height); UIGraphicsBeginImageContext(rect.size);
846+
CGContextRef context = UIGraphicsGetCurrentContext(); CGContextSetFillColorWithColor(context, [color CGColor]); CGContextFillRect(context, rect);
847+
UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext();
848+
849+
return image;
850+
}
851+
835852
+ (instancetype)imageWithStretchableName:(NSString *)imageName
836853
{
837854
UIImage *image = [UIImage imageNamed:imageName];

Podfile

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,13 @@ target 'BABaseProject' do
5353
# 把系统的target+selector/委托模式 转为 Block语法,让代码结构更加紧凑
5454
pod 'BlocksKit'
5555

56+
# 自定义提示框
5657
pod 'Toast', '~> 3.0'
5758

59+
# 一个支持多选、选原图和视频的图片选择器,同时有预览功能,适配了iOS6789系统。
60+
# pod 'TZImagePickerController'
61+
62+
5863
end
5964

6065
target 'BABaseProjectTests' do

0 commit comments

Comments
 (0)