RankHeadView.m 9.3 KB
//
//  RankHeadView.m
//  DreamSleep
//
//  Created by peter on 2022/7/2.
//

#import "RankHeadView.h"

@interface RankHeadView ()
@property (nonatomic, strong) UIImageView *rankIV;
@property (nonatomic, strong) NSArray *btns;
@property (nonatomic, strong) UIImageView *markerIV;
@property (nonatomic, assign) NSInteger curIndex;
@property (nonatomic, strong) UIView *otherView;
@property (nonatomic, strong) UILabel *myNameLab;
@property (nonatomic, strong) UILabel *myRankLab;
@property (nonatomic, strong) UILabel *myScoreLab;
@end

@implementation RankHeadView

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        self.curIndex = 0;
        
        [self addSubview:self.rankIV];
        
        UIButton *weekBtn = [self buildBtnWithTitle:@"周排行榜" tag:0];
        UIButton *totalBtn = [self buildBtnWithTitle:@"月排行榜" tag:1];
        self.btns = @[weekBtn, totalBtn];
        [self addSubview:self.markerIV];
        [self adjustMarkLayout];
        
        [self addSubview:self.otherView];
        
        CGFloat rankIV_H = kScreenWidth * 147 / 375;
        [self.rankIV mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.left.right.equalTo(self);
            make.height.equalTo(@(rankIV_H));
        }];
        [weekBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.rankIV.mas_bottom);
            make.left.equalTo(self);
            make.height.equalTo(@40);
        }];
        [totalBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(weekBtn);
            make.right.equalTo(self);
            make.size.equalTo(weekBtn);
            make.left.equalTo(weekBtn.mas_right);
        }];
        [self.otherView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(weekBtn.mas_bottom);
            make.left.right.equalTo(self);
            make.height.equalTo(@138);
        }];
        [self layoutIfNeeded];
        self.height = CGRectGetMaxY(self.otherView.frame);
    }
    return self;
}

- (UIButton *)buildBtnWithTitle:(NSString *)title tag:(NSInteger)tag {
    WS(weakSelf);
    UIButton *btn = [UIButton new];
    btn.tag = tag;
    [btn addTouchUpInsideHandler:^(NSInteger tag) {
        weakSelf.curIndex = tag;
        [weakSelf adjustMarkLayout];
        if (weakSelf.rankItemBlock) {
            weakSelf.rankItemBlock(tag);
        }
    }];
    [btn setTitle:title forState:UIControlStateNormal];
    [btn dk_setBackgroundColorPicker:DKColorPickerWithColors(ColorFromHex(0x4670FF), ColorFromHex(0x2341A3), DSWhite)];
    [self addSubview:btn];
    
    return btn;
}

- (void)adjustMarkLayout {
    [self.btns enumerateObjectsUsingBlock:^(UIButton * obj, NSUInteger idx, BOOL * _Nonnull stop) {
        if (self.curIndex == idx) {
            [obj setTitleColor:DSWhite forState:UIControlStateNormal];
            [obj.titleLabel setFont:BoldFont(16.0)];
        } else {
            [obj setTitleColor:ColorFromHexA(0xFFFFFF, .8) forState:UIControlStateNormal];
            [obj.titleLabel setFont:SysFont(14.0)];
        }
    }];
    UIButton *btn = self.btns[self.curIndex];
    [UIView animateWithDuration:.8 animations:^{
        [self.markerIV mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(btn);
            make.bottom.equalTo(btn);
        }];
    }];
}

- (void)updateMyRankView:(RankModel *)myRankModel {
    self.myNameLab.text = myRankModel.user_name;
    self.myRankLab.text = [NSString stringWithFormat:@"第%ld名", myRankModel.rank];
    self.myScoreLab.text = [NSString stringWithFormat:@"%ld", myRankModel.points];
}

#pragma mark - lazy
- (UIImageView *)rankIV {
    if (!_rankIV) {
        _rankIV = [[UIImageView alloc] dk_initWithImagePicker:DKImagePickerWithNames(@"bg_jf_paihangbang", @"dk_bg_jf_paihangbang", @"bg_jf_paihangbang")];
    }
    return _rankIV;
}

- (UIImageView *)markerIV {
    if (!_markerIV) {
        _markerIV = [[UIImageView alloc] dk_initWithImagePicker:DKImagePickerWithNames(@"ic_jf_select", @"dk_ic_jf_select", @"ic_jf_select")];
        _markerIV.size = CGSizeMake(10, 5);
    }
    return _markerIV;
}

