Skip to content

Commit

Permalink
Optimizations to page loading
Browse files Browse the repository at this point in the history
* *Important* It is no longer necessary to call `reloadData` as part of your initialization process for SYPaginatorView - it is now called automatically when the SYPaginatorDataSource is set
* Less eager calling of `reloadData`
* Less eager loading of pageViews
  • Loading branch information
dorshorst committed Jun 4, 2012
1 parent 8ad7c3c commit 7d91f78
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 25 deletions.
43 changes: 24 additions & 19 deletions SYPaginator/SYPaginatorView.m
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ @implementation SYPaginatorView {
NSMutableDictionary *_pages;
NSMutableDictionary *_reuseablePages;
BOOL _pageControlUsed;
BOOL _pageSetViaPublicMethod;
}

@synthesize scrollView = _scrollView;
Expand Down Expand Up @@ -98,6 +99,10 @@ - (id)initWithFrame:(CGRect)frame {
[_pageControl addTarget:self action:@selector(_pageControlChanged:) forControlEvents:UIControlEventValueChanged];
[self addSubview:_pageControl];

// Setup views cache
_pages = [[NSMutableDictionary alloc] init];
_reuseablePages = [[NSMutableDictionary alloc] init];

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(_cleanup)
name:UIApplicationDidReceiveMemoryWarningNotification object:nil];
}
Expand All @@ -118,12 +123,6 @@ - (void)layoutSubviews {
}


- (void)setFrame:(CGRect)frame {
[super setFrame:frame];
self.currentPageIndex = self.currentPageIndex;
}


- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event {
if (CGRectIsEmpty(_swipeableRect) == NO) {
BOOL contains = CGRectContainsPoint(_swipeableRect, point);
Expand All @@ -146,15 +145,6 @@ - (void)reloadDataRemovingCurrentPage:(BOOL)removeCurrentPage {
_scrollView.contentSize = CGSizeMake(size.width * numberOfPages, size.height);
_pageControl.numberOfPages = (NSInteger)numberOfPages;

// Setup views
if (!_pages) {
_pages = [[NSMutableDictionary alloc] init];
}

if (!_reuseablePages) {
_reuseablePages = [[NSMutableDictionary alloc] init];
}

// Remove views
NSMutableArray *keysToRemove = [[NSMutableArray alloc] init];
[_pages enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
Expand All @@ -178,6 +168,7 @@ - (void)reloadDataRemovingCurrentPage:(BOOL)removeCurrentPage {


- (void)setCurrentPageIndex:(NSInteger)targetPage animated:(BOOL)animated {
_pageSetViaPublicMethod = YES;
[self _setCurrentPageIndex:targetPage animated:animated scroll:YES forcePreload:NO];
}

Expand Down Expand Up @@ -223,11 +214,21 @@ - (void)_pageControlChanged:(id)sender {
}


#pragma mark - Properties

- (void)setDataSource:(id<SYPaginatorViewDataSource>)dataSource {
_dataSource = dataSource;
if (_dataSource) {
[self reloadData];
}
}


#pragma mark - Private


- (void)_loadPage:(NSInteger)page {
if (!_pages || page < 0 || page >= self.numberOfPages) {
if (page < 0 || page >= self.numberOfPages) {
return;
}

Expand Down Expand Up @@ -341,6 +342,10 @@ - (void)_reusePages {


- (void)_setCurrentPageIndex:(NSInteger)targetPage animated:(BOOL)animated scroll:(BOOL)scroll forcePreload:(BOOL)forcePreload {
if (_currentPageIndex == targetPage && _pageSetViaPublicMethod != YES) {
return;
}

if (scroll && _delegate && [_delegate respondsToSelector:@selector(paginatorViewDidBeginPaging:)]) {
[_delegate paginatorViewDidBeginPaging:self];
}
Expand All @@ -363,16 +368,16 @@ - (void)_setCurrentPageIndex:(NSInteger)targetPage animated:(BOOL)animated scrol
if (scroll) {
CGFloat targetX = [self _offsetForPage:targetPage] - roundf(_pageGapWidth / 2.0f);
if (_scrollView.contentOffset.x != targetX) {
CGSize size = _scrollView.bounds.size;
CGRect rect = CGRectMake(targetX, 0.0f, size.width, size.height);
[_scrollView scrollRectToVisible:rect animated:animated];
[_scrollView setContentOffset:CGPointMake(targetX, 0.0f) animated:animated];
_pageControlUsed = YES;
}

if (_delegate && [_delegate respondsToSelector:@selector(paginatorView:didScrollToPageAtIndex:)]) {
[_delegate paginatorView:self didScrollToPageAtIndex:self.currentPageIndex];
}
}

_pageSetViaPublicMethod = NO;
}


Expand Down
6 changes: 0 additions & 6 deletions SYPaginator/SYPaginatorViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,6 @@ - (void)viewDidLoad {
}


- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[_paginator reloadData];
}


#pragma mark - Private

- (void)_initialize {
Expand Down

0 comments on commit 7d91f78

Please sign in to comment.