CycleScrollView.m 11 KB

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