Skip to content

Commit

Permalink
+ Updated all the animators to support vertical scrolling.
Browse files Browse the repository at this point in the history
+ Removed TurnAttributesAnimator since it doesn’t look as expected.
  • Loading branch information
KelvinJin committed Feb 22, 2017
1 parent 02799a2 commit 789e510
Show file tree
Hide file tree
Showing 10 changed files with 48 additions and 63 deletions.
4 changes: 0 additions & 4 deletions AnimatedCollectionViewLayout.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
29A103111E4AEE1F00F03854 /* CubeAttributesAnimator.swift in Sources */ = {isa = PBXBuildFile; fileRef = 29A103101E4AEE1F00F03854 /* CubeAttributesAnimator.swift */; };
29A103131E4AEE4200F03854 /* UIView+Anchor.swift in Sources */ = {isa = PBXBuildFile; fileRef = 29A103121E4AEE4200F03854 /* UIView+Anchor.swift */; };
29A103151E4AEE5A00F03854 /* CrossFadeAttributesAnimator.swift in Sources */ = {isa = PBXBuildFile; fileRef = 29A103141E4AEE5A00F03854 /* CrossFadeAttributesAnimator.swift */; };
29A103171E4AEE7400F03854 /* TurnAttributesAnimator.swift in Sources */ = {isa = PBXBuildFile; fileRef = 29A103161E4AEE7400F03854 /* TurnAttributesAnimator.swift */; };
29A103191E4AEE8900F03854 /* PageAttributesAnimator.swift in Sources */ = {isa = PBXBuildFile; fileRef = 29A103181E4AEE8900F03854 /* PageAttributesAnimator.swift */; };
D9899DD41DF4A092008766B5 /* AnimatedCollectionViewLayout.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = D9899DCA1DF4A092008766B5 /* AnimatedCollectionViewLayout.framework */; };
D9899DD91DF4A093008766B5 /* AnimatedCollectionViewLayoutTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = D9899DD81DF4A093008766B5 /* AnimatedCollectionViewLayoutTests.swift */; };
Expand All @@ -42,7 +41,6 @@
29A103101E4AEE1F00F03854 /* CubeAttributesAnimator.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CubeAttributesAnimator.swift; sourceTree = "<group>"; };
29A103121E4AEE4200F03854 /* UIView+Anchor.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "UIView+Anchor.swift"; sourceTree = "<group>"; };
29A103141E4AEE5A00F03854 /* CrossFadeAttributesAnimator.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CrossFadeAttributesAnimator.swift; sourceTree = "<group>"; };
29A103161E4AEE7400F03854 /* TurnAttributesAnimator.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TurnAttributesAnimator.swift; sourceTree = "<group>"; };
29A103181E4AEE8900F03854 /* PageAttributesAnimator.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = PageAttributesAnimator.swift; sourceTree = "<group>"; };
D9899DCA1DF4A092008766B5 /* AnimatedCollectionViewLayout.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = AnimatedCollectionViewLayout.framework; sourceTree = BUILT_PRODUCTS_DIR; };
D9899DCD1DF4A092008766B5 /* AnimatedCollectionViewLayout.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = AnimatedCollectionViewLayout.h; sourceTree = "<group>"; };
Expand Down Expand Up @@ -81,7 +79,6 @@
29A1030E1E4AEDF800F03854 /* LinearCardAttributesAnimator.swift */,
29A103101E4AEE1F00F03854 /* CubeAttributesAnimator.swift */,
29A103141E4AEE5A00F03854 /* CrossFadeAttributesAnimator.swift */,
29A103161E4AEE7400F03854 /* TurnAttributesAnimator.swift */,
29A103181E4AEE8900F03854 /* PageAttributesAnimator.swift */,
);
name = Animators;
Expand Down Expand Up @@ -248,7 +245,6 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
29A103171E4AEE7400F03854 /* TurnAttributesAnimator.swift in Sources */,
29A1030F1E4AEDF800F03854 /* LinearCardAttributesAnimator.swift in Sources */,
D9899DE61DF4A372008766B5 /* AnimatedCollectionViewLayout.swift in Sources */,
29A103091E4AED5900F03854 /* ParallaxAttributesAnimator.swift in Sources */,
Expand Down
12 changes: 10 additions & 2 deletions Source/AnimatedCollectionViewLayout.swift
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,6 @@ public class AnimatedCollectionViewLayout: UICollectionViewFlowLayout {
public class PagerCollectionViewLayoutAttributes: UICollectionViewLayoutAttributes {
public var contentView: UIView?
public var scrollDirection: UICollectionViewScrollDirection = .vertical
public var originFrame: CGRect = .zero

public var startOffset: CGFloat = 0
public var middleOffset: CGFloat = 0
Expand All @@ -86,10 +85,19 @@ public class PagerCollectionViewLayoutAttributes: UICollectionViewLayoutAttribut
let copy = super.copy(with: zone) as! PagerCollectionViewLayoutAttributes
copy.contentView = contentView
copy.scrollDirection = scrollDirection
copy.originFrame = originFrame
copy.startOffset = startOffset
copy.middleOffset = middleOffset
copy.endOffset = endOffset
return copy
}

public override func isEqual(_ object: Any?) -> Bool {
guard let o = object as? PagerCollectionViewLayoutAttributes else { return false }

return o.contentView == contentView
&& o.scrollDirection == scrollDirection
&& o.startOffset == startOffset
&& o.middleOffset == middleOffset
&& o.endOffset == endOffset
}
}
11 changes: 9 additions & 2 deletions Source/CubeAttributesAnimator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,22 @@ public struct CubeAttributeAnimator: LayoutAttributesAnimator {
let position = attributes.middleOffset
if abs(position) >= 1 {
attributes.contentView?.layer.transform = CATransform3DIdentity
} else {
} else if attributes.scrollDirection == .horizontal {
let rotateAngle = totalAngle * position
var transform = CATransform3DIdentity
transform.m34 = perspective
transform = CATransform3DRotate(transform, rotateAngle, 0, 1, 0)

// attributes.transform = transform
attributes.contentView?.layer.transform = transform
attributes.contentView?.keepCenterAndApplyAnchorPoint(CGPoint(x: position > 0 ? 0 : 1, y: 0.5))
} else {
let rotateAngle = totalAngle * position
var transform = CATransform3DIdentity
transform.m34 = perspective
transform = CATransform3DRotate(transform, rotateAngle, -1, 0, 0)

attributes.contentView?.layer.transform = transform
attributes.contentView?.keepCenterAndApplyAnchorPoint(CGPoint(x: 0.5, y: position > 0 ? 0 : 1))
}
}
}
16 changes: 12 additions & 4 deletions Source/LinearCardAttributesAnimator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,20 @@ public struct LinearCardAttributeAnimator: LayoutAttributesAnimator {

public func animate(collectionView: UICollectionView, attributes: PagerCollectionViewLayoutAttributes) {
let position = attributes.middleOffset
let width = collectionView.frame.width
let transitionX = -(width * itemSpacing * position)
let scaleFactor = scaleRate - 0.1 * abs(position)

let scaleTransform = CGAffineTransform(scaleX: scaleFactor, y: scaleFactor)
let transitionTransform = CGAffineTransform(translationX: transitionX, y: 0)

let transitionTransform: CGAffineTransform

if attributes.scrollDirection == .horizontal {
let width = collectionView.frame.width
let transitionX = -(width * itemSpacing * position)
transitionTransform = CGAffineTransform(translationX: transitionX, y: 0)
} else {
let height = collectionView.frame.height
let transitionY = -(height * itemSpacing * position)
transitionTransform = CGAffineTransform(translationX: 0, y: transitionY)
}

attributes.alpha = 1.0 - abs(position) + minAlpha
attributes.transform = transitionTransform.concatenating(scaleTransform)
Expand Down
8 changes: 7 additions & 1 deletion Source/PageAttributesAnimator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,13 @@ public struct PageAttributeAnimator: LayoutAttributesAnimator {
let itemOrigin = attributes.frame.origin
let scaleFactor = scaleRate * min(position, 0) + 1.0
var transform = CGAffineTransform(scaleX: scaleFactor, y: scaleFactor)
transform = transform.concatenating(CGAffineTransform(translationX: position < 0 ? contentOffset.x - itemOrigin.x : 0, y: 0))

if attributes.scrollDirection == .horizontal {
transform = transform.concatenating(CGAffineTransform(translationX: position < 0 ? contentOffset.x - itemOrigin.x : 0, y: 0))
} else {
transform = transform.concatenating(CGAffineTransform(translationX: 0, y: position < 0 ? contentOffset.y - itemOrigin.y : 0))
}

attributes.transform = transform
attributes.zIndex = attributes.indexPath.row
}
Expand Down
2 changes: 1 addition & 1 deletion Source/ParallaxAttributesAnimator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public struct ParallaxAttributesAnimator: LayoutAttributesAnimator {

if abs(position) >= 1 {
// Reset views that are invisible.
contentView.transform = .identity
contentView.frame = attributes.bounds
} else if direction == .horizontal {
let width = collectionView.frame.width
let transitionX = -(width * speed * position)
Expand Down
6 changes: 2 additions & 4 deletions Source/RotateInOutAttributesAnimator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@
import UIKit

/// An animator that rotating the cell in/out when you scroll.
/// - warning: You have to set `clipsToBounds` to `false` on the cell for this to
/// be effective. This might not be needed in the future release though.
public struct RotateInOutAttributesAnimator: LayoutAttributesAnimator {
/// The alpha to apply on the cells that are away from the center. Should be
/// in range [0, 1]. 0 by default.
Expand All @@ -28,13 +26,13 @@ public struct RotateInOutAttributesAnimator: LayoutAttributesAnimator {
public func animate(collectionView: UICollectionView, attributes: PagerCollectionViewLayoutAttributes) {
let position = attributes.middleOffset
if abs(position) >= 1 {
attributes.contentView?.transform = .identity
attributes.transform = .identity
attributes.alpha = 1.0
} else {
let rotateFactor = maxRotate * position
attributes.zIndex = attributes.indexPath.row
attributes.alpha = 1.0 - abs(position) + minAlpha
attributes.contentView?.transform = CGAffineTransform(rotationAngle: rotateFactor)
attributes.transform = CGAffineTransform(rotationAngle: rotateFactor)
}
}
}
38 changes: 0 additions & 38 deletions Source/TurnAttributesAnimator.swift

This file was deleted.

11 changes: 6 additions & 5 deletions Source/ZoomInOutAttributesAnimator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import UIKit
/// An animator that zoom in/out cells when you scroll.
public struct ZoomInOutAttributesAnimator: LayoutAttributesAnimator {
/// The scaleRate decides the maximum scale rate where 0 means no scale and
/// 1 means the size will be double at max and disappearing at min.
/// 1 means the cell will disappear at min. 0.2 by default.
public var scaleRate: CGFloat

public init(scaleRate: CGFloat = 0.2) {
Expand All @@ -20,11 +20,12 @@ public struct ZoomInOutAttributesAnimator: LayoutAttributesAnimator {

public func animate(collectionView: UICollectionView, attributes: PagerCollectionViewLayoutAttributes) {
let position = attributes.middleOffset
if abs(position) >= 1 {
attributes.contentView?.transform = .identity
} else {

if position <= 0 && position > -1 {
let scaleFactor = scaleRate * position + 1.0
attributes.contentView?.transform = CGAffineTransform(scaleX: scaleFactor, y: scaleFactor)
attributes.transform = CGAffineTransform(scaleX: scaleFactor, y: scaleFactor)
} else {
attributes.transform = .identity
}
}
}
3 changes: 1 addition & 2 deletions iOS Example/Source/AnimatorTableViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,10 @@ class AnimatorTableViewController: UITableViewController {

private let animators: [(LayoutAttributesAnimator, Bool)] = [(ParallaxAttributesAnimator(), true),
(ZoomInOutAttributesAnimator(), true),
(RotateInOutAttributesAnimator(), false),
(RotateInOutAttributesAnimator(), true),
(LinearCardAttributeAnimator(), false),
(CubeAttributeAnimator(), true),
(CrossFadeAttributeAnimator(), true),
(TurnAttributeAnimator(), true),
(PageAttributeAnimator(), true)]

@IBOutlet weak var isHorizontalScrollToggle: UISwitch!
Expand Down

0 comments on commit 789e510

Please sign in to comment.