ZZCarousel.m 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. //
  2. // ZZCarousel.m
  3. // Ace
  4. //
  5. // Created by AceZZ on 15/9/7.
  6. // Copyright (c) 2015年 cscmh. All rights reserved.
  7. //
  8. #import "ZZCarousel.h"
  9. /*
  10. * UICollectionViewCell
  11. */
  12. @implementation ZZCarouselView
  13. -(instancetype)initWithFrame:(CGRect)frame
  14. {
  15. self = [super initWithFrame:frame];
  16. if (self) {
  17. _imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height)];
  18. //_imageView.contentMode = UIViewContentModeScaleAspectFit;
  19. [self.contentView addSubview:_imageView];
  20. // _title = [[UILabel alloc]initWithFrame:CGRectMake(0, frame.size.height - 20, frame.size.width, 20)];
  21. // _title.backgroundColor = [UIColor blackColor];
  22. // _title.alpha = 0.7;
  23. // _title.textColor = [UIColor whiteColor];
  24. // [self.contentView addSubview:_title];
  25. }
  26. return self;
  27. }
  28. @end
  29. @implementation ZZCarouselPageControlOfNumber
  30. -(instancetype)initWithFrame:(CGRect)frame
  31. {
  32. self = [super initWithFrame:frame];
  33. if (self) {
  34. _pageControl = [[UILabel alloc]initWithFrame:CGRectMake(5, 3, self.frame.size.width - 10, self.frame.size.height - 6)];
  35. _pageControl.backgroundColor = [UIColor clearColor];
  36. _pageControl.textAlignment = NSTextAlignmentCenter;
  37. [self addSubview:_pageControl];
  38. }
  39. return self;
  40. }
  41. @end
  42. /*
  43. * 轮播
  44. */
  45. #define DY 1000
  46. @interface ZZCarousel()<UICollectionViewDelegate,UICollectionViewDataSource>
  47. @property (nonatomic, strong) UICollectionView *carousel;
  48. @property (nonatomic, strong) UICollectionViewFlowLayout *flowLayout;
  49. @property (nonatomic, strong) UIPageControl *pageControl;
  50. //自定义 UIPageControl
  51. @property (nonatomic, strong) ZZCarouselPageControlOfNumber *pageControlOfNumber;
  52. @property (nonatomic, strong) NSTimer *timer;
  53. @property (nonatomic, assign) NSInteger realItems;
  54. @end
  55. @implementation ZZCarousel
  56. -(instancetype)initWithFrame:(CGRect)frame
  57. {
  58. self = [super initWithFrame:frame];
  59. if (self) {
  60. // 创建UI 方法
  61. [self createCarouselUI];
  62. //重用 UICollectionView
  63. }
  64. return self;
  65. }
  66. /*
  67. * 创建View 中 UI
  68. */
  69. -(void)createCarouselUI
  70. {
  71. _flowLayout = [[UICollectionViewFlowLayout alloc] init];
  72. _flowLayout.itemSize = (CGSize){self.frame.size.width,self.frame.size.height};
  73. _flowLayout.minimumLineSpacing = 0;
  74. _flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
  75. _carousel = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height) collectionViewLayout:_flowLayout];
  76. _carousel.backgroundColor = [UIColor clearColor];
  77. _carousel.pagingEnabled = YES;
  78. _carousel.scrollEnabled = YES;
  79. _carousel.showsHorizontalScrollIndicator = NO;
  80. _carousel.showsVerticalScrollIndicator = NO;
  81. [_carousel registerClass:[ZZCarouselView class] forCellWithReuseIdentifier:@"ZZWheelCell"];
  82. _carousel.dataSource = self;
  83. _carousel.delegate = self;
  84. [self addSubview:_carousel];
  85. }
  86. /*
  87. * 当使用时候执行pageControlFrame 属性时则 创建 UIPageControl 指示器
  88. 并添加到ZZCarousel显示
  89. */
  90. -(void)setPageControlFrame:(CGRect)pageControlFrame
  91. {
  92. //创建指示器
  93. if (_pageType == 0) {
  94. _pageControl = [[UIPageControl alloc] init];
  95. _pageControl.frame = pageControlFrame;
  96. _pageControl.currentPage = 0;
  97. [self addSubview:_pageControl];
  98. }else if (_pageType == 1){
  99. _pageControlOfNumber = [[ZZCarouselPageControlOfNumber alloc]initWithFrame:pageControlFrame];
  100. _pageControlOfNumber.currentPage = 0;
  101. [self addSubview:_pageControlOfNumber];
  102. }
  103. }
  104. -(void)setPageIndicatorTintColor:(UIColor *)pageIndicatorTintColor
  105. {
  106. _pageControl.pageIndicatorTintColor = pageIndicatorTintColor;
  107. }
  108. -(void)setCurrentPageIndicatorTintColor:(UIColor *)currentPageIndicatorTintColor
  109. {
  110. _pageControl.currentPageIndicatorTintColor = currentPageIndicatorTintColor;
  111. }
  112. -(void)setPageControlBackGroundColor:(UIColor *)pageControlBackGroundColor
  113. {
  114. if (_pageType == 0) {
  115. _pageControl.backgroundColor = [UIColor clearColor];
  116. }else if (_pageType == 1){
  117. _pageControlOfNumber.backgroundColor = pageControlBackGroundColor;
  118. }
  119. }
  120. -(void)setPageControlOfNumberFont:(UIFont *)pageControlOfNumberFont
  121. {
  122. _pageControlOfNumber.pageControl.font = pageControlOfNumberFont;
  123. }
  124. -(void)setPageContolOfNumberFontColor:(UIColor *)pageContolOfNumberFontColor
  125. {
  126. _pageControlOfNumber.pageControl.textColor = pageContolOfNumberFontColor;
  127. }
  128. -(void)reloadData
  129. {
  130. if (_isAutoScroll == YES) {
  131. [_timer invalidate];
  132. _timer = nil;
  133. [self createTimer];
  134. }
  135. if (_pageType == 0) {
  136. _pageControl.numberOfPages = [_delegate numberOfZZCarousel:self];
  137. }else if(_pageType == 1){
  138. _pageControlOfNumberCurrentTotal = [_delegate numberOfZZCarousel:self];
  139. _pageControlOfNumber.pageControl.text = [NSString stringWithFormat:@"%d / %ld",1,(long)_pageControlOfNumberCurrentTotal];
  140. }
  141. _numberOfItems = [_delegate numberOfZZCarousel:self];
  142. _realItems = _numberOfItems *DY;
  143. [self.carousel reloadData];
  144. if (_carousel.contentOffset.x == 0 && _realItems) {
  145. [_carousel scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:_realItems * 0.5 inSection:0] atScrollPosition:UICollectionViewScrollPositionNone animated:NO];
  146. }
  147. }
  148. #pragma mark - UICollectionViewDataSource
  149. -(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
  150. {
  151. return 1;
  152. }
  153. - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
  154. {
  155. return _realItems;
  156. }
  157. - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
  158. {
  159. NSInteger itemIndex = indexPath.row % _numberOfItems;
  160. return [_delegate zzcarousel:collectionView viewForItemAtIndex:indexPath itemsIndex:itemIndex identifire:@"ZZWheelCell" ZZCarousel:self];
  161. }
  162. - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)index
  163. {
  164. if ([self.delegate respondsToSelector:@selector(zzcarouselScrollView:didSelectItemAtIndex:)]) {
  165. [self.delegate zzcarouselScrollView:self didSelectItemAtIndex:index.row % _numberOfItems];
  166. }
  167. }
  168. /*
  169. * 创建定时器
  170. */
  171. - (void)createTimer
  172. {
  173. self.timer = [NSTimer scheduledTimerWithTimeInterval:_carouseScrollTimeInterval target:self selector:@selector(autoCarouselScroll) userInfo:nil repeats:YES];
  174. [[NSRunLoop currentRunLoop] addTimer:self.timer forMode:NSRunLoopCommonModes];
  175. }
  176. /*
  177. * 执行定时器方法
  178. */
  179. - (void)autoCarouselScroll
  180. {
  181. if (0 == _realItems) return;
  182. int currentIndex = _carousel.contentOffset.x / _flowLayout.itemSize.width;
  183. int startIndex = currentIndex + 1;
  184. if (startIndex == _realItems) {
  185. startIndex = _realItems * 0.5;
  186. [_carousel scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:startIndex inSection:0] atScrollPosition:UICollectionViewScrollPositionNone animated:NO];
  187. }
  188. [_carousel scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:startIndex inSection:0] atScrollPosition:UICollectionViewScrollPositionNone animated:YES];
  189. }
  190. #pragma mark - UIScrollViewDelegate
  191. - (void)scrollViewDidScroll:(UIScrollView *)scrollView
  192. {
  193. int itemIndex = (scrollView.contentOffset.x + self.carousel.frame.size.width * 0.5) / self.carousel.frame.size.width;
  194. if (!self.numberOfItems) return;
  195. int indexOnPageControl = itemIndex % self.numberOfItems;
  196. if (_pageType == 0) {
  197. _pageControl.currentPage = indexOnPageControl;
  198. }else if (_pageType == 1){
  199. _pageControlOfNumber.pageControl.text = [NSString stringWithFormat:@"%d / %ld",indexOnPageControl+1,(long)_pageControlOfNumberCurrentTotal];
  200. self.pageControlOfNumber.currentPage = indexOnPageControl;
  201. }
  202. }
  203. //开始拖拽视图
  204. - (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
  205. {
  206. [_timer invalidate];
  207. _timer = nil;
  208. }
  209. //完成拖拽
  210. - (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
  211. {
  212. [self createTimer];
  213. }
  214. @end