Skip to content

Commit fe21ae4

Browse files
committed
Exposed a new layouter method that moves the current page when inserting a new one at the same index (#18)
1 parent b59a4bf commit fe21ae4

File tree

3 files changed

+22
-1
lines changed

3 files changed

+22
-1
lines changed

SCPageViewController/Layouters/SCPageLayouter.m

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,11 @@ - (void)animatePageInsertionAtIndex:(NSUInteger)index
9191
}];
9292
}
9393

94+
- (BOOL)shouldPreserveOffsetForInsertionAtIndex:(NSUInteger)index pageViewController:(SCPageViewController *)pageViewController
95+
{
96+
return YES;
97+
}
98+
9499
- (void)animatePageDeletionAtIndex:(NSUInteger)index
95100
viewController:(UIViewController *)viewController
96101
pageViewController:(SCPageViewController *)pageViewController

SCPageViewController/Layouters/SCPageLayouterProtocol.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,16 @@ typedef NS_OPTIONS(NSUInteger, SCPageLayouterNavigationContraintType) {
160160
pageViewController:(SCPageViewController *)pageViewController
161161
completion:(void(^)())completion;
162162

163+
/** Called bue the pageViewController when inserting a new page that
164+
* would affect the current content offset
165+
*
166+
* @param index The index where a new page will be inserted
167+
* @param pageViewController the calling pageViewController
168+
* @return a boolean indicating whether the current offset should be kept
169+
*/
170+
- (BOOL)shouldPreserveOffsetForInsertionAtIndex:(NSUInteger)index
171+
pageViewController:(SCPageViewController *)pageViewController;
172+
163173

164174
/** Called by the pageViewController when it receives a delete page animated
165175
* request.

SCPageViewController/SCPageViewController.m

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1101,7 +1101,13 @@ - (void)insertPagesAtIndexes:(NSIndexSet *)indexes animated:(BOOL)animated compl
11011101

11021102
__block BOOL shouldAdjustOffset = NO;
11031103
[indexes enumerateIndexesUsingBlock:^(NSUInteger pageIndex, BOOL *stop) {
1104-
if(pageIndex <= self.currentPage) {
1104+
1105+
BOOL shouldKeepCurrentPage = YES;
1106+
if([self.layouter respondsToSelector:@selector(shouldPreserveOffsetForInsertionAtIndex:pageViewController:)]) {
1107+
shouldKeepCurrentPage = [self.layouter shouldPreserveOffsetForInsertionAtIndex:pageIndex pageViewController:self];
1108+
}
1109+
1110+
if((shouldKeepCurrentPage && pageIndex <= self.currentPage) || (!shouldKeepCurrentPage && pageIndex < self.currentPage)) {
11051111
shouldAdjustOffset = YES;
11061112
*stop = YES;
11071113
}

0 commit comments

Comments
 (0)