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

Fix calculation for transition distance #33

Merged
merged 2 commits into from
Feb 19, 2017
Merged
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
4 changes: 0 additions & 4 deletions CalendarExample/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,6 @@ struct CalendarPagingTheme: PagingTheme {
struct CalendarPagingOptions: PagingOptions {
let menuItemClass: PagingCell.Type = CalendarPagingCell.self
let menuItemSize: PagingMenuItemSize = .fixed(width: 48, height: 58)
let indicatorOptions: PagingIndicatorOptions = .visible(height: 2,
zIndex: Int.max,
spacing: UIEdgeInsets(top: 0, left: 8, bottom: 0, right: 8),
insets: UIEdgeInsets(top: 0, left: 8, bottom: 0, right: 8))
let theme: PagingTheme = CalendarPagingTheme()
}

Expand Down
30 changes: 23 additions & 7 deletions Parchment.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,8 @@
95B3010F1E59E70D00B95D02 /* Parchment.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 3EA04A4B1C53BFE40054E5E0 /* Parchment.framework */; };
95B301101E59E70D00B95D02 /* Parchment.framework in Embed Frameworks */ = {isa = PBXBuildFile; fileRef = 3EA04A4B1C53BFE40054E5E0 /* Parchment.framework */; settings = {ATTRIBUTES = (CodeSignOnCopy, RemoveHeadersOnCopy, ); }; };
95B301151E59E9BB00B95D02 /* UIView+constraints.swift in Sources */ = {isa = PBXBuildFile; fileRef = 95B301141E59E9BB00B95D02 /* UIView+constraints.swift */; };
95B301171E59FCD500B95D02 /* UIEdgeInsets.swift in Sources */ = {isa = PBXBuildFile; fileRef = 95B301161E59FCD500B95D02 /* UIEdgeInsets.swift */; };
95B301191E59FD1600B95D02 /* UIScrollView+near.swift in Sources */ = {isa = PBXBuildFile; fileRef = 95B301181E59FD1600B95D02 /* UIScrollView+near.swift */; };
95F2CCA61E395762003C973E /* Parchment.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 3EA04A4B1C53BFE40054E5E0 /* Parchment.framework */; };
95F2CCA71E395762003C973E /* Parchment.framework in Embed Frameworks */ = {isa = PBXBuildFile; fileRef = 3EA04A4B1C53BFE40054E5E0 /* Parchment.framework */; settings = {ATTRIBUTES = (CodeSignOnCopy, RemoveHeadersOnCopy, ); }; };
/* End PBXBuildFile section */
Expand Down Expand Up @@ -341,6 +343,8 @@
95B301091E59E43800B95D02 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/LaunchScreen.storyboard; sourceTree = "<group>"; };
95B3010B1E59E43800B95D02 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
95B301141E59E9BB00B95D02 /* UIView+constraints.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "UIView+constraints.swift"; sourceTree = "<group>"; };
95B301161E59FCD500B95D02 /* UIEdgeInsets.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = UIEdgeInsets.swift; sourceTree = "<group>"; };
95B301181E59FD1600B95D02 /* UIScrollView+near.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "UIScrollView+near.swift"; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand Down Expand Up @@ -583,6 +587,8 @@
children = (
3E2AAD271CA831AB0044AAA5 /* UIView+constraints.swift */,
9597F2941E3903F4003FD289 /* UIColor+interpolation.swift */,
95B301161E59FCD500B95D02 /* UIEdgeInsets.swift */,
95B301181E59FD1600B95D02 /* UIScrollView+near.swift */,
);
path = Extensions;
sourceTree = "<group>";
Expand Down Expand Up @@ -820,12 +826,12 @@
TargetAttributes = {
3E2AAD041CA81D480044AAA5 = {
CreatedOnToolsVersion = 7.3;
DevelopmentTeam = C48GZ8PUTA;
DevelopmentTeam = G4TGN24VA2;
LastSwiftMigration = 0810;
};
3E2AAD321CA86C690044AAA5 = {
CreatedOnToolsVersion = 7.3;
DevelopmentTeam = C48GZ8PUTA;
DevelopmentTeam = G4TGN24VA2;
LastSwiftMigration = 0810;
};
3E504EC31C7465B000AE1CE3 = {
Expand All @@ -835,7 +841,7 @@
};
3E5E93EF1CE8BA1E000762A1 = {
CreatedOnToolsVersion = 7.3;
DevelopmentTeam = C48GZ8PUTA;
DevelopmentTeam = G4TGN24VA2;
LastSwiftMigration = 0810;
};
3EA04A4A1C53BFE40054E5E0 = {
Expand All @@ -845,12 +851,12 @@
};
3EA04A591C53BFF40054E5E0 = {
CreatedOnToolsVersion = 7.2;
DevelopmentTeam = C48GZ8PUTA;
DevelopmentTeam = G4TGN24VA2;
LastSwiftMigration = 0810;
};
9597F29B1E394482003FD289 = {
CreatedOnToolsVersion = 8.2.1;
DevelopmentTeam = C48GZ8PUTA;
DevelopmentTeam = G4TGN24VA2;
ProvisioningStyle = Automatic;
};
95B300FC1E59E43800B95D02 = {
Expand Down Expand Up @@ -1049,6 +1055,7 @@
3E4090831C88BBCF00800E22 /* Reusable.swift in Sources */,
3E2AAD281CA831AB0044AAA5 /* UIView+constraints.swift in Sources */,
3E49C7251C8F5C13006269DD /* PagingBorderView.swift in Sources */,
95B301191E59FD1600B95D02 /* UIScrollView+near.swift in Sources */,
3E40909A1C88BCC900800E22 /* PagingDirection.swift in Sources */,
3ED1C8D81CC1554E00B46885 /* PagingItemPresentable.swift in Sources */,
3E49C7291C8F5C13006269DD /* PagingView.swift in Sources */,
Expand All @@ -1062,6 +1069,7 @@
3E4090AE1C88BDD100800E22 /* PagingBorderLayoutAttributes.swift in Sources */,
3E4283FC1C99CF9000032D95 /* PagingDataStructure.swift in Sources */,
3E49C7281C8F5C13006269DD /* PagingIndicatorView.swift in Sources */,
95B301171E59FCD500B95D02 /* UIEdgeInsets.swift in Sources */,
3E49C7261C8F5C13006269DD /* PagingCell.swift in Sources */,
3E49C72E1C8F5CCE006269DD /* PagingCellViewModel.swift in Sources */,
3E562ACE1CE7CD8C007623B3 /* PagingTitleCell.swift in Sources */,
Expand Down Expand Up @@ -1253,6 +1261,7 @@
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
CLANG_ANALYZER_NONNULL = YES;
CODE_SIGN_IDENTITY = "iPhone Developer";
DEVELOPMENT_TEAM = G4TGN24VA2;
INFOPLIST_FILE = CalendarExample/Info.plist;
IPHONEOS_DEPLOYMENT_TARGET = 8.2;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
Expand All @@ -1268,6 +1277,7 @@
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
CLANG_ANALYZER_NONNULL = YES;
CODE_SIGN_IDENTITY = "iPhone Developer";
DEVELOPMENT_TEAM = G4TGN24VA2;
INFOPLIST_FILE = CalendarExample/Info.plist;
IPHONEOS_DEPLOYMENT_TARGET = 8.2;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
Expand All @@ -1283,6 +1293,7 @@
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
CLANG_ANALYZER_NONNULL = YES;
CODE_SIGN_IDENTITY = "iPhone Developer";
DEVELOPMENT_TEAM = G4TGN24VA2;
INFOPLIST_FILE = DelegateExample/Info.plist;
IPHONEOS_DEPLOYMENT_TARGET = 8.2;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
Expand All @@ -1298,6 +1309,7 @@
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
CLANG_ANALYZER_NONNULL = YES;
CODE_SIGN_IDENTITY = "iPhone Developer";
DEVELOPMENT_TEAM = G4TGN24VA2;
INFOPLIST_FILE = DelegateExample/Info.plist;
IPHONEOS_DEPLOYMENT_TARGET = 8.2;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
Expand Down Expand Up @@ -1345,6 +1357,7 @@
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
CLANG_ANALYZER_NONNULL = YES;
CODE_SIGN_IDENTITY = "iPhone Developer";
DEVELOPMENT_TEAM = G4TGN24VA2;
INFOPLIST_FILE = UnplashExample/Info.plist;
IPHONEOS_DEPLOYMENT_TARGET = 8.2;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
Expand All @@ -1360,6 +1373,7 @@
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
CLANG_ANALYZER_NONNULL = YES;
CODE_SIGN_IDENTITY = "iPhone Developer";
DEVELOPMENT_TEAM = G4TGN24VA2;
INFOPLIST_FILE = UnplashExample/Info.plist;
IPHONEOS_DEPLOYMENT_TARGET = 8.2;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
Expand Down Expand Up @@ -1524,6 +1538,7 @@
ALWAYS_EMBED_SWIFT_STANDARD_LIBRARIES = YES;
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
CODE_SIGN_IDENTITY = "iPhone Developer";
DEVELOPMENT_TEAM = G4TGN24VA2;
FRAMEWORK_SEARCH_PATHS = (
"$(inherited)",
"$(PROJECT_DIR)/Carthage/Build/iOS",
Expand All @@ -1543,6 +1558,7 @@
ALWAYS_EMBED_SWIFT_STANDARD_LIBRARIES = YES;
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
CODE_SIGN_IDENTITY = "iPhone Developer";
DEVELOPMENT_TEAM = G4TGN24VA2;
FRAMEWORK_SEARCH_PATHS = (
"$(inherited)",
"$(PROJECT_DIR)/Carthage/Build/iOS",
Expand All @@ -1565,7 +1581,7 @@
CLANG_WARN_DOCUMENTATION_COMMENTS = YES;
CODE_SIGN_IDENTITY = "iPhone Developer";
"CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer";
DEVELOPMENT_TEAM = C48GZ8PUTA;
DEVELOPMENT_TEAM = G4TGN24VA2;
INFOPLIST_FILE = IconsExample/Info.plist;
IPHONEOS_DEPLOYMENT_TARGET = 8.2;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
Expand All @@ -1585,7 +1601,7 @@
CLANG_WARN_DOCUMENTATION_COMMENTS = YES;
CODE_SIGN_IDENTITY = "iPhone Developer";
"CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer";
DEVELOPMENT_TEAM = C48GZ8PUTA;
DEVELOPMENT_TEAM = G4TGN24VA2;
INFOPLIST_FILE = IconsExample/Info.plist;
IPHONEOS_DEPLOYMENT_TARGET = 8.2;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
Expand Down
59 changes: 36 additions & 23 deletions Parchment/Classes/PagingViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -172,21 +172,25 @@ open class PagingViewController<T: PagingItem>:
collectionViewLayout.state = state
switch state {
case let .selected(pagingItem):

reloadItems(around: pagingItem)
collectionView.selectItem(
at: dataStructure.indexPathForPagingItem(pagingItem),
animated: options.menuTransition == .animateAfter,
scrollPosition: options.scrollPosition)

case .scrolling:

if options.menuTransition == .scrollAlongside {
let inset = options.menuInsets.left + options.menuInsets.right
let offset = state.contentOffset.x + (state.distance * fabs(state.progress))

if dataStructure.totalWidth + inset >= collectionView.bounds.width {
collectionView.setContentOffset(CGPoint(
x: min(max(0, offset), collectionView.contentSize.width - collectionView.bounds.width),
y: state.contentOffset.y), animated: false)
if state.distance != 0 {
if dataStructure.totalWidth + options.menuInsets.horizontal >= collectionView.bounds.width {

let contentOffset = CGPoint(
x: state.contentOffset.x + (state.distance * fabs(state.progress)),
y: state.contentOffset.y)

collectionView.setContentOffset(contentOffset, animated: false)
}
}
}

Expand Down Expand Up @@ -233,6 +237,7 @@ open class PagingViewController<T: PagingItem>:
collectionView.contentOffset = CGPoint(
x: oldContentOffset.x + offset,
y: oldContentOffset.y)
collectionView.layoutIfNeeded()
}

fileprivate func selectViewController(_ pagingItem: T, direction: PagingDirection, animated: Bool = true) {
Expand All @@ -243,6 +248,21 @@ open class PagingViewController<T: PagingItem>:
animated: animated,
completion: nil)
}

fileprivate func distance(from: UICollectionViewCell, to: UICollectionViewCell) -> CGFloat {
switch (options.selectedScrollPosition) {
case .left:
return to.frame.origin.x - collectionView.contentOffset.x
case .right:
let currentPosition = to.frame.origin.x + to.frame.width
let width = collectionView.contentOffset.x + collectionView.bounds.width
return currentPosition - width
case .preferCentered:
let distanceToCenter = collectionView.bounds.midX - from.frame.midX
let distanceBetweenCells = to.frame.midX - from.frame.midX
return distanceBetweenCells - distanceToCenter
}
}

// MARK: UICollectionViewDelegateFlowLayout

Expand Down Expand Up @@ -386,24 +406,17 @@ open class PagingViewController<T: PagingItem>:

let currentCell = collectionView(collectionView, cellForItemAt: currentIndexPath)
let upcomingCell = collectionView(collectionView, cellForItemAt: upcomingIndexPath)

switch (options.selectedScrollPosition) {
case .left:
return PagingTransition(
contentOffset: collectionView.contentOffset,
distance: upcomingCell.frame.origin.x - collectionView.contentOffset.x)
case .right:
return PagingTransition(
contentOffset: collectionView.contentOffset,
distance: (upcomingCell.frame.origin.x + upcomingCell.frame.width) - (collectionView.contentOffset.x + collectionView.bounds.width))
case .preferCentered:
let distanceToCenter = collectionView.bounds.midX - currentCell.frame.midX
let distanceBetweenCells = upcomingCell.frame.midX - currentCell.frame.midX
return PagingTransition(
contentOffset: collectionView.contentOffset,
distance: distanceBetweenCells - distanceToCenter)
var distance = self.distance(from: currentCell, to: upcomingCell)

if collectionView.near(edge: .left, clearance: -distance) {
distance = -(collectionView.contentOffset.x + collectionView.contentInset.left)
} else if collectionView.near(edge: .right, clearance: distance) {
distance = collectionView.contentSize.width - (collectionView.contentOffset.x + collectionView.bounds.width)
}

return PagingTransition(
contentOffset: collectionView.contentOffset,
distance: distance)
}

func pagingStateMachine<U>(
Expand Down
13 changes: 13 additions & 0 deletions Parchment/Extensions/UIEdgeInsets.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import UIKit

extension UIEdgeInsets {

var horizontal: CGFloat {
return left + right
}

var vertical: CGFloat {
return top + bottom
}

}
22 changes: 22 additions & 0 deletions Parchment/Extensions/UIScrollView+near.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import UIKit

enum Edge {
case left, right, top, bottom
}

extension UIScrollView {

func near(edge: Edge, clearance: CGFloat = 0) -> Bool {
switch edge {
case .left:
return contentOffset.x + contentInset.left - clearance <= 0
case .right:
return (contentOffset.x + bounds.width + clearance) >= contentSize.width
case .top:
return contentOffset.y + contentInset.top - clearance <= 0
case .bottom:
return (contentOffset.y + bounds.height + clearance) >= contentSize.height
}
}

}
14 changes: 7 additions & 7 deletions StoryboardExample/Base.lproj/Main.storyboard
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,8 @@
<!--Navigation Controller-->
<scene sceneID="iFJ-Da-Xjx">
<objects>
<placeholder placeholderIdentifier="IBFirstResponder" id="UoW-K9-Ezy" userLabel="First Responder" sceneMemberID="firstResponder"/>
<navigationController id="5ZW-Jx-LCp" sceneMemberID="viewController">
<navigationBar key="navigationBar" contentMode="scaleToFill" misplaced="YES" barStyle="black" id="3Im-Xt-q8V">
<navigationBar key="navigationBar" contentMode="scaleToFill" barStyle="black" id="3Im-Xt-q8V">
<rect key="frame" x="0.0" y="0.0" width="375" height="44"/>
<autoresizingMask key="autoresizingMask"/>
<color key="barTintColor" red="0.01176470588" green="0.49019607840000001" blue="0.91372549020000005" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
Expand All @@ -23,6 +22,7 @@
<segue destination="BYZ-38-t0r" kind="relationship" relationship="rootViewController" id="SjN-qI-N0d"/>
</connections>
</navigationController>
<placeholder placeholderIdentifier="IBFirstResponder" id="UoW-K9-Ezy" userLabel="First Responder" sceneMemberID="firstResponder"/>
</objects>
<point key="canvasLocation" x="-297" y="74"/>
</scene>
Expand All @@ -34,7 +34,7 @@
<viewControllerLayoutGuide type="top" id="y3c-jy-aDJ"/>
<viewControllerLayoutGuide type="bottom" id="wfy-db-euE"/>
</layoutGuides>
<view key="view" contentMode="scaleToFill" misplaced="YES" id="8bC-Xf-vdC">
<view key="view" contentMode="scaleToFill" id="8bC-Xf-vdC">
<rect key="frame" x="0.0" y="64" width="375" height="603"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<color key="backgroundColor" red="1" green="1" blue="1" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
Expand All @@ -58,8 +58,8 @@
<rect key="frame" x="0.0" y="0.0" width="375" height="667"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<subviews>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="Label" textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="0Pg-WS-nr3">
<rect key="frame" x="154.5" y="317" width="66.5" height="33.5"/>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="First" textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="0Pg-WS-nr3">
<rect key="frame" x="162" y="317" width="51.5" height="33.5"/>
<fontDescription key="fontDescription" style="UICTFontTextStyleTitle1"/>
<color key="textColor" red="0.49803921579999999" green="0.49803921579999999" blue="0.49803921579999999" alpha="1" colorSpace="calibratedRGB"/>
<nil key="highlightedColor"/>
Expand Down Expand Up @@ -88,8 +88,8 @@
<rect key="frame" x="0.0" y="0.0" width="375" height="667"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<subviews>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="Label" textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="VTe-uF-1Q8">
<rect key="frame" x="155" y="317" width="66.5" height="33.5"/>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="Second" textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="VTe-uF-1Q8">
<rect key="frame" x="141.5" y="317" width="93.5" height="33.5"/>
<fontDescription key="fontDescription" style="UICTFontTextStyleTitle1"/>
<color key="textColor" red="0.49803921579999999" green="0.49803921579999999" blue="0.49803921579999999" alpha="1" colorSpace="calibratedRGB"/>
<nil key="highlightedColor"/>
Expand Down
3 changes: 2 additions & 1 deletion UnplashExample/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ struct ImagePagingOptions: PagingOptions {
let indicatorOptions: PagingIndicatorOptions = .visible(
height: 1,
zIndex: Int.max,
insets: UIEdgeInsets())
spacing: UIEdgeInsets.zero,
insets: UIEdgeInsets.zero)

let borderOptions: PagingBorderOptions = .visible(
height: 1,
Expand Down