// // horizonScroll.m // TheoryNetwork // // Created by tederen on 2019/9/20. // Copyright © 2019 tederen. All rights reserved. // #import "HorizonScroll.h" #import "ChannelModel.h" #import "HomeScrCell.h" @interface HorizonScroll () { UIButton *_selectButton; BOOL invalidType; } @property (nonatomic, copy) NSArray *channelArr; @property (nonatomic, strong) TDButton *addButton; @end @implementation HorizonScroll - (instancetype)initWithFrame:(CGRect)frame { if (self = [super initWithFrame:frame]) { [self addSubview:self.collectionView]; [self addSubview:self.addButton]; [self.addButton mas_makeConstraints:^(MASConstraintMaker *make) { make.top.right.bottom.mas_equalTo(self); make.width.mas_offset(44); }]; [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) { make.left.top.bottom.mas_equalTo(self); make.right.mas_equalTo(self.addButton.mas_left); }]; } return self; } - (UICollectionView *)collectionView{ if (!_collectionView) { UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init]; // 垂直方向滑动 flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal; _collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:flowLayout]; _collectionView.delegate = self; _collectionView.dataSource = self; _collectionView.showsVerticalScrollIndicator = NO; _collectionView.showsHorizontalScrollIndicator = NO; _collectionView.backgroundColor = [UIColor whiteColor]; [_collectionView registerNib:[UINib nibWithNibName:@"HomeScrCell" bundle:nil] forCellWithReuseIdentifier:@"HomeScrCellID"]; } return _collectionView; } - (void)setChannelArray:(NSArray *)channelArr { _channelArr = channelArr; [self.collectionView reloadData]; } - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section { return self.channelArr.count; } -(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{ HomeScrCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"HomeScrCellID" forIndexPath:indexPath]; ChannelModel * model = self.channelArr[indexPath.item]; [cell setDataWithTitle:model.ArticleGroupName isSelect:model.IsSelect]; return cell; } - (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewFlowLayout *)layout insetForSectionAtIndex:(NSInteger)section { return UIEdgeInsetsMake(0, 15, 0, 15); } - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath { ChannelModel * model = self.channelArr[indexPath.item]; if (self.ClickSelectChannelBlock) { self.ClickSelectChannelBlock(model.ArticleGroupId); } } - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath { ChannelModel * model = self.channelArr[indexPath.item]; UILabel * label = [UILabel new]; label.font = [UIFont systemFontOfSize:15.f]; label.text = model.ArticleGroupName; CGSize size = [label sizeThatFits:CGSizeMake(SCREEN_WIDTH, 45)]; return CGSizeMake(size.width, 45); } - (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section { return 15.f; } - (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section { return 30.f; } - (void)addChannelAction:(UIButton *)sender { if (self.ClickAddChannelBlock) { self.ClickAddChannelBlock(); } } #pragma mark - setter - (UIButton *)addButton { if (!_addButton) { _addButton = [TDButton buttonWithType:UIButtonTypeCustom]; [_addButton setImage:IMG(@"icon_circleAdd") forState:UIControlStateNormal]; [_addButton addTarget:self action:@selector(addChannelAction:) forControlEvents:UIControlEventTouchUpInside]; [_addButton setCurrentButtonHotSize:CGSizeZero]; } return _addButton; } @end