CNLiveMineHeaderView.m 13.1 KB
//
//  CNLiveMineHeaderView.m
//  CNLiveMineModule
//
//  Created by 153993236@qq.com on 11/12/2019.
//  Copyright © 2019 153993236@qq.com. All rights reserved.
//

#import "CNLiveMineHeaderView.h"
#import <Masonry/Masonry.h>
#import <SDWebImage/SDWebImage.h>
#import "QMUIKit.h"
#import "CNLiveCategory.h"
#import "CNLiveBaseKit.h"

#import "CNUserInfoManager.h"
#import "CNLiveUserInfoNotification.h"

@interface CNLiveMineHeaderView ()
@property (nonatomic, strong) UIImageView *bgView;
@property (nonatomic, strong) UIButton *witness;

@property (nonatomic, strong) UIImageView *header;
@property (nonatomic, strong) UILabel *name;
@property (nonatomic, strong) UILabel *certification;
@property (nonatomic, strong) UILabel *address;
@property (nonatomic, strong) UIButton *sign;

@end

@implementation CNLiveMineHeaderView
static const NSInteger margin = 20;

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if(self){
        self.userInteractionEnabled = YES;
        self.backgroundColor = [UIColor colorWithRed:244.0/255.0 green:244.0/255.0 blue:244.0/255.0 alpha:1.0];
        [CNLiveUserInfoNotification addUserInfoUpdateObserver:self selector:@selector(userInfoUpdateNotification:)];
        [self createSubViews];
    }
    return self;
}
// 实现方法
- (void)userInfoUpdateNotification:(NSNotification *)noti{
    //使用userInfo处理消息
    NSDictionary *dic = [noti userInfo];
    CNLiveUserInfoDataType type = [[dic objectForKey:@"dataType"] integerValue];
    NSString *info = [dic objectForKey:@"info"];
    if(type == CNLiveUserInfoDataTypeHeader){
        UIImage *xw_image = [self getImageWithImageName:@"default_avatar_y" bundleName:@"CNLiveMineModule" targetClass:[CNLiveMineHeaderView class]];
        [_header sd_setImageWithURL:[NSURL URLWithString:info] placeholderImage:xw_image];

    }else if(type == CNLiveUserInfoDataTypeNickname){
        _name.attributedText = [self createAttributedImageName:@"mine_code" string:info font:16.0 isFront:NO];

    }else if(type == CNLiveUserInfoDataTypeAddress){
        _address.attributedText = [self createAttributedImageName:@"mine_location" string:info font:13.0 isFront:YES];

    }
}

- (void)dealloc {
    NSLog(@"CNLiveMineHeaderView -- dealloc");
    [CNLiveUserInfoNotification removeObserver:self];
}

- (void)createSubViews {
    [self addSubview:self.bgView];
    [self addSubview:self.witness];

    [self addSubview:self.header];
    [self addSubview:self.name];
    [self addSubview:self.certification];
    [self addSubview:self.address];
    [self addSubview:self.sign];

}

- (void)layoutSubviews{
    [super layoutSubviews];
    [_witness mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.mas_left).with.offset(1.5*margin);
        make.right.equalTo(self.mas_right).with.offset(-1.5*margin);
        make.width.height.offset(60);
        make.bottom.equalTo(self.mas_bottom).with.offset(-0.5*margin);
        
    }];
    
    [_bgView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.mas_left).with.offset(0);
        make.right.equalTo(self.mas_right).with.offset(-0);
        make.top.equalTo(self.mas_top).with.offset(0);
        make.bottom.equalTo(self.witness.mas_centerY).with.offset(0.5*margin);
        
    }];
    
    [_header mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.mas_left).with.offset(margin);
        make.centerY.equalTo(self.bgView.mas_centerY).with.offset(0);
        make.width.height.offset(60);

    }];
    
    [_certification mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.header.mas_centerY);
        make.left.equalTo(self.header.mas_right).with.offset(margin/2.0);
        make.height.offset(35);
        
    }];
    
    [_name mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.certification.mas_top);
        make.left.equalTo(self.certification.mas_left);
        make.right.mas_lessThanOrEqualTo(self.bgView.mas_right).with.offset(-margin);
        make.height.equalTo(self.certification.mas_height);
        
    }];
    
    [_address mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.certification.mas_bottom);
        make.left.equalTo(self.name.mas_left);
        make.right.equalTo(self.name.mas_right);
        make.height.equalTo(self.name.mas_height);
        
    }];

    [_sign mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.mas_right).with.offset(0);
        make.centerY.equalTo(self.header.mas_centerY).with.offset(0);
        make.width.mas_equalTo(105);
        make.height.mas_equalTo(32);
        
    }];
}

