Created
August 31, 2011 14:02
-
-
Save pratikshabhisikar/1183604 to your computer and use it in GitHub Desktop.
Detecting Left and Right scroll in UIScrollView with paging enabled.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
typedef enum { | |
kScrollDirectionLeft = -1, | |
kScrollDirectionNone, | |
kScrollDirectionRight | |
}ScrollDirection; | |
-(void) scrollEffectInDirection:(ScrollDirection) direction{ | |
ContentView *previousPageView = (ContentView *)[self.pageScrollView viewWithTag:kPreviousPageTag]; | |
ContentView *currentPageView = (ContentView *)[self.pageScrollView viewWithTag:kCurrentPageTag]; | |
ContentView *nextPageView = (ContentView *)[self.pageScrollView viewWithTag:kNextPageTag]; | |
switch (direction) { | |
case kScrollDirectionNone: | |
[self fillPageView:currentPageView withPageNumber:self.currentPage.pageNumber]; | |
[self fillPageView:previousPageView withPageNumber:self.currentPage.pageNumber - 1]; | |
[self fillPageView:nextPageView withPageNumber:self.currentPage.pageNumber + 1]; | |
break; | |
case kScrollDirectionRight:{ | |
// The previous page is free. Make it the next page. | |
CGRect previousPageViewRect = nextPageView.frame; | |
previousPageViewRect.origin.x += PAGE_WIDTH; | |
previousPageView.frame = previousPageViewRect; | |
// Fill the page. | |
[self fillPageView:previousPageView withPageNumber:self.currentPage.pageNumber + 1]; | |
// Rearrange the page tag's. | |
nextPageView.tag = currentPageView.tag; | |
currentPageView.tag = previousPageView.tag; | |
previousPageView.tag = kNextPageTag; | |
// Update the page control. | |
[pageControl gotoNextPage]; | |
} | |
break; | |
case kScrollDirectionLeft:{ | |
// The next page is free. Make it the previous page. | |
CGRect nextPageViewRect = previousPageView.frame; | |
nextPageViewRect.origin.x -= PAGE_WIDTH; | |
nextPageView.frame = nextPageViewRect; | |
// Fill the page. | |
[self fillPageView:nextPageView withPageNumber:self.currentPage.pageNumber - 1]; | |
// Rearrange the page tag's. | |
previousPageView.tag = currentPageView.tag; | |
currentPageView.tag = nextPageView.tag; | |
nextPageView.tag = kPreviousPageTag; | |
// Update the page control. | |
[pageControl gotoPreviousPage]; | |
} | |
break; | |
default: | |
break; | |
} | |
[self inspectPageHighlightion]; | |
} | |
-(void) scrollViewDidScroll:(UIScrollView *)scrollView { | |
// Get the x of current page. | |
int currentPageX = (self.currentPage.pageNumber - 1) * PAGE_WIDTH; | |
if (self.pageScrollView.contentOffset.x >= currentPageX + PAGE_WIDTH) { | |
// User scrolled to right. | |
[self scrollEffectInDirection:kScrollDirectionRight]; | |
}else if (self.pageScrollView.contentOffset.x <= currentPageX - PAGE_WIDTH) { | |
// User scrolled to left. | |
[self scrollEffectInDirection:kScrollDirectionLeft]; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment