Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions Classes/InfinitePagingView.h
Original file line number Diff line number Diff line change
Expand Up @@ -100,4 +100,26 @@ typedef enum {
*/
- (void)scrollToNextPage;

/*!
Scroll to specific page.
@method scrollToPage:animated:
@param NSInteger page
@param BOOL animated
*/
- (void)scrollToPage:(NSInteger)page animated:(BOOL)animated;

/*!
Get page view at a specfic index.
@method pageWithIndex:
@param NSInteger index
*/
- (UIView *)pageWithIndex:(NSInteger)index;

/*!
Get page view at a specfic location.
@method pageAtLocation:
@param CGPoint location
*/
- (UIView *)pageAtLocation:(CGPoint)location;

@end
77 changes: 67 additions & 10 deletions Classes/InfinitePagingView.m
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ @implementation InfinitePagingView
UIScrollView *_innerScrollView;
NSMutableArray *_pageViews;
NSInteger _lastPageIndex;
NSInteger _destinationPageIndex;
}

@synthesize pageSize = _pageSize;
Expand Down Expand Up @@ -65,6 +66,8 @@ - (void)setFrame:(CGRect)frame
_scrollDirection = InfinitePagingViewHorizonScrollDirection;
[self addSubview:_innerScrollView];
self.pageSize = frame.size;

_destinationPageIndex = -1;
}
}

Expand Down Expand Up @@ -94,6 +97,7 @@ - (void)addPageView:(UIView *)pageView
if (nil == _pageViews) {
_pageViews = [NSMutableArray array];
}
pageView.tag = _pageViews.count;
[_pageViews addObject:pageView];
[self layoutPages];
}
Expand Down Expand Up @@ -164,9 +168,48 @@ - (void)scrollToDirection:(NSInteger)moveDirection animated:(BOOL)animated
_innerScrollView.frame.size.width, _innerScrollView.frame.size.height);

}

[_innerScrollView scrollRectToVisible:adjustScrollRect animated:animated];
}

- (void)scrollToPage:(NSInteger)page animated:(BOOL)animated
{
if (!_pageViews.count || page < 0) {
return;
}

NSInteger realPage = page % _pageViews.count;
UIView *pageView = [self pageWithIndex:realPage];
if (!pageView) {
return;
}

[_innerScrollView scrollRectToVisible:pageView.frame animated:animated];
_currentPageIndex = page;
if (!animated) {
[self _pageChanges];
}
}

- (UIView *)pageWithIndex:(NSInteger)index {
for (UIView *p in _pageViews) {
if (p.tag == index) {
return p;
}
}
return nil;
}

- (UIView *)pageAtLocation:(CGPoint)location {
CGPoint point = [_innerScrollView convertPoint:location fromView:self];
for (UIView *page in _pageViews) {
if (CGRectContainsPoint(page.frame, point)) {
return page;
}
}
return nil;
}

#pragma mark - UIScrollViewDelegate methods

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
Expand Down Expand Up @@ -197,8 +240,7 @@ - (void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView
}
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
- (void)_pageChanges {
NSInteger pageIndex = 0;
if (_scrollDirection == InfinitePagingViewHorizonScrollDirection) {
pageIndex = _innerScrollView.contentOffset.x / _innerScrollView.frame.size.width;
Expand Down Expand Up @@ -247,7 +289,6 @@ - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView

NSUInteger idx = 0;
for (UIView *pageView in _pageViews) {
UIView *pageView = [_pageViews objectAtIndex:idx];
if (_scrollDirection == InfinitePagingViewHorizonScrollDirection) {
pageView.center = CGPointMake(idx * _innerScrollView.frame.size.width + _innerScrollView.frame.size.width / 2, _innerScrollView.center.y);
} else {
Expand All @@ -256,16 +297,32 @@ - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
++idx;
}
[self scrollToDirection:moveDirection animated:NO];

_lastPageIndex = pageIndex;

if (nil != delegate && [delegate respondsToSelector:@selector(pagingView:didEndDecelerating:atPageIndex:)]) {
_currentPageIndex += moveDirection;
if (_currentPageIndex < 0) {
_currentPageIndex = _pageViews.count - 1;
} else if (_currentPageIndex >= _pageViews.count) {
_currentPageIndex = 0;
CGRect visibleRect = CGRectMake(
_innerScrollView.contentOffset.x,
_innerScrollView.contentOffset.y,
CGRectGetWidth(_innerScrollView.frame),
CGRectGetHeight(_innerScrollView.frame));

for (UIView *pageView in _pageViews) {
if (CGRectIntersectsRect(visibleRect, pageView.frame)) {
self.currentPageIndex = pageView.tag;
break;
}
}
}

- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView
{
[self _pageChanges];
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
[self _pageChanges];
if (nil != delegate && [delegate respondsToSelector:@selector(pagingView:didEndDecelerating:atPageIndex:)]) {
[delegate pagingView:self didEndDecelerating:_innerScrollView atPageIndex:_currentPageIndex];
}
}
Expand Down