Skip to content

Commit

Permalink
much simpler inset implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
George Harker committed Apr 3, 2015
1 parent 4eeb799 commit c083574
Showing 1 changed file with 17 additions and 34 deletions.
51 changes: 17 additions & 34 deletions src/pagingscrollview/src/NIPagingScrollView.m
Original file line number Diff line number Diff line change
@@ -66,6 +66,7 @@ - (void)commonInit {
_scrollView = [[UIScrollView alloc] initWithFrame:self.bounds];
_scrollView.pagingEnabled = YES;
_scrollView.scrollsToTop = NO;
_scrollView.clipsToBounds = NO;

_scrollView.autoresizingMask = UIViewAutoresizingFlexibleDimensions;

@@ -102,10 +103,10 @@ - (CGRect)frameForPagingScrollView {
if (NIPagingScrollViewHorizontal == self.type) {
// We make the paging scroll view a little bit wider on the side edges so that there
// there is space between the pages when flipping through them.
frame = CGRectInset(frame, -self.pageMargin, 0);
frame = CGRectInset(frame, self.pageInset - self.pageMargin, 0);

} else if (NIPagingScrollViewVertical == self.type) {
frame = CGRectInset(frame, 0, -self.pageMargin);
frame = CGRectInset(frame, 0, self.pageInset - self.pageMargin);
}

return frame;
@@ -121,14 +122,14 @@ - (CGRect)frameForPageAtIndex:(NSInteger)pageIndex {
CGRect pageFrame = bounds;

if (NIPagingScrollViewHorizontal == self.type) {
pageFrame.origin.x = ((bounds.size.width - self.pageInset * 2) * pageIndex);
pageFrame.origin.x = (bounds.size.width * pageIndex);
// We need to counter the extra spacing added to the paging scroll view in
// frameForPagingScrollView.
pageFrame = CGRectInset(pageFrame, self.pageMargin + self.pageInset, 0);
pageFrame = CGRectInset(pageFrame, self.pageMargin, 0);

} else if (NIPagingScrollViewVertical == self.type) {
pageFrame.origin.y = ((bounds.size.height - self.pageInset * 2) * pageIndex);
pageFrame = CGRectInset(pageFrame, 0, self.pageMargin + self.pageInset);
pageFrame.origin.y = (bounds.size.height * pageIndex);
pageFrame = CGRectInset(pageFrame, 0, self.pageMargin);
}

return pageFrame;
@@ -139,32 +140,32 @@ - (CGSize)contentSizeForPagingScrollView {
// outlined above.
CGRect bounds = _scrollView.bounds;
if (NIPagingScrollViewHorizontal == self.type) {
return CGSizeMake((bounds.size.width - self.pageInset * 2) * self.numberOfPages, bounds.size.height);
return CGSizeMake(bounds.size.width * self.numberOfPages, bounds.size.height);

} else if (NIPagingScrollViewVertical == self.type) {
return CGSizeMake(bounds.size.width, (bounds.size.height - self.pageInset * 2) * self.numberOfPages);
return CGSizeMake(bounds.size.width, bounds.size.height * self.numberOfPages);
}

return CGSizeZero;
}

- (CGPoint)contentOffsetFromPageOffset:(CGPoint)offset {
if (NIPagingScrollViewHorizontal == self.type) {
offset.x -= self.pageMargin + self.pageInset;
offset.x -= self.pageMargin;

} else if (NIPagingScrollViewVertical == self.type) {
offset.y -= self.pageMargin + self.pageInset;
offset.y -= self.pageMargin;
}

return offset;
}

- (CGFloat)pageScrollableDimension {
if (NIPagingScrollViewHorizontal == self.type) {
return _scrollView.bounds.size.width - self.pageInset * 2;
return _scrollView.bounds.size.width;

} else if (NIPagingScrollViewVertical == self.type) {
return _scrollView.bounds.size.height - self.pageInset * 2;
return _scrollView.bounds.size.height;
}

return 0;
@@ -217,13 +218,13 @@ - (NSInteger)currentVisiblePageIndex {
// Whatever image is currently displayed in the center of the screen is the currently
// visible image.
return NIBoundi((NSInteger)(NICGFloatFloor((contentOffset.x + boundsSize.width / 2) /
(boundsSize.width - self.pageInset * 2))
boundsSize.width)
+ 0.5f),
0, self.numberOfPages - 1);

} else if (NIPagingScrollViewVertical == self.type) {
return NIBoundi((NSInteger)(NICGFloatFloor((contentOffset.y + boundsSize.height / 2) /
(boundsSize.height - self.pageInset * 2))
boundsSize.height)
+ 0.5f),
0, self.numberOfPages - 1);
}
@@ -238,13 +239,13 @@ - (NSInteger)pageIndexForOffset:(CGFloat)offset {
// 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 - self.pageInset * 2))
boundsSize.width)
+ 0.5f),
0, self.numberOfPages - 1);

} else if (NIPagingScrollViewVertical == self.type) {
return NIBoundi((NSInteger)(NICGFloatFloor((offset + boundsSize.height / 2) /
(boundsSize.height - self.pageInset * 2))
boundsSize.height)
+ 0.5f),
0, self.numberOfPages - 1);
}
@@ -475,21 +476,6 @@ - (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL
}
}

- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset
{
if (_pageInset == 0) {
// We handle the standard case for scrolling via pagingEnabled.
return;
}
CGFloat targetX = _scrollView.contentOffset.x + velocity.x * 60.0f;
NSInteger targetIndex = [self pageIndexForOffset:targetX];

CGPoint offset = [self frameForPageAtIndex:targetIndex].origin;
offset = [self contentOffsetFromPageOffset:offset];

*targetContentOffset = offset;
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
[self updateVisiblePagesShouldNotifyDelegate:YES];
[self resetSurroundingPages];
@@ -746,9 +732,6 @@ - (void)setPageMargin:(CGFloat)pageMargin {

- (void)setPageInset:(CGFloat)pageInset {
_pageInset = pageInset;
// Do not auto-page inset views. The paging is based on view bounds,
// which would not be correct. We compute the snapping manually.
_scrollView.pagingEnabled = (_pageInset == 0);

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

0 comments on commit c083574

Please sign in to comment.