Skip to content

Commit

Permalink
Tweaked the size of each cell to better demonstrate the SnapInAttribu…
Browse files Browse the repository at this point in the history
…tesAnimator.
  • Loading branch information
KelvinJin committed Feb 23, 2017
1 parent cddea2f commit c28e034
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 14 deletions.
8 changes: 6 additions & 2 deletions Source/SnapInAttributesAnimator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,14 @@ public struct SnapInAttributesAnimator: LayoutAttributesAnimator {
if position < 0 && position > -1 {
if attributes.scrollDirection == .horizontal {
let translationX = (1 - pow(abs(position), 3.0)) * attributes.frame.width / 4
attributes.transform = CGAffineTransform(translationX: translationX, y: 0)
let translationTransform = CGAffineTransform(translationX: translationX, y: 0)
let scaleFactor = 0.8 - position * 0.2
attributes.transform = translationTransform.concatenating(CGAffineTransform(scaleX: scaleFactor, y: scaleFactor))
} else {
let translationY = (1 - pow(abs(position), 3.0)) * attributes.frame.height / 4
attributes.transform = CGAffineTransform(translationX: 0, y: translationY)
let translationTransform = CGAffineTransform(translationX: 0, y: translationY)
let scaleFactor = 0.8 - position * 0.2
attributes.transform = translationTransform.concatenating(CGAffineTransform(scaleX: scaleFactor, y: scaleFactor))
}
attributes.alpha = 1.0 * abs(position)
} else {
Expand Down
17 changes: 9 additions & 8 deletions iOS Example/Source/AnimatorTableViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,15 @@ import AnimatedCollectionViewLayout

class AnimatorTableViewController: UITableViewController {

private let animators: [(LayoutAttributesAnimator, Bool)] = [(ParallaxAttributesAnimator(), true),
(ZoomInOutAttributesAnimator(), true),
(RotateInOutAttributesAnimator(), true),
(LinearCardAttributesAnimator(), false),
(CubeAttributesAnimator(), true),
(CrossFadeAttributesAnimator(), true),
(SnapInAttributesAnimator(), true),
(PageAttributesAnimator(), true)]
/// animator, clipToBounds, row, column
private let animators: [(LayoutAttributesAnimator, Bool, Int, Int)] = [(ParallaxAttributesAnimator(), true, 1, 1),
(ZoomInOutAttributesAnimator(), true, 1, 1),
(RotateInOutAttributesAnimator(), true, 1, 1),
(LinearCardAttributesAnimator(), false, 1, 1),
(CubeAttributesAnimator(), true, 1, 1),
(CrossFadeAttributesAnimator(), true, 1, 1),
(PageAttributesAnimator(), true, 1, 1),
(SnapInAttributesAnimator(), true, 2, 4)]

@IBOutlet weak var isHorizontalScrollToggle: UISwitch!

Expand Down
10 changes: 6 additions & 4 deletions iOS Example/Source/ImageCollectionViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class ImageCollectionViewController: UICollectionViewController {

@IBOutlet var dismissGesture: UISwipeGestureRecognizer!

var animator: (LayoutAttributesAnimator, Bool)?
var animator: (LayoutAttributesAnimator, Bool, Int, Int)?
var direction: UICollectionViewScrollDirection = .horizontal

let cellIdentifier = "SimpleCollectionViewCell"
Expand Down Expand Up @@ -96,14 +96,15 @@ extension ImageCollectionViewController: UICollectionViewDelegateFlowLayout {
}

override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return vcs.count
return Int(Int16.max)
}

override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let c = collectionView.dequeueReusableCell(withReuseIdentifier: cellIdentifier, for: indexPath)

if let cell = c as? SimpleCollectionViewCell {
let v = vcs[indexPath.row]
let i = indexPath.row % vcs.count
let v = vcs[i]
cell.bind(color: v.0, imageName: v.1)
cell.clipsToBounds = animator?.1 ?? true
}
Expand All @@ -112,7 +113,8 @@ extension ImageCollectionViewController: UICollectionViewDelegateFlowLayout {
}

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return view.bounds.size
guard let animator = animator else { return view.bounds.size }
return CGSize(width: view.bounds.width / CGFloat(animator.2), height: view.bounds.height / CGFloat(animator.3))
}

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
Expand Down

0 comments on commit c28e034

Please sign in to comment.