Skip to content
This repository has been archived by the owner on Nov 8, 2017. It is now read-only.

Commit

Permalink
Merge pull request #84 from MerrickSapsford/infinite-scroll-fix
Browse files Browse the repository at this point in the history
Infinite scroll fix
  • Loading branch information
msaps authored Aug 16, 2016
2 parents 3ff0bb8 + 913ad7b commit 21afe23
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,12 @@

#import "MSSPageViewController.h"

@interface MSSPageViewController () <UIScrollViewDelegate>
@interface MSSPageViewController ()

/**
The default page index of the page view controller.
*/
@property (nonatomic, assign, readonly) NSInteger defaultPageIndex;
/**
The current active page index of the page view controller.
*/
@property (nonatomic, assign) NSInteger currentPage;
/**
Whether user interaction is currently enabled on the page view controller
*/
Expand Down
11 changes: 10 additions & 1 deletion Source/Components/MSSPageViewController/MSSPageViewController.h
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ willDisplayInitialViewController:(nonnull UIViewController *)viewController;

@end

@interface MSSPageViewController : UIViewController <MSSPageViewControllerDelegate, MSSPageViewControllerDataSource>
@interface MSSPageViewController : UIViewController <MSSPageViewControllerDelegate, MSSPageViewControllerDataSource, UIScrollViewDelegate>

/**
The object that acts as a data source for the page view controller.
Expand All @@ -122,6 +122,10 @@ willDisplayInitialViewController:(nonnull UIViewController *)viewController;
The number of pages in the page view controller.
*/
@property (nonatomic, assign ,readonly) NSInteger numberOfPages;
/**
The current active page index of the page view controller.
*/
@property (nonatomic, assign, readonly) NSInteger currentPage;
/**
The view controllers within the page view controller.
*/
Expand Down Expand Up @@ -190,6 +194,11 @@ willDisplayInitialViewController:(nonnull UIViewController *)viewController;
animated:(BOOL)animated
completion:(nullable MSSPageViewControllerPageMoveCompletion)completion;


/** UIScrollViewDelegate */
- (void)scrollViewWillBeginDragging:(nonnull UIScrollView *)scrollView NS_REQUIRES_SUPER;
- (void)scrollViewDidScroll:(nonnull UIScrollView *)scrollView NS_REQUIRES_SUPER;

@end

@interface UIViewController (MSSPageViewController)
Expand Down
16 changes: 13 additions & 3 deletions Source/Components/MSSPageViewController/MSSPageViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ - (void)setUpPages {
[self setUpViewControllers:self.viewControllers];

_numberOfPages = self.viewControllers.count;
self.currentPage = self.defaultPageIndex;
_currentPage = self.defaultPageIndex;

if ([self.delegate respondsToSelector:@selector(pageViewController:didPrepareViewControllers:)]) {
[self.delegate pageViewController:self didPrepareViewControllers:self.viewControllers];
Expand Down Expand Up @@ -284,6 +284,14 @@ - (UIScrollView *)scrollView {
}

- (void)updateCurrentPage:(NSInteger)currentPage {
if (self.infiniteScrollEnabled) {
if (currentPage >= self.numberOfPages) {
currentPage = 0;
} else if (currentPage < 0) {
currentPage = self.numberOfPages - 1;
}
}

if (currentPage >= 0 && currentPage < self.numberOfPages) {
_currentPage = currentPage;
if ([self.delegate respondsToSelector:@selector(pageViewController:didScrollToPage:)]) {
Expand All @@ -309,13 +317,15 @@ - (void)scrollViewDidScroll:(UIScrollView *)scrollView {
currentPagePosition > _previousPagePosition ?
MSSPageViewControllerScrollDirectionForward : MSSPageViewControllerScrollDirectionBackward;

// check if reached a page incase page view controller delegate does not report
// check if reached a page as page view controller delegate does not report reliably
// occurs when scrollview is continuously dragged
if (!self.isAnimatingPageUpdate) {
if (direction == MSSPageViewControllerScrollDirectionForward && currentPagePosition >= self.currentPage + 1) {
[self updateCurrentPage:self.currentPage + 1];
return; // ignore update if we've changed page
} else if (direction == MSSPageViewControllerScrollDirectionBackward && currentPagePosition <= self.currentPage - 1) {
[self updateCurrentPage:self.currentPage - 1];
return;
}
}

Expand Down Expand Up @@ -352,7 +362,7 @@ - (void)scrollViewDidScroll:(UIScrollView *)scrollView {
currentPagePosition = MAX(0.0f, MIN(currentPagePosition, self.numberOfPages - 1));
}
}

// check whether updates are allowed
if (self.scrollUpdatesEnabled && self.allowScrollViewUpdates) {
if ([self.delegate respondsToSelector:@selector(pageViewController:didScrollToPageOffset:direction:)]) {
Expand Down

0 comments on commit 21afe23

Please sign in to comment.