- (void)setInteractionEnabled:(BOOL)interactionEnabled{
    _interactionEnabled = interactionEnabled;
    _sign.userInteractionEnabled = interactionEnabled;
    _witness.userInteractionEnabled = interactionEnabled;
}

- (void)setIsRefresh:(BOOL)isRefresh{
    _isRefresh = isRefresh;
    if(isRefresh){
        _name.attributedText = [self createAttributedImageName:@"mine_code" string:CNUserShareModel.nickname font:16.0 isFront:NO];
        _address.attributedText = [self createAttributedImageName:@"mine_location" string:CNUserShareModel.location font:13.0 isFront:YES];
    }
}

/*
 // Only override drawRect: if you perform custom drawing.
 // An empty implementation adversely affects performance during animation.
 - (void)drawRect:(CGRect)rect {
 // Drawing code
 }
 */
#pragma mark - Private Methods
- (NSMutableAttributedString *)createAttributedImageName:(NSString *)imageName string:(NSString *)string font:(CGFloat)font isFront:(BOOL)isFront{
    if(isFront){
        string = [NSString stringWithFormat:@" %@",string];
    }
    // 1.创建一个富文本
    NSMutableAttributedString *attri =  [[NSMutableAttributedString alloc] initWithString:string];
    // 修改富文本中的不同文字的样式
//    [attri addAttribute:NSForegroundColorAttributeName value:[UIColor whiteColor] range:NSMakeRange(0, string.length)];
    [attri addAttribute:NSFontAttributeName value:UIFontCNMake(font) range:NSMakeRange(0, string.length)];
    // 2.添加表情图片
    NSTextAttachment *attch = [[NSTextAttachment alloc] init];
    // 表情图片
    UIImage *xw_image = [self getImageWithImageName:imageName bundleName:@"CNLiveMineModule" targetClass:[self class]];
    attch.image = xw_image;
    if(isFront){
        // 设置图片大小
        attch.bounds = CGRectMake(0, -2, 10, 12);
        // 创建带有图片的富文本
        NSAttributedString *str = [NSAttributedString attributedStringWithAttachment:attch];
        [attri insertAttributedString:str atIndex:0];// 插入某个位置
    }else{
        // 设置图片大小
        attch.bounds = CGRectMake(10, -2, 15, 15);
        // 创建带有图片的富文本
        NSAttributedString *str = [NSAttributedString attributedStringWithAttachment:attch];
        [attri insertAttributedString:str atIndex:string.length];// 插入某个位置

    }
    return attri;
    
}
- (NSMutableAttributedString *)createAttributedImages:(NSArray *)images {
    // 1.创建一个富文本
    NSMutableAttributedString *attri =  [[NSMutableAttributedString alloc] initWithString:@""];
    for(NSString *image in images){
        // 2.添加表情图片
        NSTextAttachment *attch = [[NSTextAttachment alloc] init];
        // 表情图片
        UIImage *xw_image = [self getImageWithImageName:image bundleName:@"CNLiveMineModule" targetClass:[CNLiveMineHeaderView class]];
        attch.image = xw_image;
        // 创建带有图片的富文本
        NSAttributedString *str = [NSAttributedString attributedStringWithAttachment:attch];
        NSAttributedString *space = [[NSAttributedString alloc]initWithString:@" "];

        [attri insertAttributedString:space atIndex:0];
        [attri insertAttributedString:str atIndex:0];// 插入某个位置

    }
    return attri;
    
}

#pragma mark - 响应方法
- (void)headerTapDidClicked:(UITapGestureRecognizer *)tap{
    if (self.delegate && [self.delegate respondsToSelector:@selector(headerView:pushToController:)]) {
        [self.delegate headerView:self pushToController:CNLiveMineHeaderViewTypeUerInfo];
    }
}

- (void)nameTapDidClicked:(UITapGestureRecognizer *)tap{
    if (self.delegate && [self.delegate respondsToSelector:@selector(headerView:pushToController:)]) {
        [self.delegate headerView:self pushToController:CNLiveMineHeaderViewTypeCode];
    }
}

- (void)signDidClicked:(UIButton *)btn{
    if (self.delegate && [self.delegate respondsToSelector:@selector(headerView:pushToController:)]) {
        btn.userInteractionEnabled = NO;
        [self.delegate headerView:self pushToController:CNLiveMineHeaderViewTypeSign];
    }
}

