Skip to content

Commit

Permalink
ATPagingView: allow to set the page index
Browse files Browse the repository at this point in the history
  • Loading branch information
andreyvit committed Apr 26, 2011
1 parent ba5bee4 commit b69e6fe
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 2 deletions.
2 changes: 1 addition & 1 deletion ATPagingView/ATPagingView.h
Expand Up @@ -39,7 +39,7 @@

@property(nonatomic, readonly) NSInteger pageCount;

@property(nonatomic, assign, readonly) NSInteger currentPageIndex;
@property(nonatomic, assign) NSInteger currentPageIndex;
@property(nonatomic, assign, readonly) NSInteger previousPageIndex; // only for reading inside currentPageDidChangeInPagingView

@property(nonatomic, assign, readonly) NSInteger firstVisiblePageIndex;
Expand Down
31 changes: 30 additions & 1 deletion ATPagingView/ATPagingView.m
Expand Up @@ -5,6 +5,9 @@
#import "ATPagingView.h"


//#define AT_PAGING_VIEW_TRACE_LAYOUT


@interface ATPagingView () <UIScrollViewDelegate>

- (void)configurePages;
Expand Down Expand Up @@ -51,7 +54,7 @@ - (void)commonInit {
// someone uses ATPagingView in a non-fullscreen layout.
self.clipsToBounds = YES;

_scrollView = [[UIScrollView alloc] initWithFrame:CGRectZero];
_scrollView = [[UIScrollView alloc] initWithFrame:[self frameForScrollView]];
_scrollView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
_scrollView.pagingEnabled = YES;
_scrollView.backgroundColor = [UIColor blackColor];
Expand Down Expand Up @@ -137,11 +140,23 @@ - (void)configurePages {

CGSize contentSize = CGSizeMake(_scrollView.frame.size.width * _pageCount, _scrollView.frame.size.height);
if (!CGSizeEqualToSize(_scrollView.contentSize, contentSize)) {
#ifdef AT_PAGING_VIEW_TRACE_LAYOUT
NSLog(@"configurePages: _scrollView.frame == %@, setting _scrollView.contentSize = %@",
NSStringFromCGRect(_scrollView.frame), NSStringFromCGSize(contentSize));
#endif
_scrollView.contentSize = contentSize;
_scrollView.contentOffset = CGPointMake(_scrollView.frame.size.width * _currentPageIndex, 0);
} else {
#ifdef AT_PAGING_VIEW_TRACE_LAYOUT
NSLog(@"configurePages: _scrollView.frame == %@", NSStringFromCGRect(_scrollView.frame));
#endif
}

CGRect visibleBounds = _scrollView.bounds;
NSInteger newPageIndex = MIN(MAX(floorf(CGRectGetMidX(visibleBounds) / CGRectGetWidth(visibleBounds)), 0), _pageCount - 1);
#ifdef AT_PAGING_VIEW_TRACE_LAYOUT
NSLog(@"newPageIndex == %d", newPageIndex);
#endif

// calculate which pages are visible
int firstVisiblePage = self.firstVisiblePageIndex;
Expand Down Expand Up @@ -266,6 +281,20 @@ - (void)didRotate {
}


#pragma mark -
#pragma mark Page navigation

- (void)setCurrentPageIndex:(NSInteger)newPageIndex {
#ifdef AT_PAGING_VIEW_TRACE_LAYOUT
NSLog(@"setCurrentPageIndex(%d): _scrollView.frame == %@", newPageIndex, NSStringFromCGRect(_scrollView.frame));
#endif
if (_scrollView.frame.size.width > 0 && fabsf(_scrollView.frame.origin.x - (-_gapBetweenPages/2)) < 1e-6)
_scrollView.contentOffset = CGPointMake(_scrollView.frame.size.width * newPageIndex, 0);
else
_currentPageIndex = newPageIndex;
}


#pragma mark -
#pragma mark Layouting

Expand Down
1 change: 1 addition & 0 deletions ATPagingView/ATPagingViewDemo/Classes/DemoViewController.m
Expand Up @@ -14,6 +14,7 @@ - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interface

- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
self.pagingView.currentPageIndex = 3;
[self currentPageDidChangeInPagingView:self.pagingView];
}

Expand Down

0 comments on commit b69e6fe

Please sign in to comment.