Skip to content

Commit

Permalink
#42 Small tweaks to the initial page implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
stefanceriu committed Nov 21, 2017
1 parent f204d8b commit 3413dca
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 21 deletions.
7 changes: 7 additions & 0 deletions Demo/ObjCDemo/SCRootViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ - (void)viewDidLoad
[self.pageViewController.view setFrame:self.view.bounds];
[self.view addSubview:self.pageViewController.view];
[self.pageViewController didMoveToParentViewController:self];

[self _updateViewControllerDetails];
}

#pragma mark - SCPageViewControllerDataSource
Expand All @@ -77,6 +79,11 @@ - (UIViewController *)pageViewController:(SCPageViewController *)pageViewControl
return viewController;
}

- (NSUInteger)initialPageInPageViewController:(SCPageViewController *)pageViewController
{
return 4;
}

#pragma mark - SCPageViewControllerDelegate

- (void)pageViewController:(SCPageViewController *)pageViewController didNavigateToOffset:(CGPoint)offset
Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,13 @@ SCPageViewController relies on page layouters to know where to place each of the
[self.pageViewController setLayouter:[[SCPageLayouter alloc] init] animated:NO completion:nil];
```
- Implement the SCPageViewControllerDataSource which defines the total number of pages and the view controllers to be used for each of them.
- Implement the SCPageViewControllerDataSource to define the total number of pages, the view controllers to be used for each of them and which one show be displayed first.
```objc
- (NSUInteger)numberOfPagesInPageViewController:(SCPageViewController *)pageViewController;
- (UIViewController *)pageViewController:(SCPageViewController *)pageViewController viewControllerForPageAtIndex:(NSUInteger)pageIndex;
- (NSUInteger)initialPageInPageViewController:(SCPageViewController *)pageViewController;
```

Expand Down
2 changes: 1 addition & 1 deletion SCPageViewController/SCPageViewController.h
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@

/**
* @param pageViewController The calling PageViewController
* @return The initial page that should be load. Otherwise the first is chosen.
* @return The initial page that should be loaded, otherwise the first is chosen.
*/
@optional
- (NSUInteger)initialPageInPageViewController:(SCPageViewController *)pageViewController;
Expand Down
38 changes: 19 additions & 19 deletions SCPageViewController/SCPageViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,6 @@ @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 All @@ -60,6 +57,8 @@ @interface SCPageViewController () <SCPageViewControllerViewDelegate, UIScrollVi

@property (nonatomic, strong) NSIndexSet *insertionIndexes;

@property (nonatomic, strong) NSNumber *initialPageIndex;

@end

@implementation SCPageViewController
Expand Down Expand Up @@ -129,21 +128,19 @@ - (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;
}


if([self.dataSource respondsToSelector:@selector(initialPageInPageViewController:)]) {
self.initialPageIndex = @([self.dataSource initialPageInPageViewController:self]);
self.currentPage = self.initialPageIndex.unsignedIntegerValue;
}

[self reloadData];
}

- (void)viewWillLayoutSubviews
{
[super viewWillLayoutSubviews];

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

Expand All @@ -155,6 +152,13 @@ - (void)viewWillAppear:(BOOL)animated
[self _tilePages];
}

- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];

self.initialPageIndex = nil;
}

- (void)viewDidDisappear:(BOOL)animated
{
[super viewDidDisappear:animated];
Expand Down Expand Up @@ -287,10 +291,6 @@ - (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 @@ -472,9 +472,9 @@ - (void)_tilePages
return;
}

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

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

0 comments on commit 3413dca

Please sign in to comment.