HorizonScroll.m 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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. @property (nonatomic, strong) TDButton *musicButtton;
  19. @property (nonatomic, strong) UIView *lineV;
  20. @end
  21. @implementation HorizonScroll
  22. - (instancetype)initWithFrame:(CGRect)frame
  23. {
  24. if (self = [super initWithFrame:frame]) {
  25. [self addSubview:self.collectionView];
  26. [self addSubview:self.addButton];
  27. [self addSubview:self.musicButtton];
  28. [self addSubview:self.lineV];
  29. [self.musicButtton mas_makeConstraints:^(MASConstraintMaker *make) {
  30. make.top.bottom.mas_equalTo(self);
  31. make.right.mas_offset(-3);
  32. make.width.mas_offset(44);
  33. }];
  34. [self.lineV mas_makeConstraints:^(MASConstraintMaker *make) {
  35. make.centerY.mas_equalTo(self);
  36. make.right.mas_equalTo(self.musicButtton.mas_left);
  37. make.size.mas_offset(CGSizeMake(0.5, 18));
  38. }];
  39. [self.addButton mas_makeConstraints:^(MASConstraintMaker *make) {
  40. make.top.bottom.mas_equalTo(self);
  41. make.right.mas_equalTo(self.lineV.mas_left);
  42. make.width.mas_offset(44);
  43. }];
  44. [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
  45. make.left.top.bottom.mas_equalTo(self);
  46. make.right.mas_equalTo(self.addButton.mas_left);
  47. }];
  48. }
  49. return self;
  50. }
  51. - (UICollectionView *)collectionView{
  52. if (!_collectionView) {
  53. UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
  54. // 垂直方向滑动
  55. flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
  56. _collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:flowLayout];
  57. _collectionView.delegate = self;
  58. _collectionView.dataSource = self;
  59. _collectionView.showsVerticalScrollIndicator = NO;
  60. _collectionView.showsHorizontalScrollIndicator = NO;
  61. _collectionView.backgroundColor = [UIColor whiteColor];
  62. [_collectionView registerNib:[UINib nibWithNibName:@"HomeScrCell" bundle:nil] forCellWithReuseIdentifier:@"HomeScrCellID"];
  63. }
  64. return _collectionView;
  65. }
  66. - (void)setChannelArray:(NSArray *)channelArr {
  67. _channelArr = channelArr;
  68. [self.collectionView reloadData];
  69. }
  70. - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
  71. {
  72. return self.channelArr.count;
  73. }
  74. -(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
  75. HomeScrCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"HomeScrCellID" forIndexPath:indexPath];
  76. ChannelModel * model = self.channelArr[indexPath.item];
  77. [cell setDataWithTitle:model.ArticleGroupName isSelect:model.IsSelect];
  78. return cell;
  79. }
  80. - (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewFlowLayout *)layout insetForSectionAtIndex:(NSInteger)section {
  81. return UIEdgeInsetsMake(0, 15, 0, 15);
  82. }
  83. - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
  84. {
  85. ChannelModel * model = self.channelArr[indexPath.item];
  86. if (self.ClickSelectChannelBlock) {
  87. self.ClickSelectChannelBlock(model.ArticleGroupId,model.StyleCss);
  88. }
  89. }
  90. - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
  91. {
  92. ChannelModel * model = self.channelArr[indexPath.item];
  93. UILabel * label = [UILabel new];
  94. label.font = [UIFont systemFontOfSize:15.f];
  95. label.text = model.ArticleGroupName;
  96. CGSize size = [label sizeThatFits:CGSizeMake(SCREEN_WIDTH, 45)];
  97. return CGSizeMake(size.width, 45);
  98. }
  99. - (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
  100. {
  101. return 15.f;
  102. }
  103. - (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
  104. {
  105. return 30.f;
  106. }
  107. - (void)addChannelAction:(UIButton *)sender {
  108. if (self.ClickAddChannelBlock) {
  109. self.ClickAddChannelBlock();
  110. }
  111. }
  112. - (void)musicAction:(UIButton *)sender {
  113. if (self.ClickMusicBlock) {
  114. self.ClickMusicBlock();
  115. }
  116. }
  117. #pragma mark - setter
  118. - (UIButton *)addButton {
  119. if (!_addButton) {
  120. _addButton = [TDButton buttonWithType:UIButtonTypeCustom];
  121. [_addButton setImage:IMG(@"home_pdAdd_icon") forState:UIControlStateNormal];
  122. [_addButton addTarget:self action:@selector(addChannelAction:) forControlEvents:UIControlEventTouchUpInside];
  123. [_addButton setCurrentButtonHotSize:CGSizeZero];
  124. }
  125. return _addButton;
  126. }
  127. - (UIButton *)musicButtton {
  128. if (!_musicButtton) {
  129. _musicButtton = [TDButton buttonWithType:UIButtonTypeCustom];
  130. [_musicButtton setImage:IMG(@"home_music_icon") forState:UIControlStateNormal];
  131. [_musicButtton addTarget:self action:@selector(musicAction:) forControlEvents:UIControlEventTouchUpInside];
  132. [_musicButtton setCurrentButtonHotSize:CGSizeZero];
  133. }
  134. return _musicButtton;
  135. }
  136. - (UIView *)lineV
  137. {
  138. if (!_lineV) {
  139. _lineV = [UIView new];
  140. _lineV.backgroundColor = UIColorHex(0xD8D8D8);
  141. }
  142. return _lineV;
  143. }
  144. @end