CNLiveServiceLookCell.m 4.75 KB
//
//  CNLiveServiceLookCell.m
//  CNLiveCServiceModule
//
//  Created by CNLive on 2021/11/27.
//

#import "CNLiveServiceLookCell.h"
#import "CNLiveCycleView.h"

@interface CNLiveServiceLookCell()
@property (nonatomic, strong) UIImageView *backImage;
@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UILabel *subTitlelabel;
@property (nonatomic, strong) CNLiveCycleView *cycleView;
@end

@implementation CNLiveServiceLookCell
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self.contentView addSubview:self.backImage];
        [self.backImage addSubview:self.titleLabel];
        [self.backImage addSubview:self.subTitlelabel];
        [self.backImage addSubview:self.cycleView];
       
    }
    return self;
}
- (void)layoutSubviews
{
    [super layoutSubviews];
    [self.backImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.equalTo(self.contentView);
        make.left.equalTo(self.contentView).offset(10);
        make.right.equalTo(self.contentView).offset(-10);
    }];
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.backImage).offset(10);
        make.top.equalTo(self.backImage).offset(15);
    }];
    [self.subTitlelabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.titleLabel.mas_right).offset(10);
        make.bottom.equalTo(self.titleLabel);
    }];
    [self.cycleView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.backImage).offset(-10);
        make.left.equalTo(self.backImage).offset(10);
        make.right.equalTo(self.backImage).offset(-10);
        make.top.equalTo(self.titleLabel.mas_bottom).offset(15);
    }];
}
#pragma mark - Method
- (NSArray *)sqereList:(NSArray *)array{
    NSMutableArray *dataArr = [NSMutableArray array];
    NSMutableArray *sqArr = [NSMutableArray array];
    for (int i = 0; i < array.count; i ++) {
        NSDictionary *dic = array[i];
        CnLivePageContentModel *model = [CnLivePageContentModel mj_objectWithKeyValues:dic];
        [sqArr addObject:model];
        if ((i != 0 || array.count == 1) && (i%2 == 0 || i == array.count - 1)) {
            NSDictionary *category = @{
                @"title":@"大家都在用",
                @"lists":sqArr.copy,
                @"layout":@{
                    @"width":@(self.cycleView.width),
                    @"height":@((self.cycleView.height - 20)/3)
                }
            };
            CNLiveSearchHotsModel *model1 = [CNLiveSearchHotsModel mj_objectWithKeyValues:category];
            [dataArr addObject:model1];
            [sqArr removeAllObjects];
        }
    }
    return dataArr.copy;
}
#pragma mark - Setting&&Getting
- (void)setModel:(CNliveServicePageModel *)model
{
    _model = model;
    [self.backImage sd_setImageWithURL:[NSURL URLWithString:model.background]];
    NSArray *dataArr = [self sqereList:model.contents];
    _cycleView.dataSource = dataArr;
    _cycleView.localizationImageNamesGroup = dataArr;
    self.titleLabel.text = model.blockName;
    [self.titleLabel sizeToFit];
    self.subTitlelabel.text = model.blockSubTitle;
    [self.subTitlelabel sizeToFit];
    [self setNeedsLayout];
}
- (UIImageView *)backImage
{
    if (!_backImage) {
        _backImage = [[UIImageView alloc] init];
        _backImage.userInteractionEnabled = YES;
    }
    return _backImage;
}
- (UILabel *)titleLabel
{
    if (!_titleLabel) {
        _titleLabel = [[UILabel alloc] init];
        _titleLabel.font = UIFontMake(17);
        _titleLabel.textColor = CNLiveColorWithHexString(@"#1F74E9");
        _titleLabel.textAlignment = NSTextAlignmentLeft;
    }
    return _titleLabel;
}
- (UILabel *)subTitlelabel
{
    if (!_subTitlelabel) {
        _subTitlelabel = [[UILabel alloc] init];
        _subTitlelabel.font = UIFontMake(12);
        _subTitlelabel.textColor = CNLiveColorWithHexString(@"#1F74E9");
        _subTitlelabel.textAlignment = NSTextAlignmentLeft;
    }
    return _subTitlelabel;
}
- (CNLiveCycleView *)cycleView
{
    if (!_cycleView) {
        _cycleView = [[CNLiveCycleView alloc] initWithFrame:CGRectZero];
        _cycleView.layer.cornerRadius = 8;
        _cycleView.backgroundColor = kWhiteColor;
        _cycleView.autoScroll = NO;
        _cycleView.showPageControl = YES;
        _cycleView.pageControlStyle = SDCycleScrollViewPageContolStyleAnimated;
        _cycleView.pageControlAliment = SDCycleScrollViewPageContolAlimentCenter;
        _cycleView.hidesForSinglePage = YES;
        _cycleView.currentPageDotImage = [UIImage loadBundleImage:@"pagecontrol_yes"];
        _cycleView.pageDotImage = [UIImage loadBundleImage:@"pagecontrol_no"];
        _cycleView.pageControlDotSize = CGSizeMake(4, 4);
    }
    return _cycleView;
}
@end