123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253 |
- //
- // DMLazyScrollView.m
- // Lazy Loading UIScrollView for iOS
- //
- // Created by Daniele Margutti (me@danielemargutti.com) on 24/11/12.
- // Copyright (c) 2012 http://www.danielemargutti.com. All rights reserved.
- // Distribuited under MIT License
- //
- #import "HZMLazyScrollView.h"
- enum {
- HZMLazyScrollViewScrollDirectionBackward = 0,
- HZMLazyScrollViewScrollDirectionForward = 1
- }; typedef NSUInteger HZMLazyScrollViewScrollDirection;
- #define kDMLazyScrollViewTransitionDuration 0.2
- @interface HZMLazyScrollView() <UIScrollViewDelegate> {
- NSUInteger currentPage;
- BOOL isManualAnimating;
- }
- @end
- @implementation HZMLazyScrollView
- @synthesize numberOfPages,currentPage;
- @synthesize dataSource,controlDelegate;
- - (id)init {
- return [self initWithFrame:CGRectZero];
- }
- - (id)initWithFrame:(CGRect)frame
- {
- self = [super initWithFrame:frame];
- if (self) {
- [self initializeControl];
- }
- return self;
- }
- - (BOOL)hasMultiplePages {
- return self.numberOfPages > 1;
- }
- - (void) awakeFromNib {
- [super awakeFromNib];
- [self initializeControl];
- }
- - (void) initializeControl {
- self.showsHorizontalScrollIndicator = NO;
- self.showsVerticalScrollIndicator = NO;
- self.pagingEnabled = YES;
- self.delegate = self;
- self.contentSize = CGSizeMake(self.frame.size.width, self.contentSize.height);
- self.bounces = NO;
- self.scrollsToTop = NO;
- currentPage = NSNotFound;
- }
- - (void) setNumberOfPages:(NSUInteger)pages {
- // if (pages != numberOfPages) {
- numberOfPages = pages;
- NSInteger offset = [self hasMultiplePages] ? numberOfPages : 1;
- self.contentSize = CGSizeMake(self.frame.size.width * offset,
- self.contentSize.height);
- [self reloadData];
- // }
- }
- -(void)setFrame:(CGRect)frame{
- [super setFrame:frame];
-
- if (self.numberOfPages) {
- [self setNumberOfPages:self.numberOfPages];
- }
- }
- - (void) reloadData {
- [self setCurrentViewController:currentPage!=NSNotFound?currentPage:0];
- }
- - (void) layoutSubviews {
- [super layoutSubviews];
- }
- - (CGRect) visibleRect {
- CGRect visibleRect;
- visibleRect.origin = self.contentOffset;
- visibleRect.size = self.bounds.size;
- return visibleRect;
- }
- - (CGPoint) createPoint:(CGFloat) size {
- // if (_direction == HZMLazyScrollViewDirectionHorizontal) {
- return CGPointMake(size, 0);
- // } else {
- // return CGPointMake(0, size);
- // }
- }
- -(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
- if (nil != controlDelegate && [controlDelegate respondsToSelector:@selector(lazyScrollViewDidEndDragging:)])
- [controlDelegate lazyScrollViewDidEndDragging:self];
- }
- - (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
- if (nil != controlDelegate && [controlDelegate respondsToSelector:@selector(lazyScrollViewWillBeginDragging:)])
- [controlDelegate lazyScrollViewWillBeginDragging:self];
- }
- - (void) scrollViewDidScroll:(UIScrollView *)scrollView {
- CGFloat offset = scrollView.contentOffset.x;
- CGFloat size = self.frame.size.width;
- NSInteger newPageIndex = -1;
- float fractionalPage = offset/size;
- if (fractionalPage -(float)currentPage >= 0) {
- newPageIndex = (int)fractionalPage;
- }else{
- newPageIndex = (int)(ceilf(fractionalPage));
- }
-
- if (newPageIndex > -1 && newPageIndex < self.numberOfPages && newPageIndex != currentPage) {
- if (newPageIndex == 1) {
- NSLog(@"%f",offset);
- }
- [self setCurrentViewController:newPageIndex];
- }
-
-
- // alert delegate
- if (nil != controlDelegate && [controlDelegate respondsToSelector:@selector(lazyScrollViewDidScroll:at:withSelfDrivenAnimation:)]) {
- [controlDelegate lazyScrollViewDidScroll:self at:[self visibleRect].origin withSelfDrivenAnimation:NO];
- }
- else if (nil != controlDelegate && [controlDelegate respondsToSelector:@selector(lazyScrollViewDidScroll:at:)]) {
- [controlDelegate lazyScrollViewDidScroll:self at:[self visibleRect].origin];
- }
- }
- - (void) setCurrentViewController:(NSInteger) index {
- if (index == currentPage) return;
- currentPage = index;
-
- if (_isForbiddenLazyLoad) {
- [self loadControllerAtIndex:index];
- }else{
- [self loadControllerAtIndex:index];
- [self loadControllerAtIndex:index-1];
- [self loadControllerAtIndex:index+1];
- }
-
- if ([self.controlDelegate respondsToSelector:@selector(lazyScrollView:currentPageChanged:)])
- [self.controlDelegate lazyScrollView:self currentPageChanged:self.currentPage];
- }
- - (NSInteger) pageIndexByAdding:(NSInteger) offset from:(NSInteger) index {
- if (offset <0) {
- return currentPage==0?-1:index-1;
- }else{
- return currentPage==self.numberOfPages-1?-1:index+1;
- }
- }
- - (void) moveByPages:(NSInteger) offset animated:(BOOL) animated {
- NSUInteger finalIndex = [self pageIndexByAdding:offset from:self.currentPage];
- HZMLazyScrollViewTransition transition = (offset >= 0 ? HZMLazyScrollViewTransitionForward :
- HZMLazyScrollViewTransitionBackward);
- [self setPage:finalIndex transition:transition];
- }
- - (void) setPage:(NSInteger) newIndex{
- [self setPage:newIndex transition:HZMLazyScrollViewTransitionForward];
- }
- - (void) setPage:(NSInteger) newIndex transition:(HZMLazyScrollViewTransition) transition{
- if (newIndex == currentPage) return;
- // CGFloat size =(_direction==HZMLazyScrollViewDirectionHorizontal) ? self.frame.size.width : self.frame.size.height;
- // CGPoint finalOffset =(_direction==HZMLazyScrollViewDirectionHorizontal) ? CGPointMake(size* newIndex,0):CGPointMake(0,size* newIndex);
-
- CGFloat size =self.frame.size.width;
- CGPoint finalOffset =CGPointMake(size* newIndex,0);
-
- [self setCurrentViewController:newIndex];
- self.contentOffset = finalOffset;
- }
- - (void) setCurrentPage:(NSUInteger)newCurrentPage {
- [self setCurrentViewController:newCurrentPage];
- }
- - (void) loadControllerAtIndex:(NSInteger) index{
- if (index < 0) {
- return;
- }
-
- if (index >= self.numberOfPages) {
- return;
- }
-
-
- UIViewController *viewController = dataSource(index);
- if (viewController == nil) {
- return;
- }
- if (viewController.view.superview ==nil) {
- CGRect viewFrame = CGRectMake(0, 0, self.frame.size.width, self.frame.size.height);
- // if (_direction == HZMLazyScrollViewDirectionHorizontal) {
- viewFrame = CGRectOffset(viewFrame, self.frame.size.width * index, 0);
- // } else {
- // viewFrame = CGRectOffset(viewFrame, 0, self.frame.size.height * index);
- // }
-
- viewController.view.frame = viewFrame;
-
- [self addSubview:viewController.view];
- }
- }
- - (void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView {
- if (nil != controlDelegate && [controlDelegate respondsToSelector:@selector(lazyScrollViewWillBeginDecelerating:)])
- [controlDelegate lazyScrollViewWillBeginDecelerating:self];
- }
- - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
- if (nil != controlDelegate && [controlDelegate respondsToSelector:@selector(lazyScrollViewDidEndDecelerating:atPageIndex:)])
- [controlDelegate lazyScrollViewDidEndDecelerating:self atPageIndex:self.currentPage];
- }
- - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
- {
- if ([gestureRecognizer isKindOfClass:[UIPanGestureRecognizer class]]
- && [otherGestureRecognizer isKindOfClass:[UIScreenEdgePanGestureRecognizer class]])
- {
- return YES;
- }
- else
- {
- return NO;
- }
- }
- @end
|