SDCycleScrollView2.m 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. //
  2. // SDCycleScrollView.m
  3. // SDCycleScrollView
  4. //
  5. // Created by aier on 15-3-22.
  6. // Copyright (c) 2015年 GSD. All rights reserved.
  7. //
  8. /**
  9. *******************************************************
  10. * *
  11. * 感谢您的支持, 如果下载的代码在使用过程中出现BUG或者其他问题 *
  12. * 您可以发邮件到gsdios@126.com 或者 到 *
  13. * https://github.com/gsdios?tab=repositories 提交问题 *
  14. * *
  15. *******************************************************
  16. */
  17. #import "SDCycleScrollView2.h"
  18. #import "SDCollectionViewCell.h"
  19. #import "UIView+SDExtension.h"
  20. #import "TAPageControl.h"
  21. //#import "NSData+SDDataCache.h"
  22. #define kRepeatCount 1 // 循环次数
  23. NSString * const MYID = @"cycleCell2";
  24. @interface SDCycleScrollView2 () <UICollectionViewDataSource, UICollectionViewDelegate>
  25. @property (nonatomic, weak) UICollectionView *mainView; // 显示图片的collectionView
  26. @property (nonatomic, weak) UICollectionViewFlowLayout *flowLayout;
  27. @property (nonatomic, strong) NSMutableArray *imagesGroup;
  28. @property (nonatomic, strong) NSTimer *timer;
  29. @property (nonatomic, assign) NSInteger totalItemsCount;
  30. @property (nonatomic, weak) TAPageControl *pageControl;
  31. @end
  32. @implementation SDCycleScrollView2
  33. - (instancetype)initWithFrame:(CGRect)frame
  34. {
  35. if (self = [super initWithFrame:frame]) {
  36. [self initialization];
  37. [self setupMainView];
  38. }
  39. return self;
  40. }
  41. - (id)initWithCoder:(NSCoder *)aDecoder
  42. {
  43. if (self = [super initWithCoder:aDecoder]) {
  44. [self initialization];
  45. [self setupMainView];
  46. }
  47. return self;
  48. }
  49. - (void)initialization
  50. {
  51. _pageControlAliment = SDCycleScrollViewPageContolAlimentCenter;
  52. _autoScrollTimeInterval = 1.0;
  53. _titleLabelTextColor = [UIColor whiteColor];
  54. _titleLabelTextFont= [UIFont systemFontOfSize:14];
  55. _titleLabelBackgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.5];
  56. _titleLabelHeight = 30;
  57. self.backgroundColor = [UIColor clearColor];
  58. }
  59. + (instancetype)cycleScrollViewWithFrame:(CGRect)frame imagesGroup:(NSArray *)imagesGroup
  60. {
  61. SDCycleScrollView2 *cycleScrollView = [[self alloc] initWithFrame:frame];
  62. cycleScrollView.imagesGroup = [NSMutableArray arrayWithArray:imagesGroup];
  63. return cycleScrollView;
  64. }
  65. + (instancetype)cycleScrollViewWithFrame:(CGRect)frame imageURLsGroup:(NSArray *)imageURLsGroup
  66. {
  67. SDCycleScrollView2 *cycleScrollView = [[self alloc] initWithFrame:frame];
  68. cycleScrollView.imageURLsGroup = [NSMutableArray arrayWithArray:imageURLsGroup];
  69. return cycleScrollView;
  70. }
  71. - (void)setFrame:(CGRect)frame
  72. {
  73. [super setFrame:frame];
  74. _flowLayout.itemSize = self.frame.size;
  75. }
  76. - (void)setPageControlDotSize:(CGSize)pageControlDotSize
  77. {
  78. _pageControlDotSize = pageControlDotSize;
  79. _pageControl.dotSize = pageControlDotSize;
  80. }
  81. - (void)setDotColor:(UIColor *)dotColor
  82. {
  83. _dotColor = dotColor;
  84. _pageControl.dotColor = dotColor;
  85. }
  86. - (void)setAutoScrollTimeInterval:(CGFloat)autoScrollTimeInterval
  87. {
  88. _autoScrollTimeInterval = autoScrollTimeInterval;
  89. [_timer invalidate];
  90. _timer = nil;
  91. }
  92. // 设置显示图片的collectionView
  93. - (void)setupMainView
  94. {
  95. UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
  96. flowLayout.itemSize = self.frame.size;
  97. flowLayout.minimumLineSpacing = 0;
  98. flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
  99. _flowLayout = flowLayout;
  100. UICollectionView *mainView = [[UICollectionView alloc] initWithFrame:self.frame collectionViewLayout:flowLayout];
  101. mainView.backgroundColor = [UIColor clearColor];
  102. mainView.pagingEnabled = YES;
  103. mainView.showsHorizontalScrollIndicator = NO;
  104. mainView.showsVerticalScrollIndicator = NO;
  105. mainView.bounces = NO;
  106. [mainView registerClass:[SDCollectionViewCell class] forCellWithReuseIdentifier:MYID];
  107. mainView.dataSource = self;
  108. mainView.delegate = self;
  109. [self addSubview:mainView];
  110. _mainView = mainView;
  111. }
  112. - (void)setImagesGroup:(NSMutableArray *)imagesGroup
  113. {
  114. if ([imagesGroup count]>0) {
  115. _imagesGroup = imagesGroup;
  116. _totalItemsCount = imagesGroup.count * kRepeatCount;
  117. [self setupPageControl];
  118. }
  119. }
  120. - (void)setupPageControl
  121. {
  122. if ([_imagesGroup count]>1)
  123. {
  124. if (_pageControl) [_pageControl removeFromSuperview];//重新加载数据时调整
  125. TAPageControl *pageControl = [[TAPageControl alloc] init];
  126. pageControl.numberOfPages = self.imagesGroup.count;
  127. [self addSubview:pageControl];
  128. _pageControl = pageControl;
  129. }
  130. }
  131. - (void)layoutSubviews
  132. {
  133. [super layoutSubviews];
  134. if ([_imagesGroup count]>1) {
  135. _mainView.frame = self.bounds;
  136. // if (_mainView.contentOffset.x == 0 && _totalItemsCount) {
  137. // [_mainView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:_totalItemsCount * 0.5 inSection:0] atScrollPosition:UICollectionViewScrollPositionNone animated:NO];
  138. // }
  139. CGSize size = [_pageControl sizeForNumberOfPages:self.imagesGroup.count];
  140. CGFloat x = (self.sd_width - size.width) * 0.5;
  141. if (self.pageControlAliment == SDCycleScrollViewPageContolAlimentRight) {
  142. x = self.mainView.sd_width - size.width - 10;
  143. }
  144. CGFloat y = self.mainView.sd_height - size.height - 10;
  145. _pageControl.frame = CGRectMake(x, y, size.width, size.height);
  146. [_pageControl sizeToFit];
  147. }
  148. }
  149. //解决当父View释放时,当前视图因为被Timer强引用而不能释放的问题
  150. - (void)willMoveToSuperview:(UIView *)newSuperview
  151. {
  152. if (!newSuperview) {
  153. [_timer invalidate];
  154. _timer = nil;
  155. }
  156. }
  157. #pragma mark - UICollectionViewDataSource
  158. - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
  159. {
  160. return _totalItemsCount;
  161. }
  162. - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
  163. {
  164. SDCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:MYID forIndexPath:indexPath];
  165. for (UIView *view in cell.subviews) {
  166. if (view) {
  167. [view removeFromSuperview];
  168. }
  169. }
  170. long itemIndex = indexPath.item % self.imagesGroup.count;
  171. [cell addSubview:self.imagesGroup[itemIndex]];
  172. if (!cell.hasConfigured) {
  173. cell.titleLabelBackgroundColor = self.titleLabelBackgroundColor;
  174. cell.titleLabelHeight = self.titleLabelHeight;
  175. cell.titleLabelTextColor = self.titleLabelTextColor;
  176. cell.titleLabelTextFont = self.titleLabelTextFont;
  177. cell.hasConfigured = YES;
  178. }
  179. return cell;
  180. }
  181. - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
  182. {
  183. if ([self.delegate respondsToSelector:@selector(cycleScrollView:didSelectItemAtIndex:)]) {
  184. [self.delegate cycleScrollView:self didSelectItemAtIndex:indexPath.item % self.imagesGroup.count];
  185. }
  186. }
  187. #pragma mark - UIScrollViewDelegate
  188. - (void)scrollViewDidScroll:(UIScrollView *)scrollView
  189. {
  190. if ([_imagesGroup count]>1) {
  191. int itemIndex = (scrollView.contentOffset.x + self.mainView.sd_width * 0.5) / self.mainView.sd_width;
  192. if (!self.imagesGroup.count) return; // 解决清除timer时偶尔会出现的问题
  193. int indexOnPageControl = itemIndex % self.imagesGroup.count;
  194. _pageControl.currentPage = indexOnPageControl;
  195. }
  196. }
  197. - (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
  198. {
  199. [_timer invalidate];
  200. _timer = nil;
  201. }
  202. @end