CycleScrollView.m 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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. NSLog(@"contentView gesture count %d",contentView.gestureRecognizers.count);
  116. CGRect rightRect = contentView.frame;
  117. rightRect.origin = CGPointMake(CGRectGetWidth(self.scrollView.frame) * (counter ++), 0);
  118. contentView.frame = rightRect;
  119. [self.scrollView addSubview:contentView];
  120. }
  121. [_scrollView setContentOffset:CGPointMake(_scrollView.frame.size.width, 0)];
  122. }
  123. /**
  124. * 设置scrollView的content数据源,即contentViews
  125. */
  126. - (void)setScrollViewContentDataSource
  127. {
  128. NSInteger previousPageIndex = [self getValidNextPageIndexWithPageIndex:self.currentPageIndex - 1];
  129. NSInteger rearPageIndex = [self getValidNextPageIndexWithPageIndex:self.currentPageIndex + 1];
  130. if (self.contentViews == nil) {
  131. self.contentViews = [@[] mutableCopy];
  132. }
  133. [self.contentViews removeAllObjects];
  134. if (self.fetchContentViewAtIndex) {
  135. [self.contentViews addObject:self.fetchContentViewAtIndex(previousPageIndex)];
  136. [self.contentViews addObject:self.fetchContentViewAtIndex(_currentPageIndex)];
  137. UIView* currentView =self.fetchContentViewAtIndex(_currentPageIndex);
  138. currentView.gestureRecognizers= nil;
  139. UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(contentViewTapAction:)];
  140. [self.fetchContentViewAtIndex(_currentPageIndex) addGestureRecognizer:tapGesture];
  141. [self.contentViews addObject:self.fetchContentViewAtIndex(rearPageIndex)];
  142. }
  143. }
  144. - (NSInteger)getValidNextPageIndexWithPageIndex:(NSInteger)currentPageIndex;
  145. {
  146. if(currentPageIndex == -1) {
  147. return self.totalPageCount - 1;
  148. } else if (currentPageIndex == self.totalPageCount) {
  149. return 0;
  150. } else {
  151. return currentPageIndex;
  152. }
  153. }
  154. #pragma mark -
  155. #pragma mark - UIScrollViewDelegate
  156. - (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
  157. {
  158. [self.animationTimer pauseTimer];
  159. }
  160. - (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
  161. {
  162. [self.animationTimer resumeTimerAfterTimeInterval:self.animationDuration];
  163. }
  164. - (void)scrollViewDidScroll:(UIScrollView *)scrollView
  165. {
  166. int contentOffsetX = scrollView.contentOffset.x;
  167. if(contentOffsetX >= (2 * CGRectGetWidth(scrollView.frame))) {
  168. self.currentPageIndex = [self getValidNextPageIndexWithPageIndex:self.currentPageIndex + 1];
  169. // DebugLog(@"next,当前页:%d",self.currentPageIndex);
  170. [self configContentViews];
  171. }
  172. if(contentOffsetX <= 0) {
  173. self.currentPageIndex = [self getValidNextPageIndexWithPageIndex:self.currentPageIndex - 1];
  174. // DebugLog(@"previous,当前页:%d",self.currentPageIndex);
  175. [self configContentViews];
  176. }
  177. self.pageControl.currentPage = self.currentPageIndex;
  178. }
  179. - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
  180. {
  181. [scrollView setContentOffset:CGPointMake(CGRectGetWidth(scrollView.frame), 0) animated:NO];
  182. }
  183. #pragma mark -
  184. #pragma mark - 响应事件
  185. - (void)animationTimerDidFired:(NSTimer *)timer
  186. {
  187. CGPoint newOffset = CGPointMake(/*self.scrollView.contentOffset.x +*/ 2* CGRectGetWidth(self.scrollView.frame), self.scrollView.contentOffset.y);
  188. [self.scrollView setContentOffset:newOffset animated:NO];
  189. }
  190. - (void)contentViewTapAction:(UITapGestureRecognizer *)tap
  191. {
  192. if (self.TapActionBlock) {
  193. self.TapActionBlock(self.currentPageIndex);
  194. }
  195. }
  196. /*
  197. // Only override drawRect: if you perform custom drawing.
  198. // An empty implementation adversely affects performance during animation.
  199. - (void)drawRect:(CGRect)rect
  200. {
  201. // Drawing code
  202. }
  203. */
  204. @end