CycleScrollView.m 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. //
  2. // CycleScrollView.m
  3. // PagedScrollView
  4. //
  5. // Created by 陈政 on 14-1-23.
  6. // Copyright (c) 2014年 Apple Inc. All rights reserved.
  7. //
  8. #import "CycleScrollView.h"
  9. #import "NSTimer+Addition.h"
  10. @interface CycleScrollView () <UIScrollViewDelegate>
  11. @property (nonatomic , assign) NSInteger currentPageIndex;
  12. @property (nonatomic , assign) NSInteger totalPageCount;
  13. @property (nonatomic , strong) NSMutableArray *contentViews;
  14. @property (nonatomic , strong) UIScrollView *scrollView;
  15. @property (nonatomic , strong) NSTimer *animationTimer;
  16. @property (nonatomic , assign) NSTimeInterval animationDuration;
  17. @property (nonatomic , strong) UIPageControl *pageControl;
  18. @end
  19. @implementation CycleScrollView
  20. -(void) animationDuration:(NSTimeInterval)animationDuration
  21. {
  22. if (animationDuration > 0.0) {
  23. self.animationTimer = [NSTimer scheduledTimerWithTimeInterval:(self.animationDuration = animationDuration)
  24. target:self
  25. selector:@selector(animationTimerDidFired:)
  26. userInfo:nil
  27. repeats:YES];
  28. [self.animationTimer pauseTimer];
  29. }
  30. }
  31. - (void)setTotalPagesCount:(NSInteger (^)(void))totalPagesCount
  32. {
  33. _totalPageCount = totalPagesCount();
  34. if (_totalPageCount > 0) {
  35. [self configContentViews];
  36. [self.animationTimer resumeTimerAfterTimeInterval:self.animationDuration];
  37. self.pageControl.numberOfPages =_totalPageCount;
  38. }
  39. }
  40. -(void) addPage:(UIView*) page
  41. {
  42. [self.arr_Pages addObject:page];
  43. }
  44. -(void)reset
  45. {
  46. // self.page_count = 0;
  47. self.arr_Pages =[@[] mutableCopy];
  48. // Initialization code
  49. self.autoresizesSubviews = YES;
  50. self.scrollView = [[UIScrollView alloc] initWithFrame:self.bounds];
  51. self.scrollView.autoresizingMask = 0xFF;
  52. self.scrollView.contentMode = UIViewContentModeCenter;
  53. self.scrollView.contentSize = CGSizeMake(3 * CGRectGetWidth(self.scrollView.frame), CGRectGetHeight(self.scrollView.frame));
  54. // CGSize sss= self.scrollView.contentSize;
  55. // CGRect rrr =self.scrollView.frame;
  56. self.scrollView.delegate = self;
  57. self.scrollView.contentOffset = CGPointMake(CGRectGetWidth(self.scrollView.frame), 0);
  58. self.scrollView.pagingEnabled = YES;
  59. [self addSubview:self.scrollView];
  60. CGRect pageFrame =self.bounds;
  61. pageFrame.origin.y = self.frame.size.height -16-20;
  62. pageFrame.size.height = 20;
  63. // pageFrame
  64. self.pageControl = [[UIPageControl alloc] initWithFrame:pageFrame];//CGRectMake(0, self.frame.size.height -16-10, 320, 10)];
  65. self.pageControl.userInteractionEnabled = NO;
  66. [self addSubview:self.pageControl];
  67. // int count =self.totalPageCount;
  68. // self.pageControl.backgroundColor = [UIColor purpleColor];
  69. self.pageControl.numberOfPages = self.totalPageCount;
  70. self.pageControl.currentPage = 0;
  71. self.scrollView.showsHorizontalScrollIndicator = false;
  72. self.scrollView.showsVerticalScrollIndicator = false;
  73. self.currentPageIndex = 0;
  74. }
  75. - (id)initWithFrame:(CGRect)frame animationDuration:(NSTimeInterval)animationDuration
  76. {
  77. self = [self initWithFrame:frame];
  78. if (animationDuration > 0.0) {
  79. self.animationTimer = [NSTimer scheduledTimerWithTimeInterval:(self.animationDuration = animationDuration)
  80. target:self
  81. selector:@selector(animationTimerDidFired:)
  82. userInfo:nil
  83. repeats:YES];
  84. [self.animationTimer pauseTimer];
  85. }
  86. return self;
  87. }
  88. - (id)initWithFrame:(CGRect)frame
  89. {
  90. self = [super initWithFrame:frame];
  91. if (self) {
  92. // Initialization code
  93. self.autoresizesSubviews = YES;
  94. self.scrollView = [[UIScrollView alloc] initWithFrame:self.bounds];
  95. self.scrollView.autoresizingMask = 0xFF;
  96. self.scrollView.contentMode = UIViewContentModeCenter;
  97. self.scrollView.contentSize = CGSizeMake(3 * CGRectGetWidth(self.scrollView.frame), CGRectGetHeight(self.scrollView.frame));
  98. self.scrollView.delegate = self;
  99. self.scrollView.contentOffset = CGPointMake(CGRectGetWidth(self.scrollView.frame), 0);
  100. self.scrollView.pagingEnabled = YES;
  101. [self addSubview:self.scrollView];
  102. self.currentPageIndex = 0;
  103. }
  104. return self;
  105. }
  106. #pragma mark -
  107. #pragma mark - 私有函数
  108. - (void)configContentViews
  109. {
  110. [self.scrollView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
  111. [self setScrollViewContentDataSource];
  112. NSInteger counter = 0;
  113. for (UIView *contentView in self.contentViews) {
  114. contentView.userInteractionEnabled = YES;
  115. UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(contentViewTapAction:)];
  116. [contentView addGestureRecognizer:tapGesture];
  117. CGRect rightRect = contentView.frame;
  118. rightRect.origin = CGPointMake(CGRectGetWidth(self.scrollView.frame) * (counter ++), 0);
  119. contentView.frame = rightRect;
  120. [self.scrollView addSubview:contentView];
  121. }
  122. [_scrollView setContentOffset:CGPointMake(_scrollView.frame.size.width, 0)];
  123. }
  124. /**
  125. * 设置scrollView的content数据源,即contentViews
  126. */
  127. - (void)setScrollViewContentDataSource
  128. {
  129. NSInteger previousPageIndex = [self getValidNextPageIndexWithPageIndex:self.currentPageIndex - 1];
  130. NSInteger rearPageIndex = [self getValidNextPageIndexWithPageIndex:self.currentPageIndex + 1];
  131. if (self.contentViews == nil) {
  132. self.contentViews = [@[] mutableCopy];
  133. }
  134. [self.contentViews removeAllObjects];
  135. if (self.fetchContentViewAtIndex) {
  136. [self.contentViews addObject:self.fetchContentViewAtIndex(previousPageIndex)];
  137. [self.contentViews addObject:self.fetchContentViewAtIndex(_currentPageIndex)];
  138. [self.contentViews addObject:self.fetchContentViewAtIndex(rearPageIndex)];
  139. }
  140. }
  141. - (NSInteger)getValidNextPageIndexWithPageIndex:(NSInteger)currentPageIndex;
  142. {
  143. if(currentPageIndex == -1) {
  144. return self.totalPageCount - 1;
  145. } else if (currentPageIndex == self.totalPageCount) {
  146. return 0;
  147. } else {
  148. return currentPageIndex;
  149. }
  150. }
  151. #pragma mark -
  152. #pragma mark - UIScrollViewDelegate
  153. - (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
  154. {
  155. [self.animationTimer pauseTimer];
  156. }
  157. - (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
  158. {
  159. [self.animationTimer resumeTimerAfterTimeInterval:self.animationDuration];
  160. }
  161. - (void)scrollViewDidScroll:(UIScrollView *)scrollView
  162. {
  163. int contentOffsetX = scrollView.contentOffset.x;
  164. if(contentOffsetX >= (2 * CGRectGetWidth(scrollView.frame))) {
  165. self.currentPageIndex = [self getValidNextPageIndexWithPageIndex:self.currentPageIndex + 1];
  166. // NSLog(@"next,当前页:%d",self.currentPageIndex);
  167. [self configContentViews];
  168. }
  169. if(contentOffsetX <= 0) {
  170. self.currentPageIndex = [self getValidNextPageIndexWithPageIndex:self.currentPageIndex - 1];
  171. // NSLog(@"previous,当前页:%d",self.currentPageIndex);
  172. [self configContentViews];
  173. }
  174. self.pageControl.currentPage = self.currentPageIndex;
  175. }
  176. - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
  177. {
  178. [scrollView setContentOffset:CGPointMake(CGRectGetWidth(scrollView.frame), 0) animated:NO];
  179. }
  180. #pragma mark -
  181. #pragma mark - 响应事件
  182. - (void)animationTimerDidFired:(NSTimer *)timer
  183. {
  184. CGPoint newOffset = CGPointMake(/*self.scrollView.contentOffset.x +*/ 2* CGRectGetWidth(self.scrollView.frame), self.scrollView.contentOffset.y);
  185. [self.scrollView setContentOffset:newOffset animated:NO];
  186. }
  187. - (void)contentViewTapAction:(UITapGestureRecognizer *)tap
  188. {
  189. if (self.TapActionBlock) {
  190. self.TapActionBlock(self.currentPageIndex);
  191. }
  192. }
  193. /*
  194. // Only override drawRect: if you perform custom drawing.
  195. // An empty implementation adversely affects performance during animation.
  196. - (void)drawRect:(CGRect)rect
  197. {
  198. // Drawing code
  199. }
  200. */
  201. @end