Skip to content

Commit

Permalink
Defer loading off-screen pages. The current behavior preloads off-scr…
Browse files Browse the repository at this point in the history
…een synchronously before displaying the current page incurring unnecessary latency.
  • Loading branch information
Andy Huang committed Jul 11, 2014
1 parent 3f3013c commit a97cebe
Showing 1 changed file with 14 additions and 7 deletions.
21 changes: 14 additions & 7 deletions src/pagingscrollview/src/NIPagingScrollView.m
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,16 @@ - (void)recyclePageAtIndex:(NSInteger)pageIndex {
}
}

- (void)preloadOffscreenPages {
NSRange rangeOfVisiblePages = [self rangeOfVisiblePages];
for (NSUInteger pageIndex = rangeOfVisiblePages.location;
pageIndex < NSMaxRange(rangeOfVisiblePages); ++pageIndex) {
if (![self isDisplayingPageForIndex:pageIndex]) {
[self displayPageAtIndex:pageIndex];
}
}
}

- (void)updateVisiblePagesShouldNotifyDelegate:(BOOL)shouldNotifyDelegate {
// Before updating _centerPageIndex, notify delegate
if (shouldNotifyDelegate && (self.numberOfPages > 0) &&
Expand Down Expand Up @@ -345,13 +355,10 @@ - (void)updateVisiblePagesShouldNotifyDelegate:(BOOL)shouldNotifyDelegate {
[self displayPageAtIndex:_centerPageIndex];
}

// Add missing pages.
for (NSUInteger pageIndex = rangeOfVisiblePages.location;
pageIndex < NSMaxRange(rangeOfVisiblePages); ++pageIndex) {
if (![self isDisplayingPageForIndex:pageIndex]) {
[self displayPageAtIndex:pageIndex];
}
}
// Add missing pages after displaying the current page.
[self performSelector:@selector(preloadOffscreenPages)
withObject:nil
afterDelay:0];
} else {
_centerPageIndex = -1;
}
Expand Down

0 comments on commit a97cebe

Please sign in to comment.