//
//  NvHomeCViewCell.m
//  SDKDemo
//
//  Created by ms20180425 on 2018/11/15.
 //
//

#import "NvHomeCViewCell.h"
#import "NvHeader.h"

@implementation NvHomeArrayModel

@end

@implementation NvHomeModel

@end

@interface NvHomeCViewCell()

@property (nonatomic, strong) UILabel *name;         //显示的文字
@property (nonatomic, strong) UIImageView *cover;    //封面
@property (nonatomic, strong) UIView *colorBg;       //渐变色背景
@end

@implementation NvHomeCViewCell

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        self.colorBg = [[UIView alloc]initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.width)];
        [self.contentView addSubview:self.colorBg];
        
        self.cover = [[UIImageView alloc]init];
        self.cover.contentMode = UIViewContentModeScaleAspectFit;
        [self.contentView addSubview:self.cover];
        [self.cover mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(self.colorBg.mas_centerX);
            make.centerY.equalTo(self.colorBg.mas_centerY);
            make.width.offset(30 * SCREANSCALE);
            make.height.offset(30 * SCREANSCALE);
        }];
        
        self.name = [[UILabel alloc]init];
        self.name.textColor = [UIColor nv_colorWithHexARGB:@"#CCFFFFFF"];
        self.name.font = [NvUtils regularFontWithSize:14];
        [self.contentView addSubview:self.name];
        [self.name mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.colorBg.mas_bottom).offset(8 * SCREANSCALE);
            make.centerX.equalTo(self.colorBg.mas_centerX);
        }];
    }
    return self;
}

- (void)renderCellWithItem:(NvHomeModel *)item {
    self.name.text = item.name;
    self.cover.image = [NvUtils imageWithName:item.cover];
    [self gradientView:self.colorBg withColors:item.color];
}


- (void)gradientView:(UIView *)sender withColors:(NSArray *)colors{
    //创建CAGradientLayer 对象
    CAGradientLayer *gradientLayer = [CAGradientLayer layer];
    //设置CAGradientLayer 对象的位置大小和承接视图等同
    gradientLayer.frame = CGRectMake(0, 0, sender.frame.size.width, sender.frame.size.height);
    //设置渐变色(即颜色数组)
    gradientLayer.colors = colors;
    //变化位置或变化点
    gradientLayer.locations = @[@(0.0f),@(1.0f)];
    //渐变方向
    gradientLayer.startPoint = CGPointMake(0.5, 0);
    gradientLayer.endPoint = CGPointMake(0.5, 1);
    gradientLayer.masksToBounds = YES;
    gradientLayer.cornerRadius = 10 * SCREANSCALE;
    //添加
    [sender.layer addSublayer:gradientLayer];
}

@end
