猎豆优选

LDSuperBrandView.m 6.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. //
  2. // LDSuperBrandView.m
  3. // YouHuiProject
  4. //
  5. // Created by 小花 on 2018/12/10.
  6. // Copyright © 2018年 kuxuan. All rights reserved.
  7. //
  8. #import "LDSuperBrandView.h"
  9. #import "LDSuperBrandCollectionCell.h"
  10. #import "LDSuperBrandMoreCell.h"
  11. static NSString *cellId = @"cellID";
  12. static NSString *moreCellId = @"moreCellId";
  13. @interface LDSuperBrandView ()
  14. <
  15. UICollectionViewDelegate,
  16. UICollectionViewDataSource,
  17. UICollectionViewDelegateFlowLayout,
  18. UIScrollViewDelegate
  19. >{
  20. NSInteger offX;
  21. BOOL returnFlag;
  22. NSArray *dataArr;
  23. }
  24. @property (nonatomic, strong) UIView *bgView;
  25. @property (nonatomic, strong) UIImageView *topImgView;
  26. @property (nonatomic, strong) UICollectionView *collectionView;
  27. @property (nonatomic, strong) NSTimer *timer;
  28. @end
  29. @implementation LDSuperBrandView
  30. - (instancetype)initWithFrame:(CGRect)frame
  31. {
  32. self = [super initWithFrame:frame];
  33. if (self) {
  34. self.backgroundColor = [UIColor whiteColor];
  35. [self initSubViews];
  36. }
  37. return self;
  38. }
  39. - (void)initSubViews {
  40. self.bgView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.width, self.height)];
  41. [self addSubview:self.bgView];
  42. self.bgView.backgroundColor = [UIColor YHColorWithHex:0xFDF8F1];
  43. self.topImgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0,self.bgView.width, self.bgView.width*36/365)];
  44. self.topImgView.image = [UIImage imageNamed:@"childSuper"];
  45. self.topImgView.backgroundColor = [UIColor YHColorWithHex:0xFDF8F1];
  46. [self.bgView addSubview:self.topImgView];
  47. UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
  48. flowLayout.minimumLineSpacing = 5;
  49. flowLayout.minimumInteritemSpacing = 0;
  50. flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
  51. self.collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, self.topImgView.bottom, self.bgView.width, 100) collectionViewLayout:flowLayout];
  52. self.collectionView.delegate = self;
  53. self.collectionView.dataSource = self;
  54. self.collectionView.backgroundColor = [UIColor YHColorWithHex:0xFDF8F1];
  55. self.collectionView.showsVerticalScrollIndicator = NO;
  56. self.collectionView.showsHorizontalScrollIndicator = NO;
  57. [self.collectionView registerClass:[LDSuperBrandCollectionCell class] forCellWithReuseIdentifier:cellId];
  58. [self.collectionView registerClass:[LDSuperBrandMoreCell class] forCellWithReuseIdentifier:moreCellId];
  59. [self.bgView addSubview:self.collectionView];
  60. }
  61. //- (void)startTimer {
  62. // if (!self.timer) {
  63. // self.timer = [NSTimer scheduledTimerWithTimeInterval:.1 target:self selector:@selector(autoNextPage) userInfo:nil repeats:YES];
  64. // [[NSRunLoop mainRunLoop] addTimer:self.timer forMode:NSRunLoopCommonModes];
  65. // }
  66. //}
  67. //
  68. //- (void)stopTimer {
  69. //
  70. // if (self.timer) {
  71. // [self.timer invalidate];
  72. // self.timer = nil;
  73. // }
  74. //}
  75. //- (void)autoNextPage {
  76. // CGSize contentSize = self.collectionView.contentSize;
  77. // CGFloat OffsetX = self.collectionView.contentOffset.x;
  78. // BOOL isBottom = (contentSize.width - OffsetX) <= self.collectionView.frame.size.width;
  79. //
  80. // if (isBottom) {
  81. // returnFlag = YES;
  82. // }
  83. // if (offX <= 0) {
  84. // returnFlag = NO;
  85. // }
  86. //
  87. // if (returnFlag) {
  88. // offX--;
  89. // }else {
  90. // offX++;
  91. // }
  92. //
  93. // [self.collectionView setContentOffset:CGPointMake(offX, 0) animated:YES];
  94. //}
  95. //#pragma mark --将要开始拖拽
  96. //- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
  97. // [self stopTimer];
  98. //}
  99. //
  100. //#pragma mark --结束拖拽
  101. //- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {
  102. // [self startTimer];
  103. //}
  104. //
  105. //
  106. //#pragma mark --将要结束拖拽
  107. //- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset {
  108. //
  109. // CGSize contentSize = self.collectionView.contentSize;
  110. // BOOL isBottom = (contentSize.width - targetContentOffset->x) <= self.collectionView.frame.size.width;
  111. //
  112. // if (isBottom) {
  113. // returnFlag = YES;
  114. // }
  115. // if (offX <= 0) {
  116. // returnFlag = NO;
  117. // }
  118. //
  119. // offX = targetContentOffset->x;
  120. //
  121. //}
  122. - (void)setSuperBrandData:(NSArray *)list {
  123. dataArr = list.mutableCopy;
  124. [self.collectionView reloadData];
  125. // if (self.timer) {
  126. // [self stopTimer];
  127. // }
  128. // [self startTimer];
  129. }
  130. #pragma mark ======
  131. - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
  132. if (dataArr.count > 0) {
  133. return dataArr.count+1;
  134. }
  135. return dataArr.count;
  136. }
  137. - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
  138. return CGSizeMake(95, 95);
  139. }
  140. - (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
  141. return UIEdgeInsetsMake(0, 5, 0, 5);
  142. }
  143. - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
  144. if (indexPath.row == dataArr.count) {
  145. LDSuperBrandMoreCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:moreCellId forIndexPath:indexPath];
  146. cell.backgroundColor = [UIColor whiteColor];
  147. return cell;
  148. }
  149. LDSuperBrandCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellId forIndexPath:indexPath];
  150. LDSuperBrandModel *model;
  151. if (dataArr.count > indexPath.row) {
  152. model = dataArr[indexPath.row];
  153. }
  154. cell.model = model;
  155. return cell;
  156. }
  157. - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
  158. if (indexPath.row == dataArr.count) {
  159. if (self.moreClick) {
  160. self.moreClick();
  161. }
  162. }else {
  163. if (self.selectedClick) {
  164. LDSuperBrandModel *model;
  165. if (dataArr.count > indexPath.row) {
  166. model = dataArr[indexPath.row];
  167. }
  168. self.selectedClick(model);
  169. }
  170. }
  171. }
  172. @end