TimingView.m
7.8 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
//
// TimingView.m
// DreamSleep
//
// Created by peter on 2022/5/9.
//
#import "TimingView.h"
@interface TimingView () <UIPickerViewDataSource, UIPickerViewDelegate>
@property (nonatomic, strong) UIView *bgView;
@property (nonatomic, strong) UIButton *closeBtn;
@property (nonatomic, strong) UILabel *titleLab;
@property (nonatomic, strong) UILabel *audioTimeLab;
@property (nonatomic, strong) UIPickerView *timePickerView;
@property (nonatomic, strong) UILabel *minuteLab;
@property (nonatomic, strong) UIButton *cancelBtn;
@property (nonatomic, strong) UIButton *sureBtn;
@property (nonatomic, strong) UIView *lineView;
@property (nonatomic, strong) NSArray *minuteDatas;
@property (nonatomic, assign) NSInteger currentTimeIndex;
@property (nonatomic, assign) NSInteger sureIndex;
@end
@implementation TimingView
- (instancetype)initWithSureBlock:(TimingBlock)timingBlock cancel:(CancelBlock)cancelBlock defalutIndex:(NSInteger)defalutIndex {
if (self = [super initWithFrame:[UIScreen mainScreen].bounds]) {
_currentTimeIndex = defalutIndex;
_sureIndex = defalutIndex;
_timingBlock = timingBlock;
_cancelBlock = cancelBlock;
self.dk_backgroundColorPicker = DKColorPickerWithColors(ColorFromHex(0x6F7587), DSClearColor, DSWhite);
self.backgroundColor = [self.backgroundColor colorWithAlphaComponent:0.6];
NSMutableArray *tmpArr = [NSMutableArray array];
for (int i = 1; i < 121; i++) {
[tmpArr addObject:[NSString stringWithFormat:@"%d", i]];
}
self.minuteDatas = [tmpArr copy];
[self addSubview:self.bgView];
[self.bgView addSubview:self.titleLab];
[self.bgView addSubview:self.closeBtn];
[self.bgView addSubview:self.audioTimeLab];
[self.bgView addSubview:self.timePickerView];
[self.bgView addSubview:self.minuteLab];
[self.bgView addSubview:self.cancelBtn];
[self.bgView addSubview:self.sureBtn];
[self.bgView addSubview:self.lineView];
[self.bgView mas_makeConstraints:^(MASConstraintMaker *make) {
make.size.mas_equalTo(CGSizeMake(280, 236));
make.center.equalTo(self);
}];
[self.titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
make.centerX.equalTo(self.bgView);
make.top.equalTo(self.bgView).offset(20);
}];
[self.closeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
make.centerY.equalTo(self.titleLab);
make.top.equalTo(self.bgView).offset(15);
make.right.equalTo(self.bgView).offset(-15);
}];
[self.audioTimeLab mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(self.bgView).offset(63);
make.top.equalTo(self.bgView).offset(97);
}];
[self.timePickerView mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(self.audioTimeLab.mas_right);
make.right.equalTo(self.minuteLab.mas_left);
make.top.equalTo(self.titleLab.mas_bottom).offset(22);
make.bottom.equalTo(self.sureBtn.mas_top).offset(-22);
}];
[self.minuteLab mas_makeConstraints:^(MASConstraintMaker *make) {
make.centerY.equalTo(self.audioTimeLab);
make.right.equalTo(self.bgView).offset(-63);
}];
[self.cancelBtn mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(self.bgView);
make.bottom.equalTo(self.bgView);
make.height.equalTo(@64);
}];
[self.sureBtn mas_makeConstraints:^(MASConstraintMaker *make) {
make.right.equalTo(self.bgView);
make.bottom.equalTo(self.bgView);
make.size.equalTo(self.cancelBtn);
make.left.equalTo(self.cancelBtn.mas_right);
}];
[self.lineView mas_makeConstraints:^(MASConstraintMaker *make) {
make.centerX.equalTo(self.bgView);
make.centerY.equalTo(self.sureBtn);
make.size.mas_equalTo(CGSizeMake(1, 40));
}];
}
return self;
}
#pragma mark - Actions
- (void)display {
[DSKeyWindow addSubview:self];
self.hidden = NO;
[self.timePickerView selectRow:self.sureIndex inComponent:0 animated:NO];
}
- (void)dismissTimingView {
self.hidden = YES;
}
- (void)sureAction {
[self dismissTimingView];
self.sureIndex = self.currentTimeIndex;
if (self.timingBlock) {
self.timingBlock(self.sureIndex, self.minuteDatas);
}
}
- (void)cancelAction {
[self dismissTimingView];
if (self.cancelBlock) {
self.cancelBlock();
}
}
#pragma mark - UIPickerViewDataSource && UIPickerViewDelegate
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView*)pickerView {
return 1;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
return self.minuteDatas.count;
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
self.currentTimeIndex = row;
}
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view {
UILabel *lab = [UILabel labWithTextColor:BrandColor font:SysFont(16)];
lab.frame = CGRectMake(0.0, 0.0, 70, 35);
lab.textAlignment = NSTextAlignmentCenter;
lab.text = self.minuteDatas[row];
if (@available(iOS 14.0, *)) {
pickerView.subviews[1].backgroundColor = DSClearColor;
}
return lab;
}
#pragma mark - lazy
- (UIView *)bgView {
if (!_bgView) {
_bgView = [UIView new];
_bgView.dk_backgroundColorPicker = DKColorPickerWithKey(TabBarBG);
[_bgView cornerRadius:24];
}
return _bgView;
}
- (UILabel *)titleLab {
if (!_titleLab) {
_titleLab = [UILabel dkLabWithText:@"设置时间" font:BoldFont(16)];
_titleLab.textAlignment = NSTextAlignmentCenter;
}
return _titleLab;
}
- (UIButton *)closeBtn {
if (!_closeBtn) {
_closeBtn = [UIButton new];
[_closeBtn dk_setImage:DKImagePickerWithNames(@"home_close", @"dk_home_close", @"home_close") forState:UIControlStateNormal];
[_closeBtn addTarget:self action:@selector(dismissTimingView) forControlEvents:UIControlEventTouchUpInside];
}
return _closeBtn;
}
- (UILabel *)audioTimeLab {
if (!_audioTimeLab) {
_audioTimeLab = [UILabel dkLabWithText:@"音频时长" font:SysFont(15)];
_audioTimeLab.textAlignment = NSTextAlignmentCenter;
}
return _audioTimeLab;
}
- (UIPickerView *)timePickerView {
if (!_timePickerView) {
_timePickerView = [UIPickerView new];
_timePickerView.delegate = self;
_timePickerView.dataSource = self;
}
return _timePickerView;
}
- (UILabel *)minuteLab {
if (!_minuteLab) {
_minuteLab = [UILabel dkLabWithText:@"分钟" font:SysFont(15)];
_minuteLab.textAlignment = NSTextAlignmentCenter;
}
return _minuteLab;
}
- (UIButton *)cancelBtn {
if (!_cancelBtn) {
_cancelBtn = [UIButton dkBtnTitle:@"取消设置" font:SysFont(15)];
[_cancelBtn dk_setTitleColorPicker:DKColorPickerWithKey(TEXT) forState:UIControlStateNormal];
[_cancelBtn addTarget:self action:@selector(cancelAction) forControlEvents:UIControlEventTouchUpInside];
}
return _cancelBtn;
}
- (UIButton *)sureBtn {
if (!_sureBtn) {
_sureBtn = [UIButton btnWithTitle:@"确定" titleColor:BrandColor font:SysFont(15)];
[_sureBtn addTarget:self action:@selector(sureAction) forControlEvents:UIControlEventTouchUpInside];
}
return _sureBtn;
}
- (UIView *)lineView {
if (!_lineView) {
_lineView = [UIView new];
_lineView.dk_backgroundColorPicker = DKColorPickerWithColors(ColorFromHex(0xE6E6E6), DarkColor, DSWhite);
}
return _lineView;
}
@end