CycleScrollView.m 11 KB

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