NoisePlayView.m
7.2 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
//
// NoisePlayView.m
// DreamSleep
//
// Created by peter on 2022/5/13.
//
#import "NoisePlayView.h"
#import "NoisePlayCell.h"
#import "NoisePlayerManager.h"
@interface NoisePlayView () <UITableViewDelegate, UITableViewDataSource, NoisePlayCellDelegate>
@property (nonatomic, strong) UIView *headView;
@property (nonatomic, strong) NSArray *audioArr;
@property (nonatomic, strong) UITableView *playListView;
@property (nonatomic, strong) UIButton *oneClickBtn;
@end
@implementation NoisePlayView
- (instancetype)init {
if (self = [super initWithFrame:[UIScreen mainScreen].bounds]) {
self.dk_backgroundColorPicker = DKColorPickerWithColors(ColorFromHexA(0x161E38, .6), ColorFromHexA(0x161E38, .6), DSWhite);
[self addSubview:self.headView];
[self addSubview:self.playListView];
[self addSubview:self.oneClickBtn];
[self.oneClickBtn mas_makeConstraints:^(MASConstraintMaker *make) {
make.centerX.equalTo(self);
make.size.mas_equalTo(CGSizeMake(155, 40));
make.bottom.equalTo(self).offset(-Bottom_SafeArea_Height-50);
}];
}
return self;
}
- (void)showNoisePlayViewWith:(BOOL)selected {
[DSKeyWindow addSubview:self];
NSArray *playCellList = [NoisePlayerManager sharedNoisePlayerManager].playItemList;
if (playCellList) {
self.audioArr = playCellList;
[self.playListView reloadData];
}
self.oneClickBtn.selected = selected;
}
- (void)setSelected:(BOOL)selected {
_selected = selected;
self.oneClickBtn.selected = selected;
}
#pragma mark - Actions
- (void)oneClickAction:(UIButton *)sender {
sender.selected = !sender.selected;
if (sender.selected) {
[[NoisePlayerManager sharedNoisePlayerManager] playAll];
} else {
[[NoisePlayerManager sharedNoisePlayerManager] pauseAll];
}
}
- (void)clearAllAction:(UIButton *)sender {
[[NoisePlayerManager sharedNoisePlayerManager] removeAllNoisePlayItem];
self.audioArr = @[];
[self.playListView reloadData];
[self removeFromSuperview];
}
- (void)closeAction:(UIButton *)sender {
[self removeFromSuperview];
}
#pragma mark - UITableViewDelegate && UITableViewDataSource
- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return self.audioArr.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *cellIdentifier = @"NoisePlayCellID";
NoisePlayCell *cell = (NoisePlayCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (!cell) {
cell = [[NoisePlayCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
cell.delegate = self;
NoisePlayItem *item = self.audioArr[indexPath.row];
cell.item = item;
cell.indexPath = indexPath;
return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return 77;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
return 0.001;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
return [[UIView alloc] initWithFrame:CGRectMake(0, 0, 10, 0.001)];
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
return 93 + Bottom_SafeArea_Height;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
return [[UIView alloc] initWithFrame:CGRectMake(0, 0, 10, 93 + Bottom_SafeArea_Height)];
}
#pragma mark - NoisePlayCellDelegate
- (void)didRemoveItem:(NSIndexPath *)indexPath {
NoisePlayItem *item = self.audioArr[indexPath.row];
// 删除数据源
NSMutableArray *tmpArr = [NSMutableArray arrayWithArray:self.audioArr];
[tmpArr removeObjectAtIndex:indexPath.row];
self.audioArr = [tmpArr copy];
[[NoisePlayerManager sharedNoisePlayerManager] removeNoisePlayItemWithAudioID:item.noise_audio_id];
// 执行删除动画
[self.playListView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationLeft];
dispatch_after(.3, dispatch_get_main_queue(), ^{
[self.playListView reloadData];
});
if (self.audioArr.count == 0) { [self removeFromSuperview]; }
}
- (void)didSetVolume:(float)volume indexPath:(NSIndexPath *)indexPath {
NoisePlayItem *item = self.audioArr[indexPath.row];
item.volume = volume;
}
#pragma mark - lazy
- (UIView *)headView {
if (!_headView) {
_headView = [[UIView alloc] initWithFrame:CGRectMake(0, 44, self.width, 60)];
_headView.dk_backgroundColorPicker = DKColorPickerWithKey(TabBarBG);
[_headView setCornerRadiusRect:(UIRectCornerTopLeft | UIRectCornerTopRight) cornerRadius:24];
UIButton *clearAllBtn = [UIButton btnWithTitle:@"清除全部" titleColor:BrandColor font:SysFont(12.0)];
[clearAllBtn cornerRadius:12.0];
clearAllBtn.layer.borderColor = BrandColor.CGColor;
clearAllBtn.layer.borderWidth = 1.0;
[clearAllBtn addTarget:self action:@selector(clearAllAction:) forControlEvents:UIControlEventTouchUpInside];
[_headView addSubview:clearAllBtn];
UIButton *closeBtn = [UIButton new];
[closeBtn dk_setImage:DKImagePickerWithNames(@"home_close", @"dk_home_close", @"home_close") forState:UIControlStateNormal];
[closeBtn addTarget:self action:@selector(closeAction:) forControlEvents:UIControlEventTouchUpInside];
[_headView addSubview:closeBtn];
[clearAllBtn mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(_headView).offset(15);
make.size.mas_equalTo(CGSizeMake(67, 26));
make.centerY.equalTo(_headView);
}];
[closeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
make.right.equalTo(_headView).offset(-15);
make.size.mas_equalTo(CGSizeMake(30, 30));
make.centerY.equalTo(_headView);
}];
}
return _headView;
}
- (UITableView *)playListView {
if (!_playListView) {
CGFloat y = CGRectGetMaxY(self.headView.frame);
_playListView = [[UITableView alloc] initWithFrame:CGRectMake(0, y, self.width, self.height - y) style:UITableViewStyleGrouped];
_playListView.showsVerticalScrollIndicator = NO;
_playListView.bounces = NO;
_playListView.delegate = self;
_playListView.dataSource = self;
_playListView.separatorStyle = UITableViewCellSeparatorStyleNone;
_playListView.dk_backgroundColorPicker = DKColorPickerWithKey(TabBarBG);
}
return _playListView;
}
- (UIButton *)oneClickBtn {
if (!_oneClickBtn) {
_oneClickBtn = [UIButton btnWithTitle:@"" titleColor:DSWhite font:BoldFont(16) bgColor:BrandColor];
[_oneClickBtn addTarget:self action:@selector(oneClickAction:) forControlEvents:UIControlEventTouchUpInside];
[_oneClickBtn cornerRadius:20];
[_oneClickBtn setTitle:@"一键开启" forState:UIControlStateNormal];
[_oneClickBtn setTitle:@"一键暂停" forState:UIControlStateSelected];
}
return _oneClickBtn;
}
- (NSArray *)audioArr {
if (!_audioArr) {
_audioArr = [NSArray array];
}
return _audioArr;
}
@end