Skip to content

Upgrade Swift version & Refactor #21

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

Open
wants to merge 1 commit 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
12 changes: 9 additions & 3 deletions VegaScroll.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -109,11 +109,12 @@
DD52B8531F6AC89B00E2AD33 /* Project object */ = {
isa = PBXProject;
attributes = {
LastUpgradeCheck = 0900;
LastUpgradeCheck = 1130;
ORGANIZATIONNAME = "Guilherme Rambo";
TargetAttributes = {
DD52B85B1F6AC89B00E2AD33 = {
CreatedOnToolsVersion = 9.0;
LastSwiftMigration = 1130;
ProvisioningStyle = Automatic;
};
};
Expand All @@ -124,6 +125,7 @@
hasScannedForEncodings = 0;
knownRegions = (
en,
Base,
);
mainGroup = DD52B8521F6AC89B00E2AD33;
productRefGroup = DD52B85D1F6AC89B00E2AD33 /* Products */;
Expand Down Expand Up @@ -171,13 +173,15 @@
CLANG_WARN_BOOL_CONVERSION = YES;
CLANG_WARN_COMMA = YES;
CLANG_WARN_CONSTANT_CONVERSION = YES;
CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES;
CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR;
CLANG_WARN_DOCUMENTATION_COMMENTS = YES;
CLANG_WARN_EMPTY_BODY = YES;
CLANG_WARN_ENUM_CONVERSION = YES;
CLANG_WARN_INFINITE_RECURSION = YES;
CLANG_WARN_INT_CONVERSION = YES;
CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES;
CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES;
CLANG_WARN_OBJC_LITERAL_CONVERSION = YES;
CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
CLANG_WARN_RANGE_LOOP_ANALYSIS = YES;
Expand Down Expand Up @@ -231,13 +235,15 @@
CLANG_WARN_BOOL_CONVERSION = YES;
CLANG_WARN_COMMA = YES;
CLANG_WARN_CONSTANT_CONVERSION = YES;
CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES;
CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR;
CLANG_WARN_DOCUMENTATION_COMMENTS = YES;
CLANG_WARN_EMPTY_BODY = YES;
CLANG_WARN_ENUM_CONVERSION = YES;
CLANG_WARN_INFINITE_RECURSION = YES;
CLANG_WARN_INT_CONVERSION = YES;
CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES;
CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES;
CLANG_WARN_OBJC_LITERAL_CONVERSION = YES;
CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
CLANG_WARN_RANGE_LOOP_ANALYSIS = YES;
Expand Down Expand Up @@ -285,7 +291,7 @@
PRODUCT_BUNDLE_IDENTIFIER = br.com.guilhermerambo.VegaScrollFlowLayout;
PRODUCT_NAME = "$(TARGET_NAME:c99extidentifier)";
SKIP_INSTALL = YES;
SWIFT_VERSION = 4.0;
SWIFT_VERSION = 5.0;
TARGETED_DEVICE_FAMILY = "1,2";
};
name = Debug;
Expand All @@ -305,7 +311,7 @@
PRODUCT_BUNDLE_IDENTIFIER = br.com.guilhermerambo.VegaScrollFlowLayout;
PRODUCT_NAME = "$(TARGET_NAME:c99extidentifier)";
SKIP_INSTALL = YES;
SWIFT_VERSION = 4.0;
SWIFT_VERSION = 5.0;
TARGETED_DEVICE_FAMILY = "1,2";
};
name = Release;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>IDEDidComputeMac32BitWarning</key>
<true/>
</dict>
</plist>
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "0900"
LastUpgradeVersion = "1130"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
Expand All @@ -26,18 +26,14 @@
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
language = ""
shouldUseLaunchSchemeArgsEnv = "YES">
<Testables>
</Testables>
<AdditionalOptions>
</AdditionalOptions>
</TestAction>
<LaunchAction
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
language = ""
launchStyle = "0"
useCustomWorkingDirectory = "NO"
ignoresPersistentStateOnLaunch = "NO"
Expand All @@ -53,8 +49,6 @@
ReferencedContainer = "container:VegaScroll.xcodeproj">
</BuildableReference>
</MacroExpansion>
<AdditionalOptions>
</AdditionalOptions>
</LaunchAction>
<ProfileAction
buildConfiguration = "Release"
Expand Down
72 changes: 33 additions & 39 deletions VegaScroll/Classes/VegaScrollFlowLayout.swift
Original file line number Diff line number Diff line change
Expand Up @@ -51,21 +51,20 @@ open class VegaScrollFlowLayout: UICollectionViewFlowLayout {
super.prepare()
guard let collectionView = collectionView else { return }

// expand the visible rect slightly to avoid flickering when scrolling quickly
let expandBy: CGFloat = -100
// expand the visible rect slightly to avoid flickering when scrolling quickly
let expandBy: CGFloat = -100
let visibleRect = CGRect(origin: collectionView.bounds.origin,
size: collectionView.frame.size).insetBy(dx: 0, dy: expandBy)

guard let visibleItems = super.layoutAttributesForElements(in: visibleRect) else { return }
let indexPathsInVisibleRect = Set(visibleItems.map{ $0.indexPath })

removeNoLongerVisibleBehaviors(indexPathsInVisibleRect: indexPathsInVisibleRect)

let newlyVisibleItems = visibleItems.filter { item in
return !visibleIndexPaths.contains(item.indexPath)

if let visibleItems = super.layoutAttributesForElements(in: visibleRect) {

let indexPathsInVisibleRect = Set(visibleItems.map { $0.indexPath })
removeNoLongerVisibleBehaviors(indexPathsInVisibleRect: indexPathsInVisibleRect)

let newlyVisibleItems = visibleItems.filter { !visibleIndexPaths.contains($0.indexPath) }

addBehaviors(for: newlyVisibleItems)
}

addBehaviors(for: newlyVisibleItems)
}

open override func targetContentOffset(forProposedContentOffset proposedContentOffset: CGPoint,
Expand All @@ -85,28 +84,25 @@ open class VegaScrollFlowLayout: UICollectionViewFlowLayout {
override open func layoutAttributesForElements(in rect: CGRect) -> [UICollectionViewLayoutAttributes]? {
guard let collectionView = collectionView else { return nil }
let dynamicItems = dynamicAnimator.items(in: rect) as? [UICollectionViewLayoutAttributes]

dynamicItems?.forEach { item in
let convertedY = item.center.y - collectionView.contentOffset.y - sectionInset.top
item.zIndex = item.indexPath.row
transformItemIfNeeded(y: convertedY, item: item)
let convertedY = item.center.y - collectionView.contentOffset.y - sectionInset.top
item.zIndex = item.indexPath.row
transformItemIfNeeded(y: convertedY, item: item)
}
return dynamicItems
}

private func transformItemIfNeeded(y: CGFloat, item: UICollectionViewLayoutAttributes) {
guard itemSize.height > 0, y < itemSize.height * 0.5 else {
return
}

let scaleFactor: CGFloat = scaleDistributor(x: y)
private func transformItemIfNeeded(y: CGFloat, item: UICollectionViewLayoutAttributes) {
guard itemSize.height > 0, y < itemSize.height * 0.5 else { return }

let yDelta = getYDelta(y: y)
let scaleFactor: CGFloat = scaleDistributor(x: y)
let yDelta = getYDelta(y: y)

item.transform3D = CATransform3DTranslate(transformIdentity, 0, yDelta, 0)
item.transform3D = CATransform3DScale(item.transform3D, scaleFactor, scaleFactor, scaleFactor)
item.alpha = alphaDistributor(x: y)

}
item.transform3D = CATransform3DTranslate(transformIdentity, 0, yDelta, 0)
item.transform3D = CATransform3DScale(item.transform3D, scaleFactor, scaleFactor, scaleFactor)
item.alpha = alphaDistributor(x: y)
}

override open func layoutAttributesForItem(at indexPath: IndexPath) -> UICollectionViewLayoutAttributes? {
return dynamicAnimator.layoutAttributesForCell(at: indexPath)!
Expand All @@ -119,7 +115,7 @@ open class VegaScrollFlowLayout: UICollectionViewFlowLayout {

let touchLocation = collectionView!.panGestureRecognizer.location(in: collectionView)

dynamicAnimator.behaviors.flatMap { $0 as? UIAttachmentBehavior }.forEach { behavior in
dynamicAnimator.behaviors.compactMap { $0 as? UIAttachmentBehavior }.forEach { behavior in
let attrs = behavior.items.first as! UICollectionViewLayoutAttributes
attrs.center = getUpdatedBehaviorItemCenter(behavior: behavior, touchLocation: touchLocation)
self.dynamicAnimator.updateItem(usingCurrentState: attrs)
Expand Down Expand Up @@ -168,8 +164,8 @@ open class VegaScrollFlowLayout: UICollectionViewFlowLayout {

private func getUpdatedBehaviorItemCenter(behavior: UIAttachmentBehavior,
touchLocation: CGPoint) -> CGPoint {
let yDistanceFromTouch = fabs(touchLocation.y - behavior.anchorPoint.y)
let xDistanceFromTouch = fabs(touchLocation.x - behavior.anchorPoint.x)
let yDistanceFromTouch = abs(touchLocation.y - behavior.anchorPoint.y)
let xDistanceFromTouch = abs(touchLocation.x - behavior.anchorPoint.x)
let scrollResistance = (yDistanceFromTouch + xDistanceFromTouch) / (springHardness * 100)

let attrs = behavior.items.first as! UICollectionViewLayoutAttributes
Expand All @@ -191,24 +187,22 @@ open class VegaScrollFlowLayout: UICollectionViewFlowLayout {
- parameter xOrigin: x coordinate of the function origin.
*/
private func distributor(x: CGFloat, threshold: CGFloat, xOrigin: CGFloat) -> CGFloat {
guard threshold > xOrigin else {
return 1
}
guard threshold > xOrigin else { return 1 }
var arg = (x - xOrigin)/(threshold - xOrigin)
arg = arg <= 0 ? 0 : arg
let y = sqrt(arg)
return y > 1 ? 1 : y
}

private func scaleDistributor(x: CGFloat) -> CGFloat {
return distributor(x: x, threshold: itemSize.height * 0.5, xOrigin: -itemSize.height * 5)
private func scaleDistributor(x: CGFloat) -> CGFloat {
return distributor(x: x, threshold: itemSize.height * 0.5, xOrigin: -itemSize.height * 5)
}

private func alphaDistributor(x: CGFloat) -> CGFloat {
return distributor(x: x, threshold: itemSize.height * 0.5, xOrigin: -itemSize.height)
return distributor(x: x, threshold: itemSize.height * 0.5, xOrigin: -itemSize.height)
}

private func getYDelta(y: CGFloat) -> CGFloat {
return itemSize.height * 0.5 - y
}
private func getYDelta(y: CGFloat) -> CGFloat {
return itemSize.height * 0.5 - y
}
}