HorizonScroll.m 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. //
  2. // horizonScroll.m
  3. // TheoryNetwork
  4. //
  5. // Created by tederen on 2019/9/20.
  6. // Copyright © 2019 tederen. All rights reserved.
  7. //
  8. #import "HorizonScroll.h"
  9. #import "ChannelModel.h"
  10. #import "HomeScrCell.h"
  11. @interface HorizonScroll ()<UICollectionViewDelegate, UICollectionViewDataSource ,UICollectionViewDelegateFlowLayout>
  12. {
  13. UIButton *_selectButton;
  14. BOOL invalidType;
  15. }
  16. @property (nonatomic, copy) NSArray *channelArr;
  17. @property (nonatomic, strong) TDButton *addButton;
  18. @end
  19. @implementation HorizonScroll
  20. - (instancetype)initWithFrame:(CGRect)frame
  21. {
  22. if (self = [super initWithFrame:frame]) {
  23. [self addSubview:self.collectionView];
  24. [self addSubview:self.addButton];
  25. [self.addButton mas_makeConstraints:^(MASConstraintMaker *make) {
  26. make.top.right.bottom.mas_equalTo(self);
  27. make.width.mas_offset(44);
  28. }];
  29. [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
  30. make.left.top.bottom.mas_equalTo(self);
  31. make.right.mas_equalTo(self.addButton.mas_left);
  32. }];
  33. }
  34. return self;
  35. }
  36. - (UICollectionView *)collectionView{
  37. if (!_collectionView) {
  38. UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
  39. // 垂直方向滑动
  40. flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
  41. _collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:flowLayout];
  42. _collectionView.delegate = self;
  43. _collectionView.dataSource = self;
  44. _collectionView.showsVerticalScrollIndicator = NO;
  45. _collectionView.showsHorizontalScrollIndicator = NO;
  46. _collectionView.backgroundColor = [UIColor whiteColor];
  47. [_collectionView registerNib:[UINib nibWithNibName:@"HomeScrCell" bundle:nil] forCellWithReuseIdentifier:@"HomeScrCellID"];
  48. }
  49. return _collectionView;
  50. }
  51. - (void)setChannelArray:(NSArray *)channelArr {
  52. _channelArr = channelArr;
  53. [self.collectionView reloadData];
  54. }
  55. - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
  56. {
  57. return self.channelArr.count;
  58. }
  59. -(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
  60. HomeScrCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"HomeScrCellID" forIndexPath:indexPath];
  61. ChannelModel * model = self.channelArr[indexPath.item];
  62. [cell setDataWithTitle:model.ArticleGroupName isSelect:model.IsSelect];
  63. return cell;
  64. }
  65. - (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewFlowLayout *)layout insetForSectionAtIndex:(NSInteger)section {
  66. return UIEdgeInsetsMake(0, 15, 0, 15);
  67. }
  68. - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
  69. {
  70. ChannelModel * model = self.channelArr[indexPath.item];
  71. if (self.ClickSelectChannelBlock) {
  72. self.ClickSelectChannelBlock(model.ArticleGroupId);
  73. }
  74. }
  75. - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
  76. {
  77. ChannelModel * model = self.channelArr[indexPath.item];
  78. UILabel * label = [UILabel new];
  79. label.font = [UIFont systemFontOfSize:15.f];
  80. label.text = model.ArticleGroupName;
  81. CGSize size = [label sizeThatFits:CGSizeMake(SCREEN_WIDTH, 45)];
  82. return CGSizeMake(size.width, 45);
  83. }
  84. - (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
  85. {
  86. return 15.f;
  87. }
  88. - (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
  89. {
  90. return 30.f;
  91. }
  92. - (void)addChannelAction:(UIButton *)sender {
  93. if (self.ClickAddChannelBlock) {
  94. self.ClickAddChannelBlock();
  95. }
  96. }
  97. #pragma mark - setter
  98. - (UIButton *)addButton {
  99. if (!_addButton) {
  100. _addButton = [TDButton buttonWithType:UIButtonTypeCustom];
  101. [_addButton setImage:IMG(@"icon_circleAdd") forState:UIControlStateNormal];
  102. [_addButton addTarget:self action:@selector(addChannelAction:) forControlEvents:UIControlEventTouchUpInside];
  103. [_addButton setCurrentButtonHotSize:CGSizeZero];
  104. }
  105. return _addButton;
  106. }
  107. @end