悟空记账

HZMLazyScrollView.m 7.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. //
  2. // DMLazyScrollView.m
  3. // Lazy Loading UIScrollView for iOS
  4. //
  5. // Created by Daniele Margutti (me@danielemargutti.com) on 24/11/12.
  6. // Copyright (c) 2012 http://www.danielemargutti.com. All rights reserved.
  7. // Distribuited under MIT License
  8. //
  9. #import "HZMLazyScrollView.h"
  10. enum {
  11. HZMLazyScrollViewScrollDirectionBackward = 0,
  12. HZMLazyScrollViewScrollDirectionForward = 1
  13. }; typedef NSUInteger HZMLazyScrollViewScrollDirection;
  14. #define kDMLazyScrollViewTransitionDuration 0.2
  15. @interface HZMLazyScrollView() <UIScrollViewDelegate> {
  16. NSUInteger currentPage;
  17. BOOL isManualAnimating;
  18. }
  19. @end
  20. @implementation HZMLazyScrollView
  21. @synthesize numberOfPages,currentPage;
  22. @synthesize dataSource,controlDelegate;
  23. - (id)init {
  24. return [self initWithFrame:CGRectZero];
  25. }
  26. - (id)initWithFrame:(CGRect)frame
  27. {
  28. self = [super initWithFrame:frame];
  29. if (self) {
  30. [self initializeControl];
  31. }
  32. return self;
  33. }
  34. - (BOOL)hasMultiplePages {
  35. return self.numberOfPages > 1;
  36. }
  37. - (void) awakeFromNib {
  38. [super awakeFromNib];
  39. [self initializeControl];
  40. }
  41. - (void) initializeControl {
  42. self.showsHorizontalScrollIndicator = NO;
  43. self.showsVerticalScrollIndicator = NO;
  44. self.pagingEnabled = YES;
  45. self.delegate = self;
  46. self.contentSize = CGSizeMake(self.frame.size.width, self.contentSize.height);
  47. self.bounces = NO;
  48. self.scrollsToTop = NO;
  49. currentPage = NSNotFound;
  50. }
  51. - (void) setNumberOfPages:(NSUInteger)pages {
  52. // if (pages != numberOfPages) {
  53. numberOfPages = pages;
  54. NSInteger offset = [self hasMultiplePages] ? numberOfPages : 1;
  55. self.contentSize = CGSizeMake(self.frame.size.width * offset,
  56. self.contentSize.height);
  57. [self reloadData];
  58. // }
  59. }
  60. -(void)setFrame:(CGRect)frame{
  61. [super setFrame:frame];
  62. if (self.numberOfPages) {
  63. [self setNumberOfPages:self.numberOfPages];
  64. }
  65. }
  66. - (void) reloadData {
  67. [self setCurrentViewController:currentPage!=NSNotFound?currentPage:0];
  68. }
  69. - (void) layoutSubviews {
  70. [super layoutSubviews];
  71. }
  72. - (CGRect) visibleRect {
  73. CGRect visibleRect;
  74. visibleRect.origin = self.contentOffset;
  75. visibleRect.size = self.bounds.size;
  76. return visibleRect;
  77. }
  78. - (CGPoint) createPoint:(CGFloat) size {
  79. // if (_direction == HZMLazyScrollViewDirectionHorizontal) {
  80. return CGPointMake(size, 0);
  81. // } else {
  82. // return CGPointMake(0, size);
  83. // }
  84. }
  85. -(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
  86. if (nil != controlDelegate && [controlDelegate respondsToSelector:@selector(lazyScrollViewDidEndDragging:)])
  87. [controlDelegate lazyScrollViewDidEndDragging:self];
  88. }
  89. - (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
  90. if (nil != controlDelegate && [controlDelegate respondsToSelector:@selector(lazyScrollViewWillBeginDragging:)])
  91. [controlDelegate lazyScrollViewWillBeginDragging:self];
  92. }
  93. - (void) scrollViewDidScroll:(UIScrollView *)scrollView {
  94. CGFloat offset = scrollView.contentOffset.x;
  95. CGFloat size = self.frame.size.width;
  96. NSInteger newPageIndex = -1;
  97. float fractionalPage = offset/size;
  98. if (fractionalPage -(float)currentPage >= 0) {
  99. newPageIndex = (int)fractionalPage;
  100. }else{
  101. newPageIndex = (int)(ceilf(fractionalPage));
  102. }
  103. if (newPageIndex > -1 && newPageIndex < self.numberOfPages && newPageIndex != currentPage) {
  104. if (newPageIndex == 1) {
  105. NSLog(@"%f",offset);
  106. }
  107. [self setCurrentViewController:newPageIndex];
  108. }
  109. // alert delegate
  110. if (nil != controlDelegate && [controlDelegate respondsToSelector:@selector(lazyScrollViewDidScroll:at:withSelfDrivenAnimation:)]) {
  111. [controlDelegate lazyScrollViewDidScroll:self at:[self visibleRect].origin withSelfDrivenAnimation:NO];
  112. }
  113. else if (nil != controlDelegate && [controlDelegate respondsToSelector:@selector(lazyScrollViewDidScroll:at:)]) {
  114. [controlDelegate lazyScrollViewDidScroll:self at:[self visibleRect].origin];
  115. }
  116. }
  117. - (void) setCurrentViewController:(NSInteger) index {
  118. if (index == currentPage) return;
  119. currentPage = index;
  120. if (_isForbiddenLazyLoad) {
  121. [self loadControllerAtIndex:index];
  122. }else{
  123. [self loadControllerAtIndex:index];
  124. [self loadControllerAtIndex:index-1];
  125. [self loadControllerAtIndex:index+1];
  126. }
  127. if ([self.controlDelegate respondsToSelector:@selector(lazyScrollView:currentPageChanged:)])
  128. [self.controlDelegate lazyScrollView:self currentPageChanged:self.currentPage];
  129. }
  130. - (NSInteger) pageIndexByAdding:(NSInteger) offset from:(NSInteger) index {
  131. if (offset <0) {
  132. return currentPage==0?-1:index-1;
  133. }else{
  134. return currentPage==self.numberOfPages-1?-1:index+1;
  135. }
  136. }
  137. - (void) moveByPages:(NSInteger) offset animated:(BOOL) animated {
  138. NSUInteger finalIndex = [self pageIndexByAdding:offset from:self.currentPage];
  139. HZMLazyScrollViewTransition transition = (offset >= 0 ? HZMLazyScrollViewTransitionForward :
  140. HZMLazyScrollViewTransitionBackward);
  141. [self setPage:finalIndex transition:transition];
  142. }
  143. - (void) setPage:(NSInteger) newIndex{
  144. [self setPage:newIndex transition:HZMLazyScrollViewTransitionForward];
  145. }
  146. - (void) setPage:(NSInteger) newIndex transition:(HZMLazyScrollViewTransition) transition{
  147. if (newIndex == currentPage) return;
  148. // CGFloat size =(_direction==HZMLazyScrollViewDirectionHorizontal) ? self.frame.size.width : self.frame.size.height;
  149. // CGPoint finalOffset =(_direction==HZMLazyScrollViewDirectionHorizontal) ? CGPointMake(size* newIndex,0):CGPointMake(0,size* newIndex);
  150. CGFloat size =self.frame.size.width;
  151. CGPoint finalOffset =CGPointMake(size* newIndex,0);
  152. [self setCurrentViewController:newIndex];
  153. self.contentOffset = finalOffset;
  154. }
  155. - (void) setCurrentPage:(NSUInteger)newCurrentPage {
  156. [self setCurrentViewController:newCurrentPage];
  157. }
  158. - (void) loadControllerAtIndex:(NSInteger) index{
  159. if (index < 0) {
  160. return;
  161. }
  162. if (index >= self.numberOfPages) {
  163. return;
  164. }
  165. UIViewController *viewController = dataSource(index);
  166. if (viewController == nil) {
  167. return;
  168. }
  169. if (viewController.view.superview ==nil) {
  170. CGRect viewFrame = CGRectMake(0, 0, self.frame.size.width, self.frame.size.height);
  171. // if (_direction == HZMLazyScrollViewDirectionHorizontal) {
  172. viewFrame = CGRectOffset(viewFrame, self.frame.size.width * index, 0);
  173. // } else {
  174. // viewFrame = CGRectOffset(viewFrame, 0, self.frame.size.height * index);
  175. // }
  176. viewController.view.frame = viewFrame;
  177. [self addSubview:viewController.view];
  178. }
  179. }
  180. - (void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView {
  181. if (nil != controlDelegate && [controlDelegate respondsToSelector:@selector(lazyScrollViewWillBeginDecelerating:)])
  182. [controlDelegate lazyScrollViewWillBeginDecelerating:self];
  183. }
  184. - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
  185. if (nil != controlDelegate && [controlDelegate respondsToSelector:@selector(lazyScrollViewDidEndDecelerating:atPageIndex:)])
  186. [controlDelegate lazyScrollViewDidEndDecelerating:self atPageIndex:self.currentPage];
  187. }
  188. - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
  189. {
  190. if ([gestureRecognizer isKindOfClass:[UIPanGestureRecognizer class]]
  191. && [otherGestureRecognizer isKindOfClass:[UIScreenEdgePanGestureRecognizer class]])
  192. {
  193. return YES;
  194. }
  195. else
  196. {
  197. return NO;
  198. }
  199. }
  200. @end