LookAllController.m
5.3 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
//
// LookAllController.m
// DreamSleep
//
// Created by peter on 2022/5/5.
//
#import "LookAllController.h"
#import "SafeSleepListController.h"
@interface LookAllController () <UIScrollViewDelegate>
@property (nonatomic, strong) UIView *headView;
@property (nonatomic, strong) UIScrollView *bodyView;
@end
@implementation LookAllController {
UIView *_indicatorView;
NSArray *_btns;
NSInteger _selectedIndex;
}
- (instancetype)initWithDefaultIndex:(NSInteger)defaultIndex {
if (self = [super init]) {
_selectedIndex = defaultIndex;
self.navigationItem.title = _selectedIndex == 0 ? @"舒眠课程" : @"助眠音乐";
}
return self;
}
- (void)viewDidLoad {
[super viewDidLoad];
self.view.dk_backgroundColorPicker = DKColorPickerWithKey(VCViewBG);
[self.view addSubview:self.headView];
[self.view addSubview:self.bodyView];
[[NSNotificationCenter defaultCenter] postNotificationName:NeedPauseAllNoise object:nil];
}
#pragma mark - 品牌模式
- (NaviStyle)navigationBarStyle {
return NaviStyleDefault;
}
#pragma mark - Actions
- (void)adjustUI {
for (UIButton *btn in _btns) {
if (_selectedIndex == btn.tag) {
[btn.titleLabel setFont:BoldFont(16)];
[btn dk_setTitleColorPicker:DKColorPickerWithColors(MainTextColor, ColorFromHex(0xE8E9E9), DSWhite) forState:UIControlStateNormal];
} else {
[btn.titleLabel setFont:SysFont(15)];
[btn dk_setTitleColorPicker:DKColorPickerWithColors(ColorFromHexA(0x333333, .49), ColorFromHex(0x7A7F8E), DSWhite) forState:UIControlStateNormal];
}
}
self.navigationItem.title = _selectedIndex == 0 ? @"舒眠课程" : @"助眠音乐";
}
- (void)switchViewControllerAction:(UIButton *)sender {
_selectedIndex = sender.tag;
[UIView animateWithDuration:.3 animations:^{
self->_indicatorView.centerX = sender.centerX;
[self->_bodyView setContentOffset:CGPointMake(sender.tag * self->_bodyView.width, 0)];
}];
[self adjustUI];
}
- (UIButton *)btnWithTitle:(NSString *)title font:(UIFont *)font tag:(NSInteger)tag sView:(UIView *)sView {
UIButton *btn = [UIButton btnWithTitle:title font:font];
btn.tag = tag;
[btn addTarget:self action:@selector(switchViewControllerAction:) forControlEvents:UIControlEventTouchUpInside];
[sView addSubview:btn];
return btn;
}
- (void)addSubControllerView {
SafeSleepListController *safeVC = [[SafeSleepListController alloc] initWithCourseType:CourseTypeSafe];
safeVC.view.frame = CGRectMake(0, 0, kScreenWidth, self.bodyView.height);
[self.bodyView addSubview:safeVC.view];
[self addChildViewController:safeVC];
SafeSleepListController *relaxVC = [[SafeSleepListController alloc] initWithCourseType:CourseTypeRelax];
relaxVC.view.frame = CGRectMake(kScreenWidth, 0, kScreenWidth, self.bodyView.height);
[self.bodyView addSubview:relaxVC.view];
[self addChildViewController:relaxVC];
}
#pragma mark - UIScrollViewDelegate
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
NSInteger index = scrollView.contentOffset.x / scrollView.width;
[scrollView setContentOffset:CGPointMake(index*scrollView.width, 0)];
_selectedIndex = index;
UIButton *btn = _btns[index];
[UIView animateWithDuration:.3 animations:^{
self->_indicatorView.centerX = btn.centerX;
}];
[self adjustUI];
}
#pragma mark - lazy
- (UIView *)headView {
if (!_headView) {
_headView = [[UIView alloc] initWithFrame:CGRectMake(15, 0, kScreenWidth - 30, 40)];
_indicatorView = [UIView new];
_indicatorView.backgroundColor = BrandColor;
[_headView addSubview:_indicatorView];
UIButton *btn1 = [self btnWithTitle:@"舒眠课程" font:BoldFont(16) tag:0 sView:_headView];
UIButton *btn2 = [self btnWithTitle:@"助眠音乐" font:SysFont(15) tag:1 sView:_headView];
[btn1 mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(_headView);
make.bottom.equalTo(_headView).offset(1);
make.size.mas_equalTo(CGSizeMake(70, 30));
}];
[btn2 mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(btn1.mas_right).offset(26);
make.centerY.equalTo(btn1);
make.size.equalTo(btn1);
}];
[_indicatorView mas_makeConstraints:^(MASConstraintMaker *make) {
make.width.equalTo(@64);
make.centerX.equalTo(_selectedIndex == 0 ? btn1 : btn2);
make.height.equalTo(@4);
make.bottom.equalTo(_headView).offset(-4);
}];
_btns = @[btn1, btn2];
[self adjustUI];
}
return _headView;
}
- (UIScrollView *)bodyView {
if (!_bodyView) {
_bodyView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(_headView.frame), kScreenWidth, kScreenHeight - kTopHeight(0) - _headView.height)];
_bodyView.contentSize = CGSizeMake(2 * kScreenWidth, _bodyView.height);
_bodyView.pagingEnabled = YES;
_bodyView.showsHorizontalScrollIndicator = NO;
_bodyView.delegate = self;
[_bodyView setContentOffset:CGPointMake(_selectedIndex * _bodyView.width, 0)];
[self addSubControllerView];
}
return _bodyView;
}
@end