LookAllController.m 5.3 KB
//
//  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