Skip to content

Commit

Permalink
added support to load a specific page initially
Browse files Browse the repository at this point in the history
  • Loading branch information
neilabdev committed Nov 18, 2017
1 parent 6c740b2 commit f204d8b
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,19 @@
<BuildAction
parallelizeBuildables = "YES"
buildImplicitDependencies = "YES">
<BuildActionEntries>
<BuildActionEntry
buildForRunning = "YES"
buildForTesting = "YES">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "181957A41B37432800474C45"
BuildableName = "Tests.xctest"
BlueprintName = "Tests"
ReferencedContainer = "container:SCPageViewController.xcodeproj">
</BuildableReference>
</BuildActionEntry>
</BuildActionEntries>
</BuildAction>
<TestAction
buildConfiguration = "Debug"
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ SCPageViewController relies on page layouters to know where to place each of the
- (NSUInteger)numberOfPagesInPageViewController:(SCPageViewController *)pageViewController;
- (UIViewController *)pageViewController:(SCPageViewController *)pageViewController viewControllerForPageAtIndex:(NSUInteger)pageIndex;
- (NSUInteger)initialPageInPageViewController:(SCPageViewController *)pageViewController;
```

- Optionally, modify the following properties to your liking
Expand Down
4 changes: 2 additions & 2 deletions SCPageViewController.podspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Pod::Spec.new do |s|
s.name = 'SCPageViewController'
s.version = '2.0.11'
s.version = '2.0.12'
s.platform = :ios
s.ios.deployment_target = '6.0'

Expand Down Expand Up @@ -33,4 +33,4 @@ Pod::Spec.new do |s|

s.dependency 'SCScrollView', '~> 1.1'

end
end
7 changes: 7 additions & 0 deletions SCPageViewController/SCPageViewController.h
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,13 @@
*/
- (UIViewController *)pageViewController:(SCPageViewController *)pageViewController viewControllerForPageAtIndex:(NSUInteger)pageIndex;

/**
* @param pageViewController The calling PageViewController
* @return The initial page that should be load. Otherwise the first is chosen.
*/
@optional
- (NSUInteger)initialPageInPageViewController:(SCPageViewController *)pageViewController;

@end


Expand Down
35 changes: 25 additions & 10 deletions SCPageViewController/SCPageViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ @interface SCPageViewController () <SCPageViewControllerViewDelegate, UIScrollVi

@property (nonatomic, assign) NSUInteger currentPage;

@property (nonatomic, assign) NSUInteger initialPage;
@property (nonatomic, assign) BOOL finishedLoadingInitialPage;

@property (nonatomic, strong) NSMutableArray *visibleControllers;

@property (nonatomic, assign) BOOL isContentOffsetBlocked;
Expand Down Expand Up @@ -126,14 +129,21 @@ - (void)viewDidLoad
[scrollViewWrapper addSubview:self.scrollView];

[self.view addSubview:scrollViewWrapper];


if([self.dataSource respondsToSelector:@selector(initialPageInPageViewController:)]) {
self.initialPage = [self.dataSource initialPageInPageViewController:self];
self.currentPage = self.initialPage;
self.finishedLoadingInitialPage = false;
} else {
self.finishedLoadingInitialPage = true;
}

[self reloadData];
}

- (void)viewWillLayoutSubviews
{
[super viewWillLayoutSubviews];

[self setLayouter:self.layouter andFocusOnIndex:self.currentPage animated:NO completion:nil];
}

Expand Down Expand Up @@ -172,7 +182,7 @@ - (void)setLayouter:(id<SCPageLayouterProtocol>)layouter
[self navigateToPageAtIndex:pageIndex animated:NO completion:nil];
} completion:nil];
} else {
[self navigateToPageAtIndex:pageIndex animated:animated completion:nil];
[self navigateToPageAtIndex:pageIndex animated:animated completion:nil]; //HERE
}
}
}
Expand Down Expand Up @@ -277,6 +287,10 @@ - (void)navigateToPageAtIndex:(NSUInteger)pageIndex
if(completion) {
completion();
}

if(!self.finishedLoadingInitialPage && pageIndex == self.initialPage) {
self.finishedLoadingInitialPage = true;
}
};

[self.scrollView setContentOffset:offset easingFunction:self.easingFunction duration:(animated ? self.animationDuration : 0.0f) completion:animationFinishedBlock];
Expand Down Expand Up @@ -457,9 +471,11 @@ - (void)_tilePages
if(self.numberOfPages == 0) {
return;
}

self.currentPage = [self _calculateCurrentPage];


if(self.finishedLoadingInitialPage) {
self.currentPage = [self _calculateCurrentPage];
}

NSInteger firstNeededPageIndex = self.currentPage - [self.layouter numberOfPagesToPreloadBeforeCurrentPage];
firstNeededPageIndex = MAX(firstNeededPageIndex, 0);

Expand Down Expand Up @@ -666,7 +682,7 @@ - (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL
}

[self _updateNavigationContraints];

if([self.delegate respondsToSelector:@selector(pageViewController:didNavigateToPageAtIndex:)]) {
[self.delegate pageViewController:self didNavigateToPageAtIndex:self.currentPage];
}
Expand All @@ -680,7 +696,7 @@ - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
}

[self _updateNavigationContraints];

if([self.delegate respondsToSelector:@selector(pageViewController:didNavigateToPageAtIndex:)]) {
[self.delegate pageViewController:self didNavigateToPageAtIndex:self.currentPage];
}
Expand All @@ -693,7 +709,7 @@ - (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView
}

[self _updateNavigationContraints];

if([self.delegate respondsToSelector:@selector(pageViewController:didNavigateToPageAtIndex:)]) {
[self.delegate pageViewController:self didNavigateToPageAtIndex:self.currentPage];
}
Expand Down Expand Up @@ -765,7 +781,6 @@ - (void)_unblockContentOffset
- (NSUInteger)_calculateCurrentPage
{
NSMutableArray *pages = [self.pages mutableCopy];

for(NSUInteger i = 0; i < pages.count; i++) {
SCPageViewControllerPageDetails *details = pages[i];

Expand Down

0 comments on commit f204d8b

Please sign in to comment.