// // XPHomeLiveHeadView.m // xplan-ios // // Created by GreenLand on 2022/7/18. // #import "XPHomeLiveHeadView.h" #import "HomeRecommendRoomModel.h" ///Third #import #import "ThemeColor.h" #import "XPMacro.h" ///View #import "XPHomeLiveRecordCell.h" #import "StatisticsServiceHelper.h" @interface XPHomeLiveHeadView() @property (nonatomic, strong) UIImageView *bgImageView; @property (nonatomic, strong) UIImageView *iconImageView; @property (nonatomic, strong) UILabel *titleLabel; ///列表 @property (nonatomic,strong) UICollectionView *collectionView; @end @implementation XPHomeLiveHeadView - (instancetype)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { [self initSubViews]; [self initSubViewConstraints]; self.clipsToBounds = YES; } return self; } #pragma mark - Public Method + (CGFloat)getHomeLiveHeaderViewHeight:(NSArray *)recommendList { CGFloat totaHeight = 0; if (recommendList.count) { totaHeight = 127 + 8; } return totaHeight; } #pragma mark - Private Method - (void)initSubViews { [self addSubview:self.bgImageView]; [self addSubview:self.iconImageView]; [self addSubview:self.titleLabel]; [self addSubview:self.collectionView]; } - (void)initSubViewConstraints { [self.bgImageView mas_makeConstraints:^(MASConstraintMaker *make) { make.left.mas_equalTo(15); make.right.mas_equalTo(-15); make.top.mas_equalTo(8); make.bottom.mas_equalTo(0); }]; [self.iconImageView mas_makeConstraints:^(MASConstraintMaker *make) { make.left.mas_equalTo(self.bgImageView).mas_offset(14); make.top.mas_equalTo(self.bgImageView).mas_offset(6); make.width.height.mas_equalTo(16); }]; [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) { make.left.mas_equalTo(self.iconImageView.mas_right).mas_offset(4); make.centerY.mas_equalTo(self.iconImageView); }]; [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) { make.left.right.bottom.mas_equalTo(self.bgImageView); make.top.mas_equalTo(self.iconImageView.mas_bottom).mas_offset(8); }]; } #pragma mark - UICollectionViewCellDelegate And UICollectionViewDatasource - (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { XPHomeLiveRecordCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([XPHomeLiveRecordCell class]) forIndexPath:indexPath]; cell.recordRoom = [self.roomList objectAtIndex:indexPath.item]; return cell; } - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section { return self.roomList.count; } - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath { [collectionView deselectItemAtIndexPath:indexPath animated:YES]; if (self.roomList.count > 0) { HomeRecommendRoomModel * liveModel = [self.roomList objectAtIndex:indexPath.item]; if (self.delegate && [self.delegate respondsToSelector:@selector(xPHomeLiveHeadViewDidClickItem:)]) { [StatisticsServiceHelper trackEventWithKey:StatisticsServiceEventRecent_card_click eventAttributes:@{@"roomUid" : liveModel.uid}]; [self.delegate xPHomeLiveHeadViewDidClickItem:liveModel]; } } } #pragma mark - Getters And Setters - (void)setRoomList:(NSMutableArray *)roomList { _roomList = roomList; [self.collectionView reloadData]; } - (void)setHadHistoryRecord:(BOOL)hadHistoryRecord { if (hadHistoryRecord) { self.bgImageView.image = [UIImage imageNamed:@"home_live_record_bg"]; self.titleLabel.text = @"曾经看过的人"; } else { self.bgImageView.image = [UIImage imageNamed:@"home_live_hot_bg"]; self.titleLabel.text = @"Top热播"; } } - (UICollectionView *)collectionView{ if (!_collectionView) { UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init]; layout.scrollDirection = UICollectionViewScrollDirectionHorizontal; layout.minimumLineSpacing = 12; layout.itemSize = CGSizeMake(62, 80); layout.sectionInset = UIEdgeInsetsMake(0, 15, 0, 15); _collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout]; _collectionView.dataSource = self; _collectionView.delegate = self; _collectionView.backgroundColor = [UIColor clearColor]; [_collectionView registerClass:[XPHomeLiveRecordCell class] forCellWithReuseIdentifier:NSStringFromClass([XPHomeLiveRecordCell class])]; _collectionView.showsHorizontalScrollIndicator = NO; } return _collectionView; } - (UIImageView *)bgImageView { if (!_bgImageView) { _bgImageView = [[UIImageView alloc] init]; _bgImageView.image = [UIImage imageNamed:@"home_live_record_bg"]; } return _bgImageView; } - (UIImageView *)iconImageView { if (!_iconImageView) { _iconImageView = [[UIImageView alloc] init]; _iconImageView.image = [UIImage imageNamed:@"home_live_record_icon"]; } return _iconImageView; } - (UILabel *)titleLabel { if (!_titleLabel) { _titleLabel = [[UILabel alloc] init]; _titleLabel.font = [UIFont systemFontOfSize:14 weight:UIFontWeightMedium]; _titleLabel.textColor = [ThemeColor mainTextColor]; _titleLabel.text = @"曾经看过的人"; } return _titleLabel; } @end