CNLiveLoginInputView.m
7.99 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
//
// CNLiveLoginInputView.m
// CNLiveScioLive
//
// Created by CNLive-zxw on 2018/9/20.
// Copyright © 2018年 CNLive. All rights reserved.
//
#import "CNLiveLoginInputView.h"
@interface CNLiveLoginInputView()<UITextFieldDelegate>
@property (nonatomic, strong) UIImageView *picImage;
@property (nonatomic, strong, readwrite) UITextField *textField;
@property (nonatomic, strong) UIButton *show;
@property (nonatomic, strong) UIImageView *line;
@property (nonatomic, assign) CNLiveButtonType type;
@end
@implementation CNLiveLoginInputView
- (instancetype)initWithPic:(NSString *)pic placeholder:(NSString *)placeholder type:(CNLiveButtonType)type{
self = [super init];
if(self){
_type = type;
[self setUpUI];
_picImage.image = [UIImage imageNamed:pic];
_textField.placeholder = placeholder;
switch (type) {
case CNLiveButtonTypeNormal:
{
_show.hidden = YES;
}
break;
case CNLiveButtonTypeTextEntry:
{
_show.hidden = NO;
_textField.secureTextEntry = YES;
// [_show setTitle:CustomStr(@"Show") forState:UIControlStateNormal];
// [_show setTitle:CustomStr(@"Hide") forState:UIControlStateSelected];
[_show setImage:[UIImage imageNamed:@"cnlive_show"] forState:UIControlStateNormal];
[_show setImage:[UIImage imageNamed:@"cnlive_hide"] forState:UIControlStateSelected];
[_show addTarget:self action:@selector(clickedShowBtn:) forControlEvents:UIControlEventTouchUpInside];
}
break;
case CNLiveButtonTypeCode:
{
_show.hidden = NO;
[_show setTitleColor:HEXCOLOR(0xc81e19) forState:UIControlStateNormal];
_show.layer.borderColor = HEXCOLOR(0xc81e19).CGColor;
[_show setTitle:CustomStr(@"GetCode") forState:UIControlStateNormal];
[_show addTarget:self action:@selector(clickedCodeBtn:) forControlEvents:UIControlEventTouchUpInside];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(countdown) name:GetCodeSuccessful object:nil];
}
break;
default:
break;
}
}
return self;
}
- (instancetype)initWithFrame:(CGRect)frame{
self = [super initWithFrame:frame];
if(self){
[self setUpUI];
}
return self;
}
- (void)setUpUI{
_picImage = [[UIImageView alloc] init];
_picImage.contentMode = UIViewContentModeCenter;
_picImage.layer.masksToBounds = YES;
[self addSubview:_picImage];
_textField = [[UITextField alloc]init];
_textField.delegate = self;
_textField.backgroundColor = [UIColor whiteColor];
_textField.font = [UIFont systemFontOfSize:15];
[_textField setValue:HEXCOLOR(0xebebeb) forKeyPath:@"_placeholderLabel.textColor"];
[self addSubview:_textField];
_show = [UIButton buttonWithType:UIButtonTypeCustom];
_show.backgroundColor = [UIColor whiteColor];
// _show.layer.masksToBounds = YES;
// _show.layer.cornerRadius = 4.0f;
// _show.layer.borderColor = HEXCOLOR(0x333333).CGColor;
// [_show setTitleColor:HEXCOLOR(0x333333) forState:UIControlStateNormal];
// _show.layer.borderWidth = 1.0f;
// _show.highlighted = NO;
// _show.titleLabel.font = [UIFont systemFontOfSize:12];
[self addSubview:_show];
_line = [[UIImageView alloc] init];
_line.backgroundColor = Color_Line;
[self addSubview:_line];
}
- (void)layoutSubviews{
[_picImage mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(self.mas_top).with.offset(10);
make.left.equalTo(self.mas_left).with.offset(10);
make.width.offset(20);
make.height.offset(20);
}];
[_textField mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(self.mas_top).with.offset(0);
make.bottom.equalTo(self.mas_bottom).with.offset(0);
make.left.equalTo(self->_picImage.mas_right).with.offset(10);
make.right.equalTo(self.mas_right).with.offset(-10-40);
}];
switch (_type) {
case CNLiveButtonTypeNormal:
{
_show.hidden = YES;
}
break;
case CNLiveButtonTypeTextEntry:
{
[_show mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(self.mas_top).with.offset(8);
make.bottom.equalTo(self.mas_bottom).with.offset(-8);
make.right.equalTo(self.mas_right).with.offset(-10);
make.width.offset(40);
}];
}
break;
case CNLiveButtonTypeCode:
{
_show.layer.masksToBounds = YES;
_show.layer.cornerRadius = 4.0f;
_show.layer.borderColor = HEXCOLOR(0x333333).CGColor;
[_show setTitleColor:HEXCOLOR(0x333333) forState:UIControlStateNormal];
_show.layer.borderWidth = 1.0f;
_show.highlighted = NO;
_show.titleLabel.font = [UIFont systemFontOfSize:12];
[_show mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(self.mas_top).with.offset(8);
make.bottom.equalTo(self.mas_bottom).with.offset(-8);
make.right.equalTo(self.mas_right).with.offset(-10);
make.width.offset(70);
}];
}
break;
default:
break;
}
[_line mas_makeConstraints:^(MASConstraintMaker *make) {
make.bottom.equalTo(self.mas_bottom).with.offset(-1);
make.height.offset(0.5);
make.left.equalTo(self.mas_left).with.offset(0);
make.right.equalTo(self.mas_right).with.offset(0);
}];
}
#pragma mark - Action
- (void)clickedShowBtn:(UIButton *)sender{
//避免明文/密文切换后光标位置偏移
self.textField.enabled = NO; // the first one;
self.textField.secureTextEntry = sender.selected;
sender.selected = !sender.selected;
self.textField.enabled = YES; // the second one;
[self.textField becomeFirstResponder]; // the third one
sender.backgroundColor = [UIColor whiteColor];
}
- (void)clickedCodeBtn:(UIButton *)sender{
self.block?self.block():@"";
}
- (void)countdown{
__block NSInteger time = 59; //倒计时时间
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_source_t _timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue);
dispatch_source_set_timer(_timer,dispatch_walltime(NULL, 0),1.0*NSEC_PER_SEC, 0); //每秒执行
dispatch_source_set_event_handler(_timer, ^{
if(time <= 0){ //倒计时结束,关闭
dispatch_source_cancel(_timer);
dispatch_async(dispatch_get_main_queue(), ^{
//设置按钮的样式
[self.show setTitle:@"重新发送" forState:UIControlStateNormal];
[self.show setTitleColor:HEXCOLOR(0x333333) forState:UIControlStateNormal];
self.show.userInteractionEnabled = YES;
});
}else{
int seconds = time % 60;
dispatch_async(dispatch_get_main_queue(), ^{
//设置按钮显示读秒效果
[self.show setTitle:[NSString stringWithFormat:@"%.2ds", seconds] forState:UIControlStateNormal];
[self.show setTitleColor:HEXCOLOR(0x979797) forState:UIControlStateNormal];
self.show.userInteractionEnabled = NO;
});
time--;
}
});
dispatch_resume(_timer);
}
#pragma mark - UITextFieldDelegate
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField{
return YES;
}
- (void)textFieldDidEndEditing:(UITextField *)textField{
}
- (void)dealloc{
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
@end