PageScrollView.m 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. //
  2. // PageScrollView.m
  3. // CommonLibrary
  4. //
  5. // Created by Alexi Chen on 3/5/13.
  6. // Copyright (c) 2013 AlexiChen. All rights reserved.
  7. //
  8. #import "PageScrollView.h"
  9. #import "UIView+Layout.h"
  10. #import "IOSDeviceConfig.h"
  11. #import "NSString+Common.h"
  12. #import "UIView+CustomAutoLayout.h"
  13. @interface PageScrollView ()
  14. @end
  15. @implementation PageScrollView
  16. - (void)addScrollView
  17. {
  18. _scrollView = [[UIScrollView alloc] init];
  19. _scrollView.pagingEnabled = YES;
  20. _scrollView.delegate = self;
  21. _scrollView.bounces = NO;
  22. _scrollView.showsHorizontalScrollIndicator = NO;
  23. _scrollView.showsVerticalScrollIndicator = NO;
  24. _scrollView.scrollsToTop = NO;
  25. _scrollView.indicatorStyle = UIScrollViewIndicatorStyleWhite;
  26. [self addSubview:_scrollView];
  27. _pageControl = [[UIPageControl alloc] init];
  28. // _pageControl.numberOfPages = _images.count;
  29. // _pageControl.currentPage = 0;
  30. // [_pageControl addTarget:self action:@selector(changePage:) forControlEvents:UIControlEventValueChanged];
  31. [self addSubview:_pageControl];
  32. _pageControl.hidden = YES;
  33. }
  34. - (void)setPageIndex:(NSInteger)pageIndex
  35. {
  36. _pageIndex = pageIndex;
  37. _pageControl.currentPage = pageIndex;
  38. }
  39. - (void)changeToPage:(NSInteger)page manual:(BOOL)isManual
  40. {
  41. [self loadPage:page - 1 feedBack:NO];
  42. [self loadPage:page feedBack:isManual];
  43. [self loadPage:page + 1 feedBack:NO];
  44. CGRect frame = _scrollView.frame;
  45. frame.origin.x = frame.size.width * page;
  46. frame.origin.y = 0;
  47. [_scrollView scrollRectToVisible:frame animated:NO];
  48. _pageControlUsed = YES;
  49. }
  50. - (void)changePage:(id)sender
  51. {
  52. NSInteger page = _pageControl.currentPage;
  53. [self changeToPage:page manual:NO];
  54. }
  55. - (void)addOwnViews
  56. {
  57. [self addScrollView];
  58. }
  59. - (void)roratePages
  60. {
  61. for (NSInteger page = 0; page < _pages.count; page++)
  62. {
  63. UIView *view = [_pages objectAtIndex:page];
  64. if (view.superview != nil)
  65. {
  66. CGRect rect = _scrollView.bounds;
  67. rect.origin.x = rect.size.width * page;
  68. [view setFrameAndLayout:rect];
  69. }
  70. }
  71. }
  72. - (void)setFrameAndLayout:(CGRect)rect withPages:(NSArray *)pages
  73. {
  74. if (!pages) {
  75. return;
  76. }
  77. if (_pages != pages)
  78. {
  79. self.pages = pages;
  80. }
  81. [self setFrameAndLayout:rect];
  82. // 设置scrollView相关的内容
  83. _scrollView.contentSize = CGSizeMake(_scrollView.frame.size.width * _pages.count, _scrollView.frame.size.height);
  84. _pageControl.numberOfPages = self.pages.count;
  85. _pageControl.currentPage = _pageIndex;
  86. [self roratePages];
  87. [self loadPage:_pageIndex - 1 feedBack:NO];
  88. [self loadPage:_pageIndex feedBack:NO];
  89. [self loadPage:_pageIndex + 1 feedBack:NO];
  90. // [self loadPage:_pageIndex feedBack:NO];
  91. CGPoint p = _scrollView.contentOffset;
  92. p.x = _pageIndex * _scrollView.frame.size.width;
  93. _scrollView.contentOffset = p;
  94. if (_pageIndex >= 0 && _pageIndex < _pages.count)
  95. {
  96. if (_pageScrollDelegate && [_pageScrollDelegate respondsToSelector:@selector(onPageScrollView:scrollToPage:)])
  97. {
  98. [_pageScrollDelegate onPageScrollView:self scrollToPage:_pageIndex];
  99. }
  100. }
  101. }
  102. #define kPageIndicatorHeight 10
  103. - (CGRect)pageIndicatorRect
  104. {
  105. CGRect rect = self.bounds;
  106. rect.origin.y += rect.size.height - kPageIndicatorHeight;
  107. rect.size.height = kPageIndicatorHeight;
  108. rect = CGRectInset(rect, 9, (kPageIndicatorHeight - 5)/2);
  109. CGFloat pw = rect.size.width / _pages.count;
  110. CGFloat pageWidth = _scrollView.frame.size.width;
  111. int page = floor(((_scrollView.contentOffset.x - pageWidth/2)/pageWidth)+1);
  112. rect.origin.x += page * pw;
  113. rect.size.width = pw;
  114. return rect;
  115. }
  116. - (void)relayoutFrameOfSubViews
  117. {
  118. _scrollView.frame = self.bounds;
  119. [_pageControl layoutParentHorizontalCenter];
  120. [_pageControl alignParentBottomWithMargin:50];
  121. }
  122. - (void)loadPage:(NSInteger)page feedBack:(BOOL)need
  123. {
  124. if (page < 0) {
  125. return;
  126. }
  127. if (page >= _pages.count) {
  128. return;
  129. }
  130. if (_pages.count == 0) {
  131. return;
  132. }
  133. UIView *view = [_pages objectAtIndex:page];
  134. if (view.superview == nil)
  135. {
  136. CGRect rect = _scrollView.bounds;
  137. rect.origin.x = rect.size.width * page;
  138. [view setFrameAndLayout:rect];
  139. [_scrollView addSubview:view];
  140. }
  141. else
  142. {
  143. CGRect rect = _scrollView.bounds;
  144. rect.origin.x = rect.size.width * page;
  145. [view setFrameAndLayout:rect];
  146. }
  147. if (need)
  148. {
  149. if (_pageIndex >= 0 && _pageIndex < _pages.count)
  150. {
  151. if (_pageScrollDelegate && [_pageScrollDelegate respondsToSelector:@selector(onPageScrollView:scrollToPage:)])
  152. {
  153. [_pageScrollDelegate onPageScrollView:self scrollToPage:_pageIndex];
  154. }
  155. }
  156. }
  157. }
  158. - (void)scrollTo:(NSInteger)pageIndex
  159. {
  160. CGFloat pageWidth = _scrollView.frame.size.width;
  161. CGPoint p = _scrollView.contentOffset;
  162. p.x = pageIndex * pageWidth;
  163. _scrollView.contentOffset = p;
  164. // DebugLog(@"%@", NSStringFromCGPoint(p));
  165. self.pageIndex = pageIndex;
  166. }
  167. - (void)scrollViewDidScroll:(UIScrollView *)scrollView
  168. {
  169. if (_pageControlUsed)
  170. {
  171. return;
  172. }
  173. // DebugLog(@"scrollViewDidScroll");
  174. CGFloat pageWidth = _scrollView.frame.size.width;
  175. NSInteger pi = floor(((_scrollView.contentOffset.x - pageWidth/2)/pageWidth)+1);
  176. if (_pageIndex == pi) {
  177. return;
  178. }
  179. else
  180. {
  181. self.pageIndex = pi;
  182. [self loadPage:_pageIndex - 1 feedBack:NO];
  183. [self loadPage:_pageIndex feedBack:YES];
  184. [self loadPage:_pageIndex + 1 feedBack:NO];
  185. }
  186. }
  187. - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
  188. {
  189. _pageControlUsed = NO;
  190. }
  191. - (void)setPageFrame:(NSInteger)page
  192. {
  193. if (page < 0) {
  194. return;
  195. }
  196. if (page >= _pages.count) {
  197. return;
  198. }
  199. if (_pages.count == 0) {
  200. return;
  201. }
  202. UIView *view = [_pages objectAtIndex:page];
  203. if (view.superview == nil)
  204. {
  205. CGRect rect = _scrollView.bounds;
  206. rect.origin.x = rect.size.width * page;
  207. [view setFrameAndLayout:rect];
  208. [_scrollView addSubview:view];
  209. }
  210. }
  211. - (NSInteger)pageCount
  212. {
  213. return [_pages count];
  214. }
  215. @end