- (UIView *)otherView {
    if (!_otherView) {
        _otherView = [UIView new];
        
        UIView *shadowView = [UIView new];
        shadowView.dk_backgroundColorPicker = DKColorPickerWithColors(DSWhite, ColorFromHex(0x1F263F), DSWhite);
        [shadowView cornerRadius:12.0];
        shadowView.layer.dk_shadowColorPicker = DKColorPickerWithColors(ColorFromRGBA(162.0, 239.0, 254.0, 1.0), ColorFromRGBA(162.0, 239.0, 254.0, .2), DSWhite);
        shadowView.layer.shadowOffset = CGSizeMake(0, 1);
        shadowView.layer.shadowOpacity = 1;
        shadowView.layer.shadowRadius = 4;
        [_otherView addSubview:shadowView];
        
        // 我的成绩
        UILabel *myScoreTitleLab = [UILabel labWithText:@"我的\n成绩" textColor:BrandColor font:SysFont(14.0)];
        myScoreTitleLab.numberOfLines = 0;
        [shadowView addSubview:myScoreTitleLab];
        // 我的头像
        UIImageView *myHeadIV = [UIImageView new];
        [myHeadIV cornerRadius:20.0];
        myHeadIV.dk_alphaPicker = DKAlphaPickerWithAlphas(1, .5, .5);
        [myHeadIV yy_setImageWithURL:[NSURL URLWithString:[LoginUtils getFaceImg]] placeholder:[UIImage imageNamed:@"basicPlaceholder"]];
        [shadowView addSubview:myHeadIV];
        // 我的名称
        UILabel *myNameLab = [UILabel labWithFont:SysFont(14.0)];
        myNameLab.dk_textColorPicker = DKColorPickerWithColors(SubTitleColor, ColorFromHexA(0xFFFFFF, .5), DSWhite);
        [shadowView addSubview:myNameLab];
        self.myNameLab = myNameLab;
        // 我的排名
        UILabel *myRankLab = [UILabel labWithTextColor:BrandColor font:SysFont(14.0)];
        myRankLab.textAlignment = NSTextAlignmentRight;
        [shadowView addSubview:myRankLab];
        self.myRankLab = myRankLab;
        // 分割线
        UIView *lineView = [UIView new];
        lineView.backgroundColor = ColorFromHex(0xF5F5F5);
        [shadowView addSubview:lineView];
        // 我的积分
        UILabel *myScoreLab = [UILabel labWithTextColor:BrandColor font:SysFont(14.0)];
        myScoreLab.textAlignment = NSTextAlignmentCenter;
        [shadowView addSubview:myScoreLab];
        self.myScoreLab = myScoreLab;
        
        UILabel *rankLab = [UILabel labWithText:@"排名" font:SysFont(14.0) fit:YES];
        rankLab.dk_textColorPicker = DKColorPickerWithColors(SmallTextColor, ColorFromHexA(0xFFFFFF, .3), DSWhite);
        [_otherView addSubview:rankLab];
        
        UILabel *userLab = [UILabel labWithText:@"用户" font:SysFont(14.0) fit:YES];
        userLab.dk_textColorPicker = DKColorPickerWithColors(SmallTextColor, ColorFromHexA(0xFFFFFF, .3), DSWhite);
        [_otherView addSubview:userLab];
        
        UILabel *scoreLab = [UILabel labWithText:@"积分" font:SysFont(14.0) fit:YES];
        scoreLab.textAlignment = NSTextAlignmentCenter;
        scoreLab.dk_textColorPicker = DKColorPickerWithColors(SmallTextColor, ColorFromHexA(0xFFFFFF, .3), DSWhite);
        [_otherView addSubview:scoreLab];
        
        [shadowView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(_otherView).offset(24);
            make.left.equalTo(_otherView).offset(15);
            make.right.equalTo(_otherView).offset(-15);
            make.height.equalTo(@64);
        }];
        [myScoreTitleLab mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(shadowView).offset(20);
            make.centerY.equalTo(shadowView);
        }];
        [myHeadIV mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.height.equalTo(@40);
            make.centerY.equalTo(myScoreTitleLab);
            make.left.equalTo(myScoreTitleLab.mas_right).offset(20);
        }];
        [myNameLab mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(myHeadIV);
            make.left.equalTo(myHeadIV.mas_right).offset(14);
        }];
        [myRankLab mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(myHeadIV);
            make.left.equalTo(myNameLab.mas_right).offset(20);
            make.right.equalTo(lineView.mas_left).offset(-20);
        }];
        [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(shadowView);
            make.top.equalTo(shadowView).offset(12);
            make.right.equalTo(shadowView).offset(-64);
            make.width.equalTo(@1);
        }];
        [myScoreLab mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(myHeadIV);
            make.right.equalTo(shadowView);
            make.left.equalTo(lineView.mas_right);
        }];
        [rankLab mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(_otherView).offset(35);
            make.top.equalTo(shadowView.mas_bottom).offset(24);
        }];
        [userLab mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(rankLab);
            make.left.equalTo(rankLab.mas_right).offset(26);
        }];
        [scoreLab mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(rankLab);
            make.right.equalTo(_otherView).offset(-15);
            make.width.equalTo(@64);
        }];
    }
    return _otherView;
}

@end