// // XPMonentsRecommendHeaderView.m // xplan-ios // // Created by 冯硕 on 2022/5/18. // #import "XPMonentsRecommendHeaderView.h" ///Third #import ///Model #import "MonentsTopicModel.h" ///View #import "XPMonentsTopicCollectionViewCell.h" @interface XPMonentsRecommendHeaderView () ///背景图 @property (nonatomic,strong) UIImageView *bgView; ///titile背景 @property (nonatomic,strong) UIImageView *titleBgView; ///显示标题 @property (nonatomic,strong) UILabel *titleLabel; ///显示箭头 //@property (nonatomic,strong) UIButton *arrowButton; ///列表背景 @property (nonatomic,strong) UIView *collectionBgView; ///列表 @property (nonatomic,strong) UICollectionView *collectionView; @end @implementation XPMonentsRecommendHeaderView - (instancetype)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { [self initSubViews]; [self initSubViewConstraints]; } return self; } #pragma mark - Private Method - (void)initSubViews { self.backgroundColor = [DJDKMIMOMColor appCellBackgroundColor]; [self addSubview:self.bgView]; [self.bgView addSubview:self.collectionBgView]; [self.collectionBgView addSubview:self.collectionView]; [self.bgView addSubview:self.titleBgView]; [self.titleBgView addSubview:self.titleLabel]; } - (void)initSubViewConstraints { [self.bgView mas_makeConstraints:^(MASConstraintMaker *make) { make.top.mas_equalTo(16); make.right.mas_offset(-15); make.left.mas_offset(15); make.bottom.mas_equalTo(-20); }]; // [self.collectionBgView mas_makeConstraints:^(MASConstraintMaker *make) { // make.top.mas_equalTo(10); // make.left.mas_equalTo(10); // make.right.mas_equalTo(-10); // make.bottom.mas_equalTo(-10); // }]; [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) { make.top.mas_equalTo(15); make.left.right.equalTo(self.collectionBgView); make.height.mas_equalTo(69); }]; [self.titleBgView mas_makeConstraints:^(MASConstraintMaker *make) { make.top.left.equalTo(self.bgView); make.width.mas_equalTo(74); make.height.mas_equalTo(21); }]; [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) { make.center.equalTo(self.titleBgView); }]; // [self.arrowButton mas_makeConstraints:^(MASConstraintMaker *make) { // make.size.mas_equalTo(CGSizeMake(11, 20)); // make.centerY.mas_equalTo(self.titleLabel); // make.right.mas_equalTo(self).offset(-15); // }]; } #pragma mark - UICollectionViewDelegate And UICollectionViewDatasource - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section { return self.topicList.count; } //- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath { // MonentsTopicModel * topicModel = [self.topicList safeObjectWithIndex:indexPath.row]; // CGFloat itemWidth = [topicModel.name boundingRectWithSize:CGSizeMake(100, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingTruncatesLastVisibleLine attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:10 weight:UIFontWeightBold]} context:nil].size.width; // return CGSizeMake(itemWidth +34, 26); //} - (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { XPMonentsTopicCollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([XPMonentsTopicCollectionViewCell class]) forIndexPath:indexPath]; cell.topicInfo = [self.topicList safeObjectAtIndex1:indexPath.row]; return cell; } - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath { [collectionView deselectItemAtIndexPath:indexPath animated:YES]; if (self.topicList.count > 0) { MonentsTopicModel * topicInfo = [self.topicList safeObjectAtIndex1:indexPath.row]; if(topicInfo == nil)return; if (self.delegate && [self.delegate respondsToSelector:@selector(xPMonentsRecommendHeaderView:didSelectItem:)]) { [self.delegate xPMonentsRecommendHeaderView:self didSelectItem:topicInfo]; } } } #pragma mark - Event Response - (void)arrowButtonAction:(UIButton *)sender { if (self.delegate && [self.delegate respondsToSelector:@selector(xPMonentsRecommendHeaderView:didClickMoreTopic:)]) { [self.delegate xPMonentsRecommendHeaderView:self didClickMoreTopic:sender]; } } #pragma mark - Getters And Setters - (void)setTopicList:(NSArray *)topicList { _topicList = topicList; [self.collectionView reloadData]; } - (UILabel *)titleLabel { if (!_titleLabel) { _titleLabel = [[UILabel alloc] init]; _titleLabel.font = [UIFont systemFontOfSize:15 weight:UIFontWeightMedium]; _titleLabel.textColor = UIColorFromRGB(0x0D344D); _titleLabel.text = YMLocalizedString(@"XPMonentsRecommendHeaderView0"); } return _titleLabel; } //- (UIButton *)arrowButton { // if (!_arrowButton) { // _arrowButton = [UIButton buttonWithType:UIButtonTypeCustom]; // [_arrowButton setImage:[UIImage imageNamed:@"room_setting_arrow"] forState:UIControlStateNormal]; // [_arrowButton setImage:[UIImage imageNamed:@"room_setting_arrow"] forState:UIControlStateSelected]; // [_arrowButton addTarget:self action:@selector(arrowButtonAction:) forControlEvents:UIControlEventTouchUpInside]; // } // return _arrowButton; //} - (UICollectionView *)collectionView{ if (!_collectionView) { UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init]; layout.sectionInset = UIEdgeInsetsMake(0,0, 0, 0); layout.itemSize = CGSizeMake((KScreenWidth - 50)/2, 18); layout.minimumLineSpacing = 0; layout.minimumInteritemSpacing = 0; layout.scrollDirection = UICollectionViewScrollDirectionHorizontal; _collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout]; _collectionView.dataSource = self; _collectionView.delegate = self; _collectionView.backgroundColor = [UIColor clearColor]; _collectionView.pagingEnabled = YES; _collectionView.showsHorizontalScrollIndicator = NO; [_collectionView registerClass:[XPMonentsTopicCollectionViewCell class] forCellWithReuseIdentifier:NSStringFromClass([XPMonentsTopicCollectionViewCell class])]; } return _collectionView; } - (UIView *)collectionBgView{ if (!_collectionBgView){ UIView *view = [[UIView alloc] init]; view.frame = CGRectMake(10,10,KScreenWidth - 50,89); view.layer.borderWidth = 0.5; view.layer.borderColor = [UIColor colorWithRed:255/255.0 green:255/255.0 blue:255/255.0 alpha:1.0].CGColor; view.layer.backgroundColor = [UIColor colorWithRed:255/255.0 green:255/255.0 blue:255/255.0 alpha:0.6300].CGColor; // blur UIBlurEffect *blurEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight]; UIVisualEffectView *visualView = [[UIVisualEffectView alloc]initWithEffect:blurEffect]; visualView.frame = CGRectMake(10,10,KScreenWidth - 50,89); view.layer.cornerRadius = 8; _collectionBgView = view; _collectionView.userInteractionEnabled = YES; } return _collectionBgView; } - (UIImageView *)bgView{ if (!_bgView){ _bgView = [UIImageView new]; UIImage *image = [UIImage gradientColorImageFromColors:@[UIColorFromRGB(0x9FF4F2),UIColorFromRGB(0x96D6FF)] gradientType:GradientTypeLeftToRight imgSize:CGSizeMake(KScreenWidth - 30, 109)]; _bgView.image = image; [_bgView setCornerWithLeftTopCorner:10 rightTopCorner:10 bottomLeftCorner:10 bottomRightCorner:10 size:CGSizeMake(KScreenWidth - 30, 109)]; _bgView.userInteractionEnabled = YES; } return _bgView; } -(UIImageView *)titleBgView{ if (!_titleBgView){ _titleBgView = [UIImageView new]; UIImage *image = [UIImage gradientColorImageFromColors:@[UIColorFromRGB(0x83E8E7),UIColorFromRGB(0x86D4F6)] gradientType:GradientTypeLeftToRight imgSize:CGSizeMake(74, 21)]; _titleBgView.image = image; [_titleBgView setCornerWithLeftTopCorner:10 rightTopCorner:0 bottomLeftCorner:0 bottomRightCorner:10 size:CGSizeMake(74, 21)]; _titleBgView.userInteractionEnabled = YES; } return _titleBgView; } @end