Skip to content

Commit

Permalink
fixes, make formal layoutSubviews method
Browse files Browse the repository at this point in the history
  • Loading branch information
George Harker committed Apr 3, 2015
1 parent 11ada75 commit 0270e37
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 33 deletions.
7 changes: 4 additions & 3 deletions src/pagingscrollview/src/NIPagingScrollView.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,11 @@ typedef enum {

#pragma mark Configuring Presentation

// Controls the border between images.
// Controls the border between pages.
@property (nonatomic) CGFloat pageMargin;
// Used to make the view smaller than full screen, thus showing neighboring images, horizontally
// in the view.
// Used to make the view smaller than the frame of the paging scroll view, thus showing
// neighboring pages, either horizontally or vertically depending on the configuration
// of the view.
@property (nonatomic) CGFloat pageInset;
@property (nonatomic) NIPagingScrollViewType type; // Default: NIPagingScrollViewHorizontal

Expand Down
50 changes: 20 additions & 30 deletions src/pagingscrollview/src/NIPagingScrollView.m
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,18 @@ - (id)initWithCoder:(NSCoder *)aDecoder {

#pragma mark - Page Layout

- (void)layoutSubviews {
[super layoutSubviews];
_scrollView.frame = [self frameForPagingScrollView];

// Retain the current position.
CGPoint offset = [self frameForPageAtIndex:_centerPageIndex].origin;
_scrollView.contentOffset = [self contentOffsetFromPageOffset:offset];

_scrollView.contentSize = [self contentSizeForPagingScrollView];
[self layoutVisiblePages];
}

// The following three methods are from Apple's ImageScrollView example application and have
// been used here because they are well-documented and concise.

Expand Down Expand Up @@ -230,31 +242,18 @@ - (NSInteger)currentVisiblePageIndex {
return 0;
}

- (NSInteger)pageIndexForOffset:(CGFloat)offset {
CGSize boundsSize = _scrollView.bounds.size;

if (NIPagingScrollViewHorizontal == self.type) {
// Whatever image is currently displayed in the center of the screen is the currently
// visible image.
return NIBoundi((NSInteger)(NICGFloatFloor((offset + boundsSize.width / 2) / boundsSize.width)
+ 0.5f),
0, self.numberOfPages - 1);

} else if (NIPagingScrollViewVertical == self.type) {
return NIBoundi((NSInteger)(NICGFloatFloor((offset + boundsSize.height / 2) / boundsSize.height)
+ 0.5f),
0, self.numberOfPages - 1);
}

return 0;
}

- (NSRange)rangeOfVisiblePages {
if (0 >= self.numberOfPages) {
return NSMakeRange(0, 0);
}

NSInteger visibleRange = (_pageInset == 0) ? 1 : 2;
NSInteger visibleRange = 1;
if (_pageInset != 0) {
CGSize boundsSize = _scrollView.bounds.size;
CGSize frameSize = self.frame.size;
visibleRange = ceil(frameSize.width / (boundsSize.width + _pageMargin));
}

NSInteger currentVisiblePageIndex = [self currentVisiblePageIndex];

NSInteger firstVisiblePageIndex = NIBoundi(currentVisiblePageIndex - visibleRange, 0, self.numberOfPages - 1);
Expand Down Expand Up @@ -374,7 +373,7 @@ - (void)updateVisiblePagesShouldNotifyDelegate:(BOOL)shouldNotifyDelegate {
[self didChangeCenterPageIndexFrom:oldCenterPageIndex to:_centerPageIndex];

if (_pageInset != 0) {
// When multiple images are visible load them all.
// Load all visible insetted pages immediately.
[self preloadOffscreenPages];
} else {
// Prioritize displaying the currently visible page.
Expand Down Expand Up @@ -724,15 +723,6 @@ - (void)setPageMargin:(CGFloat)pageMargin {

- (void)setPageInset:(CGFloat)pageInset {
_pageInset = pageInset;
_scrollView.frame = [self frameForPagingScrollView];

// Retain the current position.
CGPoint offset = [self frameForPageAtIndex:_centerPageIndex].origin;
_scrollView.contentOffset = [self contentOffsetFromPageOffset:offset];

_scrollView.contentSize = [self contentSizeForPagingScrollView];
[self layoutVisiblePages];

[self setNeedsLayout];
}

Expand Down

0 comments on commit 0270e37

Please sign in to comment.