Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Springboard-like page scrolling #23

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="2.0" toolsVersion="3084" systemVersion="12C3006" targetRuntime="iOS.CocoaTouch.iPad" propertyAccessControl="none" useAutolayout="YES" initialViewController="wfC-9f-vtC">
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="2.0" toolsVersion="3084" systemVersion="12D78" targetRuntime="iOS.CocoaTouch.iPad" propertyAccessControl="none" useAutolayout="YES" initialViewController="wfC-9f-vtC">
<dependencies>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="2083"/>
</dependencies>
Expand All @@ -8,7 +8,7 @@
<scene sceneID="3Tg-sk-Lj5">
<objects>
<collectionViewController autoresizesArchivedViewToFullSize="NO" id="wfC-9f-vtC" customClass="LXCollectionViewController" sceneMemberID="viewController">
<collectionView key="view" opaque="NO" clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="scaleToFill" minimumZoomScale="0.0" maximumZoomScale="0.0" dataMode="prototypes" id="jt6-40-vJL">
<collectionView key="view" opaque="NO" clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="scaleToFill" pagingEnabled="YES" minimumZoomScale="0.0" maximumZoomScale="0.0" dataMode="prototypes" id="jt6-40-vJL">
<rect key="frame" x="0.0" y="20" width="768" height="1004"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<collectionViewFlowLayout key="collectionViewLayout" minimumLineSpacing="10" minimumInteritemSpacing="10" id="zhp-KR-emn" customClass="LXReorderableCollectionViewFlowLayout">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,51 @@ typedef NS_ENUM(NSInteger, LXScrollingDirection) {
static NSString * const kLXScrollingDirectionKey = @"LXScrollingDirection";
static NSString * const kLXCollectionViewKeyPath = @"collectionView";

@interface UICollectionViewFlowLayout (LXPaging)

- (CGPoint)LX_contentOffsetForPageIndex:(NSInteger)pageIndex;
- (NSInteger)LX_currentPageIndex;
- (NSInteger)LX_pageCount;

@end

@implementation UICollectionViewFlowLayout (LXPaging)

- (NSInteger)LX_currentPageIndex {
switch (self.scrollDirection) {
case UICollectionViewScrollDirectionHorizontal: {
return round(self.collectionView.contentOffset.x / self.collectionView.frame.size.width);
}
case UICollectionViewScrollDirectionVertical: {
return round(self.collectionView.contentOffset.y / self.collectionView.frame.size.height);
}
}
}

- (NSInteger)LX_pageCount {
switch (self.scrollDirection) {
case UICollectionViewScrollDirectionHorizontal: {
return ceil(self.collectionViewContentSize.width / self.collectionView.frame.size.width);
}
case UICollectionViewScrollDirectionVertical: {
return ceil(self.collectionViewContentSize.height / self.collectionView.frame.size.height);
}
}
}

- (CGPoint)LX_contentOffsetForPageIndex:(NSInteger)pageIndex {
switch (self.scrollDirection) {
case UICollectionViewScrollDirectionHorizontal: {
return CGPointMake(self.collectionView.frame.size.width * pageIndex, 0);
}
case UICollectionViewScrollDirectionVertical: {
return CGPointMake(0, self.collectionView.frame.size.height * pageIndex);
}
}
}

@end

@interface UICollectionViewCell (LXReorderableCollectionViewFlowLayout)

- (UIImage *)LX_rasterizedImage;
Expand Down Expand Up @@ -59,7 +104,9 @@ @interface LXReorderableCollectionViewFlowLayout ()

@end

@implementation LXReorderableCollectionViewFlowLayout
@implementation LXReorderableCollectionViewFlowLayout {
BOOL _pageScrollingDisabled;
}

- (void)setDefaults {
_scrollingSpeed = 300.0f;
Expand Down Expand Up @@ -126,7 +173,8 @@ - (void)applyLayoutAttributes:(UICollectionViewLayoutAttributes *)layoutAttribut
}

- (void)invalidateLayoutIfNecessary {
NSIndexPath *newIndexPath = [self.collectionView indexPathForItemAtPoint:self.currentView.center];
const CGPoint point = [self.collectionView convertPoint:self.currentView.center fromView:self.collectionView.superview];
NSIndexPath *newIndexPath = [self.collectionView indexPathForItemAtPoint:point];
NSIndexPath *previousIndexPath = self.selectedItemIndexPath;

if ((newIndexPath == nil) || [newIndexPath isEqual:previousIndexPath]) {
Expand Down Expand Up @@ -159,6 +207,85 @@ - (void)invalidatesScrollTimer {
self.scrollingTimer = nil;
}

- (void)scrollIfNecessary {
if (!self.currentView) return; // Prevent scrollToPageIndex to continue scrolling after the drag ended

const CGPoint viewCenter = [self.collectionView convertPoint:self.currentView.center fromView:self.collectionView.superview];
switch (self.scrollDirection) {
case UICollectionViewScrollDirectionVertical: {
if (viewCenter.y < (CGRectGetMinY(self.collectionView.bounds) + self.scrollingTriggerEdgeInsets.top)) {
[self scrollWithDirection:LXScrollingDirectionUp];
} else {
if (viewCenter.y > (CGRectGetMaxY(self.collectionView.bounds) - self.scrollingTriggerEdgeInsets.bottom)) {
[self scrollWithDirection:LXScrollingDirectionDown];
} else {
[self invalidatesScrollTimer];
}
}
} break;
case UICollectionViewScrollDirectionHorizontal: {
if (viewCenter.x < (CGRectGetMinX(self.collectionView.bounds) + self.scrollingTriggerEdgeInsets.left)) {
[self scrollWithDirection:LXScrollingDirectionLeft];
} else {
if (viewCenter.x > (CGRectGetMaxX(self.collectionView.bounds) - self.scrollingTriggerEdgeInsets.right)) {
[self scrollWithDirection:LXScrollingDirectionRight];
} else {
[self invalidatesScrollTimer];
}
}
} break;
}
}

- (void)scrollToPreviousPage {
if (_pageScrollingDisabled) return;
const NSInteger currentPage = [self LX_currentPageIndex];
if (currentPage <= 0) return;
const NSInteger newPage = currentPage - 1;
[self scrollToPageIndex:newPage forward:NO];
}

- (void)scrollToNextPage {
if (_pageScrollingDisabled) return;
const NSInteger currentPage = [self LX_currentPageIndex];
const NSInteger pageCount = [self LX_pageCount];
if (currentPage >= pageCount - 1) return;
const NSInteger newPage = currentPage + 1;
[self scrollToPageIndex:newPage forward:YES];
}

- (void)scrollToPageIndex:(NSInteger)pageIndex forward:(BOOL)forward {
const CGPoint offset = [self LX_contentOffsetForPageIndex:pageIndex];
[self.collectionView setContentOffset:offset animated:YES];

// Wait a little bit before changing page again
_pageScrollingDisabled = YES;
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 1 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{ // Delay must be bigger than the contentOffset animation
_pageScrollingDisabled = NO;
[self scrollIfNecessary];
});
}

- (void)scrollWithDirection:(LXScrollingDirection)direction {
if (self.collectionView.pagingEnabled) {
switch(direction) {
case LXScrollingDirectionUp:
case LXScrollingDirectionLeft: {
[self scrollToPreviousPage];
} break;
case LXScrollingDirectionDown:
case LXScrollingDirectionRight: {
[self scrollToNextPage];
} break;
default: {
// Do nothing...
} break;
}
} else {
[self setupScrollTimerInDirection:direction];
}
}

- (void)setupScrollTimerInDirection:(LXScrollingDirection)direction {
if (self.scrollingTimer.isValid) {
LXScrollingDirection oldDirection = [self.scrollingTimer.userInfo[kLXScrollingDirectionKey] integerValue];
Expand Down Expand Up @@ -236,7 +363,6 @@ - (void)handleScroll:(NSTimer *)timer {
} break;
}

self.currentViewCenter = LXS_CGPointAdd(self.currentViewCenter, translation);
self.currentView.center = LXS_CGPointAdd(self.currentViewCenter, self.panTranslationInCollectionView);
self.collectionView.contentOffset = LXS_CGPointAdd(contentOffset, translation);
}
Expand Down Expand Up @@ -274,7 +400,9 @@ - (void)handleLongPressGesture:(UILongPressGestureRecognizer *)gestureRecognizer

[self.currentView addSubview:imageView];
[self.currentView addSubview:highlightedImageView];
[self.collectionView addSubview:self.currentView];
const CGPoint center = [self.collectionView.superview convertPoint:collectionViewCell.center fromView:self.collectionView];
self.currentView.center = center;
[self.collectionView.superview addSubview:self.currentView];

self.currentViewCenter = self.currentView.center;

Expand Down Expand Up @@ -326,7 +454,8 @@ - (void)handleLongPressGesture:(UILongPressGestureRecognizer *)gestureRecognizer
__strong typeof(self) strongSelf = weakSelf;
if (strongSelf) {
strongSelf.currentView.transform = CGAffineTransformMakeScale(1.0f, 1.0f);
strongSelf.currentView.center = layoutAttributes.center;
const CGPoint center = [strongSelf.collectionView.superview convertPoint:layoutAttributes.center fromView:strongSelf.collectionView];
strongSelf.currentView.center = center;
}
}
completion:^(BOOL finished) {
Expand All @@ -352,41 +481,14 @@ - (void)handlePanGesture:(UIPanGestureRecognizer *)gestureRecognizer {
switch (gestureRecognizer.state) {
case UIGestureRecognizerStateBegan:
case UIGestureRecognizerStateChanged: {
self.panTranslationInCollectionView = [gestureRecognizer translationInView:self.collectionView];
CGPoint viewCenter = self.currentView.center = LXS_CGPointAdd(self.currentViewCenter, self.panTranslationInCollectionView);

self.panTranslationInCollectionView = [gestureRecognizer translationInView:self.collectionView.superview];
self.currentView.center = LXS_CGPointAdd(self.currentViewCenter, self.panTranslationInCollectionView);
[self invalidateLayoutIfNecessary];

switch (self.scrollDirection) {
case UICollectionViewScrollDirectionVertical: {
if (viewCenter.y < (CGRectGetMinY(self.collectionView.bounds) + self.scrollingTriggerEdgeInsets.top)) {
[self setupScrollTimerInDirection:LXScrollingDirectionUp];
} else {
if (viewCenter.y > (CGRectGetMaxY(self.collectionView.bounds) - self.scrollingTriggerEdgeInsets.bottom)) {
[self setupScrollTimerInDirection:LXScrollingDirectionDown];
} else {
[self invalidatesScrollTimer];
}
}
} break;
case UICollectionViewScrollDirectionHorizontal: {
if (viewCenter.x < (CGRectGetMinX(self.collectionView.bounds) + self.scrollingTriggerEdgeInsets.left)) {
[self setupScrollTimerInDirection:LXScrollingDirectionLeft];
} else {
if (viewCenter.x > (CGRectGetMaxX(self.collectionView.bounds) - self.scrollingTriggerEdgeInsets.right)) {
[self setupScrollTimerInDirection:LXScrollingDirectionRight];
} else {
[self invalidatesScrollTimer];
}
}
} break;
}
} break;
case UIGestureRecognizerStateEnded: {
[self invalidatesScrollTimer];
[self scrollIfNecessary];
} break;
case UIGestureRecognizerStateEnded:
default: {
// Do nothing...
[self invalidatesScrollTimer];
} break;
}
}
Expand Down