- (void)witnessDidClicked:(UIButton *)btn{
    if (self.delegate && [self.delegate respondsToSelector:@selector(headerView:pushToController:)]) {
        btn.userInteractionEnabled = NO;
        [self.delegate headerView:self pushToController:CNLiveMineHeaderViewTypeWintess];
    }
}

#pragma mark - 懒加载
- (UIButton *)witness {
    if (!_witness) {
        _witness = [UIButton buttonWithType:UIButtonTypeCustom];
        UIImage *xw_image = [self getImageWithImageName:@"mine_observer" bundleName:@"CNLiveMineModule" targetClass:[CNLiveMineHeaderView class]];
        _witness.imageView.contentMode = UIViewContentModeScaleAspectFit;
        [_witness setImage:xw_image forState:UIControlStateNormal];
        _witness.adjustsImageWhenHighlighted = NO;
        [_witness addTarget:self action:@selector(witnessDidClicked:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _witness;
}
- (UIImageView *)bgView {
    if (!_bgView) {
        _bgView = [[UIImageView alloc] init];
        _bgView.userInteractionEnabled = YES;
        UIImage *xw_image = [self getImageWithImageName:@"mine_bg" bundleName:@"CNLiveMineModule" targetClass:[CNLiveMineHeaderView class]];
        _bgView.image = xw_image;
    }
    return _bgView;
}
- (UIImageView *)header {
    if (!_header) {
        _header = [[UIImageView alloc] init];
        _header.userInteractionEnabled = YES;
        _header.layer.masksToBounds = YES;
        _header.layer.cornerRadius = 30;
        UIImage *xw_image = [self getImageWithImageName:@"default_avatar_y" bundleName:@"CNLiveMineModule" targetClass:[CNLiveMineHeaderView class]];
        [_header sd_setImageWithURL:[NSURL URLWithString:CNUserShareModel.faceUrl] placeholderImage:xw_image];
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(headerTapDidClicked:)];
        [_header addGestureRecognizer:tap];
    }
    return _header;
}

- (UILabel *)name {
    if (!_name) {
        _name = [[UILabel alloc] init];
        _name.userInteractionEnabled = YES;
        _name.numberOfLines = 1;
        _name.textColor = [UIColor whiteColor];
        _name.font = [UIFont systemFontOfSize:16];
        _name.attributedText = [self createAttributedImageName:@"mine_code" string:CNUserShareModel.nickname font:16.0 isFront:NO];
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(nameTapDidClicked:)];
        [_name addGestureRecognizer:tap];
        
    }
    return _name;
}
- (UILabel *)certification {
    if (!_certification) {
        _certification = [[UILabel alloc] init];
        _certification.numberOfLines = 1;
        _certification.textColor = [UIColor whiteColor];
        _certification.font = [UIFont systemFontOfSize:16];
        _certification.attributedText = [self createAttributedImages:@[@"mine_overbadge",@"mine_common", @"mine_wjj_certification"]];
    }
    return _certification;
}
- (UILabel *)address {
    if (!_address) {
        _address = [[UILabel alloc] init];
        _address.numberOfLines = 1;
        _address.textColor = [UIColor colorWithRed:140/255.0 green:140/255.0 blue:142/255.0 alpha:1.0];
        _address.font = [UIFont systemFontOfSize:13];
        _address.attributedText = [self createAttributedImageName:@"mine_location" string:CNUserShareModel.location font:13.0 isFront:YES];
    }
    return _address;
}
- (UIButton *)sign {
    if (!_sign) {
        _sign = [UIButton buttonWithType:UIButtonTypeCustom];
        _sign.frame = CGRectMake(0, 0, 105, 32);
        _sign.backgroundColor = [[UIColor whiteColor] colorWithAlphaComponent:0.2];
        [_sign setTitle:@"签到赚积分" forState:UIControlStateNormal];
        _sign.titleLabel.font = [UIFont systemFontOfSize:13.0];
        [_sign setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        UIImage *xw_image = [self getImageWithImageName:@"sign_in_soccer" bundleName:@"CNLiveMineModule" targetClass:[CNLiveMineHeaderView class]];
        [_sign setImage:xw_image forState:UIControlStateNormal];
        [_sign addTarget:self action:@selector(signDidClicked:) forControlEvents:UIControlEventTouchUpInside];
        UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:_sign.layer.bounds byRoundingCorners:UIRectCornerBottomLeft | UIRectCornerTopLeft cornerRadii:CGSizeMake(16, 16)];
        CAShapeLayer *maskLayer = [CAShapeLayer layer];
        maskLayer.frame = _sign.bounds;
        maskLayer.path = maskPath.CGPath;
        _sign.layer.mask = maskLayer;
    }
    return _sign;
}

@end