CNLiveLoginView.m
13.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
//
// CNLiveLoginView.m
// CNLiveLoginModule
//
// Created by 153993236@qq.com on 11/12/2019.
// Copyright (c) 2019 153993236@qq.com. All rights reserved.
//
#import "CNLiveLoginView.h"
#import "YYKit.h"
#import "CNLiveLoginCountryView.h"
#import "CNLiveLoginTextField.h"
#import "CNLiveCountDownButton.h"
//#import "XWLoginNotification.h"
@interface CNLiveLoginView()<UITextFieldDelegate, CNLiveLoginCountryViewDelegate,CNLiveCountDownButtonDataSource, CNLiveCountDownButtonDelegate>
{
NSString *_countryCode; // 国家Code
NSString *_verificationCode; // 验证码
}
@property (nonatomic, weak) UIImageView *logo;
@property (nonatomic, weak) CNLiveLoginCountryView *country;
@property (nonatomic, weak) CNLiveLoginTextField *account;
@property (nonatomic, weak) CNLiveLoginTextField *code;
@property (nonatomic, strong) CNLiveCountDownButton *countDown;
@property (nonatomic, weak) UIButton *login;
@end
@implementation CNLiveLoginView
static NSInteger const rowHeight = 40;
static NSInteger const space = 20;
- (instancetype)initWithFrame:(CGRect)frame{
self = [super initWithFrame:frame];
if (self) {
self.userInteractionEnabled = YES;
_countryCode = @"86";
[self createSubviews:frame];
}
return self;
}
- (void)createSubviews:(CGRect)frame{
UIImageView *logo = [[UIImageView alloc] initWithFrame:CGRectMake((frame.size.width-80)/2.0, 0, 80, 105)];
UIImage *image1 = [self xw_getImageName:@"login_logo" bundleName:@"CNLiveLoginModule" targetClass:[self class]];
logo.image = image1;
[self addSubview:logo];
self.logo = logo;
CNLiveLoginCountryView *country = [[CNLiveLoginCountryView alloc] initWithFrame:CGRectMake(0, logo.bottom+space, frame.size.width, rowHeight)];
country.delegate = self;
[self addSubview:country];
self.country = country;
CNLiveLoginTextField *account = [[CNLiveLoginTextField alloc] initWithFrame:CGRectMake(country.left, country.bottom+space, frame.size.width, rowHeight)];
account.keyboardType = UIKeyboardTypeNumberPad;
account.placeholder = @"请输入手机号";
account.textColor = [UIColor colorWithRed:51/255.0 green:51/255.0 blue:51/255.0 alpha:1.0];
account.placeholderColor = [UIColor colorWithRed:173/255.0 green:173/255.0 blue:173/255.0 alpha:1.0];
account.font = [UIFont systemFontOfSize:14];
account.textAlignment = NSTextAlignmentLeft;
[account addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
account.clearButtonMode = UITextFieldViewModeWhileEditing;
account.delegate = self;
[self addSubview:account];
self.account = account;
CNLiveLoginTextField *code = [[CNLiveLoginTextField alloc] initWithFrame:CGRectMake(country.left, account.bottom+space, frame.size.width/2.0, rowHeight)];
code.keyboardType = UIKeyboardTypeNumberPad;
code.placeholder = @"请输入验证码";
code.textColor = [UIColor colorWithRed:51/255.0 green:51/255.0 blue:51/255.0 alpha:1.0];
code.placeholderColor = [UIColor colorWithRed:173/255.0 green:173/255.0 blue:173/255.0 alpha:1.0];
code.font = [UIFont systemFontOfSize:14];
code.textAlignment = NSTextAlignmentLeft;
[code addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
code.clearButtonMode = UITextFieldViewModeWhileEditing;
code.delegate = self;
[self addSubview:code];
self.code = code;
CNLiveCountDownButton *countDown = [CNLiveCountDownButton buttonWithType:UIButtonTypeCustom];
countDown.dataSource = self;
countDown.delegate = self;
countDown.frame = CGRectMake(frame.size.width-80, account.bottom+space+10, 80, rowHeight-10);
UIImage *image2 = [self xw_getImageName:@"login_get_code" bundleName:@"CNLiveLoginModule" targetClass:[self class]];
[countDown setImage:image2 forState:UIControlStateNormal];
countDown.adjustsImageWhenHighlighted = NO;
[countDown setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
countDown.titleLabel.font = [UIFont systemFontOfSize:12.0];
countDown.layer.cornerRadius = 15;
countDown.layer.masksToBounds = YES;
[self addSubview:countDown];
self.countDown = countDown;
UIButton *login = [UIButton buttonWithType:UIButtonTypeCustom];
login.frame = CGRectMake(5, code.bottom+space, frame.size.width-10, rowHeight);
[login setTitle:@"登录" forState:UIControlStateNormal];
[login setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
login.titleLabel.font = [UIFont systemFontOfSize:15.0];
login.backgroundColor = [UIColor colorWithRed:35/255.0 green:212/255.0 blue:30/255.0 alpha:1.0];
login.layer.cornerRadius = 20;
login.layer.masksToBounds = YES;
login.adjustsImageWhenHighlighted = NO;
[login addTarget:self action:@selector(loginDidClicked:) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:login];
self.login = login;
}
#pragma mark - UITextFieldDelegate
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
if ([textField isEqual:_account] || [textField isEqual:_code]) {
return YES;
}
else {
return YES;
}
}
- (void)textFieldDidChange:(UITextField *)textField {
if (![_account.text isEqualToString:@""] && ![_code.text isEqualToString:@""]) {
_login.enabled = YES;
}
else {
_login.enabled = NO;
}
}
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
if ([textField isEqual:_account]) {
NSInteger maxTextLength = 20;
if(string.length == 0) {
//判断是不是删除键
return YES;
}
else if(textField.text.length >= maxTextLength) {
NSLog(@"输入的字符个数大于20,忽略输入");
return NO;
}
else if (![self isNum:string]) {
return NO;
}
else if (textField.text.length + string.length > maxTextLength){
NSInteger length = textField.text.length;
NSInteger needLen = maxTextLength - length;
textField.text = [textField.text stringByAppendingString:[string substringToIndex:needLen]];
return NO;
}
else{
return YES;
}
}
else if ([textField isEqual:self.code]){
NSInteger maxTextLength = 6;
if(string.length == 0) {
//判断是不是删除键
return YES;
}
else if(textField.text.length >= maxTextLength) {
NSLog(@"输入的字符个数大于6,忽略输入");
return NO;
}
else if (![self isNum:string]) {
return NO;
}
else if (textField.text.length + string.length > maxTextLength){
NSInteger length = textField.text.length;
NSInteger needLen = maxTextLength - length;
textField.text = [textField.text stringByAppendingString:[string substringToIndex:needLen]];
return NO;
}
else{
return YES;
}
}
else{
return YES;
}
}
/** 判断是否为纯数字 */
- (BOOL)isNum:(NSString *)checkedNumString {
checkedNumString = [checkedNumString stringByTrimmingCharactersInSet:[NSCharacterSet decimalDigitCharacterSet]];
if(checkedNumString.length > 0) {
return NO;
}
return YES;
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
// Drawing code
}
*/
#pragma mark - XWLoginCountryViewDelegate
- (void)selectCountryName:(NSString *)countryName countryCode:(NSString *)countryCode{
[_account resignFirstResponder];
[_code resignFirstResponder];
if(![_countryCode isEqualToString:countryCode]){
_countryCode = countryCode;
}
}
#pragma mark - XWCountDownButton DataSource
// 设置倒计时总秒数
- (NSInteger)startCountDownNumOfCountDownButton:(CNLiveCountDownButton *)countdownButton {
return 60;
}
#pragma mark - XWCountdownButton Delegate
// 倒计时按钮点击
- (void)countDownButtonDidClick:(CNLiveCountDownButton *)button {
//获取验证码
__weak typeof(self) weakSelf = self;
if ([_account.text isEqualToString:@""]) {
// [XWAlertView alertViewWithFlexTextMessage:@"请输入手机号" view:nil];
return;
}
// 按钮点击后将enabled设置为NO
button.userInteractionEnabled = NO;
// [XWUserRequest verificationCodeWithType:XWCodeTypeShortcutLogin mobile:_account.text countryCode:_countryCode success:^(id _Nonnull result) {
// __strong typeof(weakSelf) strongSelf = weakSelf;
// if(!strongSelf) return;
// // 获取的验证码
// strongSelf->_verificationCode = @"666666";
//
// [XWAlertView alertSucceedViewWithTextMessage:@"获取成功" view:nil];
// button.backgroundColor = HexColor(0xdedede);
// button.frame = CGRectMake(self.frame.size.width-110, self.account.bottom+space+10, 110, rowHeight-10);
// [button setImage:nil forState:UIControlStateNormal];
// NSString *title = [NSString stringWithFormat:@"%d秒后重新发送", 60];
// [button setTitle:title forState:UIControlStateNormal];
// [button startCountDown];
//
// } failure:^(NSDictionary * _Nonnull errorDic) {
// __strong typeof(weakSelf) strongSelf = weakSelf;
// if(!strongSelf) return;
// button.userInteractionEnabled = YES;
// [XWAlertView alertErrorViewWithTextMessage:@"获取失败" view:nil];
//
// }];
}
// 倒计时进行中
- (void)countDownButtonDidCountDown:(CNLiveCountDownButton *)button countDownNum:(NSInteger)countDownNum {
if(button.frame.size.width != 110){
button.frame = CGRectMake(self.frame.size.width-110, self.account.bottom+space+10, 110, rowHeight-10);
}
NSString *title = [NSString stringWithFormat:@"%ld秒后重新发送", countDownNum];
[button setTitle:title forState:UIControlStateNormal];
}
// 倒计时结束
- (void)countDownButtonDidEndCountDown:(CNLiveCountDownButton *)button {
button.frame = CGRectMake(self.frame.size.width-80, self.account.bottom+space+10, 80, rowHeight-10);
button.backgroundColor = [UIColor whiteColor];
button.userInteractionEnabled = YES;
[button setTitle:@"" forState:UIControlStateNormal];
UIImage *image2 = [self xw_getImageName:@"login_get_code" bundleName:@"CNLiveLoginModule" targetClass:[self class]];
[button setImage:image2 forState:UIControlStateNormal];
}
#pragma mark - Action
- (void)loginDidClicked:(UIButton *)btn{
//登录
_verificationCode = @"666666";
if ([_account.text isEqualToString:@""]) {
// [XWAlertView alertViewWithFlexTextMessage:@"请输入手机号" view:self.superview];
return;
}
if ([_code.text isEqualToString:@""]) {
// [XWAlertView alertViewWithFlexTextMessage:@"请输入验证码" view:self.superview];
return;
}
btn.userInteractionEnabled = NO;
// 隐藏键盘
[self.superview endEditing:YES];
__weak typeof(self) weakSelf = self;
// [XWUserRequest shortcutLoginWithUserName:_account.text verificationCode:_verificationCode countryCode:_countryCode fromId:@"apple" success:^(id _Nonnull result) {
// btn.userInteractionEnabled = YES;
// __strong typeof(weakSelf) strongSelf = weakSelf;
// if(!strongSelf) return;
// [XWUserManager defaultConfiguration:result];
// // YES 新用户 NO 老用户
// BOOL isNewUser = YES;
// if (strongSelf.delegate && [strongSelf.delegate respondsToSelector:@selector(loginSuccess:isNewUser:result:)]) {
// [strongSelf.delegate loginSuccess:strongSelf isNewUser:isNewUser result:result];
// }
//
// } failure:^(NSDictionary * _Nonnull errorDic) {
// btn.userInteractionEnabled = YES;
// __strong typeof(weakSelf) strongSelf = weakSelf;
// if(!strongSelf) return;
//// [XWAlterView alertErrorViewWithTextMessage:@"登录失败" view:strongSelf.superview];
//
// // ------ 最后删除 ------
// NSDictionary *dic = @{@"token":@"用户token",@"id":@"用户id",@"tel":@"17600922519",@"faceUrl":@"http://pic1.win4000.com/wallpaper/c/53cdd1f7c1f21.jpg",@"name":@"张璟熙",@"gender":@"0",@"address":@"北京市海淀区"};
// [XWUserManager defaultConfiguration:dic];
// BOOL isNewUser = YES;
// if (strongSelf.delegate && [strongSelf.delegate respondsToSelector:@selector(loginSuccess:isNewUser:result:)]) {
// [strongSelf.delegate loginSuccess:strongSelf isNewUser:isNewUser result:errorDic];
// }
// // ------ 最后删除 ------
//
// }];
}
/**
* 创建图片
*
* @param imageName 图片名字
* @param bundleName 图片所在的bundle名字
* @param targetClass 类和bundle的同级目录
* @return 返回UIImage
*/
- (UIImage *)xw_getImageName:(NSString *)imageName bundleName:(NSString *)bundleName targetClass:(Class)targetClass{
NSBundle *bundle = [NSBundle bundleForClass:targetClass];
NSURL *url = [bundle URLForResource:bundleName withExtension:@"bundle"];
NSBundle *targetBundle = [NSBundle bundleWithURL:url];
UIImage *image = [UIImage imageNamed:imageName inBundle:targetBundle compatibleWithTraitCollection:nil];
return image?image:[UIImage imageNamed:imageName inBundle:[NSBundle mainBundle] compatibleWithTraitCollection:nil];
}
@end