RelaxTrainController.m
5.7 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
//
// RelaxTrainController.m
// DreamSleep
//
// Created by peter on 2022/6/1.
//
#import "RelaxTrainController.h"
#import "RelaxTrainRequestModel.h"
#import "RelaxBodyView.h"
@interface RelaxTrainController () <RelaxBodyViewDelegate, UIScrollViewDelegate>
@property (nonatomic, strong) UIImageView *bgIV;
@property (nonatomic, strong) UILabel *cusTitleLab;
@property (nonatomic, strong) NSArray *listArr;
@property (nonatomic, strong) UIScrollView *lucencyView;
@property (nonatomic, strong) RelaxBodyView *bodyView;
@end
@implementation RelaxTrainController
- (void)viewDidLoad {
[super viewDidLoad];
self.view.dk_backgroundColorPicker = DKColorPickerWithKey(VCViewBG);
[self.view addSubview:self.bgIV];
[self.view addSubview:self.lucencyView];
[self.lucencyView addSubview:self.bodyView];
[self.view addSubview:self.dsNaviBar];
[self.view addSubview:self.dkBackBtn];
[self.dkBackBtn dk_setImage:DKImagePickerWithNames(@"cus_back_icon", @"cus_back_icon", @"dk_cus_back_icon") forState:UIControlStateNormal];
[self.view addSubview:self.cusTitleLab];
CGFloat iv_h = kScreenWidth*1233/1125.0;
[self.bgIV mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.left.right.equalTo(self.view);
make.height.equalTo(@(iv_h));
}];
[self.cusTitleLab mas_makeConstraints:^(MASConstraintMaker *make) {
make.centerX.equalTo(self.dsNaviBar);
make.bottom.equalTo(self.dsNaviBar).offset(-9);
}];
[self.lucencyView mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.left.right.bottom.equalTo(self.view);
}];
[RelaxTrainRequestModel queryRelaxAudioListWithCompletion:^(RelaxTrainRequestModel * _Nonnull requestModel) {
if (requestModel.resCode == DSResCodeSuccess) {
if (requestModel.trainAudioList.count) {
self.listArr = requestModel.trainAudioList;
if (self.listArr && self.listArr.count > 0) {
NSInteger currentIndex = [self getIndexWithListData:self.listArr];
[self.bodyView refreshData:self.listArr currentIndex:currentIndex];
}
}
}
}];
[[NSNotificationCenter defaultCenter] postNotificationName:NeedPauseAllNoise object:nil];
[DataStatisticsUtil event:RelaxTrain attributes:@{@"data":@"腹式放松法页面"}];
}
#pragma mark - 根据title匹配下标索引
- (NSInteger)getIndexWithListData:(NSArray *)listData {
__block NSInteger currentIndex = 0;
NSString *title = self.params[@"title"];
// 放松音频随机选一个
if ([title isEqualToString:@"练习放松训练"]) {
currentIndex = arc4random() % listData.count;
} else {
[listData enumerateObjectsUsingBlock:^(RelaxTrainModel * obj, NSUInteger idx, BOOL * _Nonnull stop) {
if ([obj.audio_name containsString:title]) {
currentIndex = idx;
*stop = YES;
}
}];
}
return currentIndex;
}
#pragma mark - Actions
- (void)backAction {
[super backAction];
[self.bodyView stopAudio];
if (self.refreshDelegate && [self.refreshDelegate respondsToSelector:@selector(reloadAIPage)]) {
[self.refreshDelegate reloadAIPage];
}
[self.navigationController popViewControllerAnimated:YES];
}
#pragma mark - RelaxBodyViewDelegate
- (void)updateNaviTitleAndBgImg:(RelaxTrainModel *)trainModel {
[self.bgIV setImageWithURL:[NSURL URLWithString:trainModel.bg_url] placeholderImage:[UIImage imageNamed:@"basicPlaceholder"]];
self.cusTitleLab.text = trainModel.audio_name;
self.titleLab.text = trainModel.audio_name;
}
#pragma mark - UIScrollViewDelegate
- (void)scrollViewDidScroll:(UIScrollView*)scrollView {
CGFloat offset = scrollView.contentOffset.y;
if (offset >= self.lucencyView.contentSize.height - kScreenHeight) {
self.dkBackBtn.hidden = YES;
self.dsNaviBar.alpha = 1;
self.titleLab.hidden = NO;
self.cusTitleLab.hidden = YES;
self.isNeedUpdateStatusBarStyle = YES;
}
if (offset <= 0) {
self.dkBackBtn.hidden = NO;
self.dsNaviBar.alpha = 0;
self.titleLab.hidden = YES;
self.cusTitleLab.hidden = NO;
self.isNeedUpdateStatusBarStyle = NO;
}
}
#pragma mark - lazy
- (UIImageView *)bgIV {
if (!_bgIV) {
_bgIV = [UIImageView new];
}
return _bgIV;
}
- (UILabel *)cusTitleLab {
if (!_cusTitleLab) {
_cusTitleLab = [UILabel labWithTextColor:MainTextColor font:BoldFont(18)];
_cusTitleLab.textAlignment = NSTextAlignmentCenter;
}
return _cusTitleLab;
}
- (NSArray *)listArr {
if (!_listArr) {
_listArr = [NSArray array];
}
return _listArr;
}
- (UIScrollView *)lucencyView {
if (!_lucencyView) {
_lucencyView = [UIScrollView new];
_lucencyView.showsVerticalScrollIndicator = NO;
_lucencyView.delegate = self;
CGFloat bodyTopMargin = 351;
CGFloat bodyH = 420 + Bottom_SafeArea_Height;
_lucencyView.contentSize = CGSizeMake(kScreenWidth, bodyH + bodyTopMargin);
_lucencyView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
}
return _lucencyView;
}
- (RelaxBodyView *)bodyView {
if (!_bodyView) {
_bodyView = [[RelaxBodyView alloc] initWithFrame:CGRectMake(0, 351, kScreenWidth, 420 + Bottom_SafeArea_Height)];
_bodyView.delegate = self;
_bodyView.updateParams = self.params;
}
return _bodyView;
}
#pragma mark - 设置状态栏文字颜色(重写父类)
- (UIStatusBarStyle)preferredStatusBarStyle {
if (self.isNeedUpdateStatusBarStyle) {
return UIStatusBarStyleLightContent;
}
return UIStatusBarStyleDefault;
}
@end