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