Skip to content

Commit f204d8b

Browse files
committed
added support to load a specific page initially
1 parent 6c740b2 commit f204d8b

File tree

5 files changed

+48
-12
lines changed

5 files changed

+48
-12
lines changed

Demo/SCPageViewController.xcodeproj/xcshareddata/xcschemes/Tests.xcscheme

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,19 @@
55
<BuildAction
66
parallelizeBuildables = "YES"
77
buildImplicitDependencies = "YES">
8+
<BuildActionEntries>
9+
<BuildActionEntry
10+
buildForRunning = "YES"
11+
buildForTesting = "YES">
12+
<BuildableReference
13+
BuildableIdentifier = "primary"
14+
BlueprintIdentifier = "181957A41B37432800474C45"
15+
BuildableName = "Tests.xctest"
16+
BlueprintName = "Tests"
17+
ReferencedContainer = "container:SCPageViewController.xcodeproj">
18+
</BuildableReference>
19+
</BuildActionEntry>
20+
</BuildActionEntries>
821
</BuildAction>
922
<TestAction
1023
buildConfiguration = "Debug"

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ SCPageViewController relies on page layouters to know where to place each of the
4949
- (NSUInteger)numberOfPagesInPageViewController:(SCPageViewController *)pageViewController;
5050
5151
- (UIViewController *)pageViewController:(SCPageViewController *)pageViewController viewControllerForPageAtIndex:(NSUInteger)pageIndex;
52+
- (NSUInteger)initialPageInPageViewController:(SCPageViewController *)pageViewController;
5253
```
5354

5455
- Optionally, modify the following properties to your liking

SCPageViewController.podspec

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Pod::Spec.new do |s|
22
s.name = 'SCPageViewController'
3-
s.version = '2.0.11'
3+
s.version = '2.0.12'
44
s.platform = :ios
55
s.ios.deployment_target = '6.0'
66

@@ -33,4 +33,4 @@ Pod::Spec.new do |s|
3333

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

36-
end
36+
end

SCPageViewController/SCPageViewController.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,13 @@
213213
*/
214214
- (UIViewController *)pageViewController:(SCPageViewController *)pageViewController viewControllerForPageAtIndex:(NSUInteger)pageIndex;
215215

216+
/**
217+
* @param pageViewController The calling PageViewController
218+
* @return The initial page that should be load. Otherwise the first is chosen.
219+
*/
220+
@optional
221+
- (NSUInteger)initialPageInPageViewController:(SCPageViewController *)pageViewController;
222+
216223
@end
217224

218225

SCPageViewController/SCPageViewController.m

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@ @interface SCPageViewController () <SCPageViewControllerViewDelegate, UIScrollVi
4242

4343
@property (nonatomic, assign) NSUInteger currentPage;
4444

45+
@property (nonatomic, assign) NSUInteger initialPage;
46+
@property (nonatomic, assign) BOOL finishedLoadingInitialPage;
47+
4548
@property (nonatomic, strong) NSMutableArray *visibleControllers;
4649

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

128131
[self.view addSubview:scrollViewWrapper];
129-
132+
133+
if([self.dataSource respondsToSelector:@selector(initialPageInPageViewController:)]) {
134+
self.initialPage = [self.dataSource initialPageInPageViewController:self];
135+
self.currentPage = self.initialPage;
136+
self.finishedLoadingInitialPage = false;
137+
} else {
138+
self.finishedLoadingInitialPage = true;
139+
}
140+
130141
[self reloadData];
131142
}
132143

133144
- (void)viewWillLayoutSubviews
134145
{
135146
[super viewWillLayoutSubviews];
136-
137147
[self setLayouter:self.layouter andFocusOnIndex:self.currentPage animated:NO completion:nil];
138148
}
139149

@@ -172,7 +182,7 @@ - (void)setLayouter:(id<SCPageLayouterProtocol>)layouter
172182
[self navigateToPageAtIndex:pageIndex animated:NO completion:nil];
173183
} completion:nil];
174184
} else {
175-
[self navigateToPageAtIndex:pageIndex animated:animated completion:nil];
185+
[self navigateToPageAtIndex:pageIndex animated:animated completion:nil]; //HERE
176186
}
177187
}
178188
}
@@ -277,6 +287,10 @@ - (void)navigateToPageAtIndex:(NSUInteger)pageIndex
277287
if(completion) {
278288
completion();
279289
}
290+
291+
if(!self.finishedLoadingInitialPage && pageIndex == self.initialPage) {
292+
self.finishedLoadingInitialPage = true;
293+
}
280294
};
281295

282296
[self.scrollView setContentOffset:offset easingFunction:self.easingFunction duration:(animated ? self.animationDuration : 0.0f) completion:animationFinishedBlock];
@@ -457,9 +471,11 @@ - (void)_tilePages
457471
if(self.numberOfPages == 0) {
458472
return;
459473
}
460-
461-
self.currentPage = [self _calculateCurrentPage];
462-
474+
475+
if(self.finishedLoadingInitialPage) {
476+
self.currentPage = [self _calculateCurrentPage];
477+
}
478+
463479
NSInteger firstNeededPageIndex = self.currentPage - [self.layouter numberOfPagesToPreloadBeforeCurrentPage];
464480
firstNeededPageIndex = MAX(firstNeededPageIndex, 0);
465481

@@ -666,7 +682,7 @@ - (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL
666682
}
667683

668684
[self _updateNavigationContraints];
669-
685+
670686
if([self.delegate respondsToSelector:@selector(pageViewController:didNavigateToPageAtIndex:)]) {
671687
[self.delegate pageViewController:self didNavigateToPageAtIndex:self.currentPage];
672688
}
@@ -680,7 +696,7 @@ - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
680696
}
681697

682698
[self _updateNavigationContraints];
683-
699+
684700
if([self.delegate respondsToSelector:@selector(pageViewController:didNavigateToPageAtIndex:)]) {
685701
[self.delegate pageViewController:self didNavigateToPageAtIndex:self.currentPage];
686702
}
@@ -693,7 +709,7 @@ - (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView
693709
}
694710

695711
[self _updateNavigationContraints];
696-
712+
697713
if([self.delegate respondsToSelector:@selector(pageViewController:didNavigateToPageAtIndex:)]) {
698714
[self.delegate pageViewController:self didNavigateToPageAtIndex:self.currentPage];
699715
}
@@ -765,7 +781,6 @@ - (void)_unblockContentOffset
765781
- (NSUInteger)_calculateCurrentPage
766782
{
767783
NSMutableArray *pages = [self.pages mutableCopy];
768-
769784
for(NSUInteger i = 0; i < pages.count; i++) {
770785
SCPageViewControllerPageDetails *details = pages[i];
771786

0 commit comments

Comments
 (0)