Skip to content

Instantly share code, notes, and snippets.

@Oni-zerone
Last active May 2, 2018 19:49
Show Gist options
  • Save Oni-zerone/bca974e4e805047ddb35e71f6bcf34b3 to your computer and use it in GitHub Desktop.
Save Oni-zerone/bca974e4e805047ddb35e71f6bcf34b3 to your computer and use it in GitHub Desktop.
A card zooming custom flow layout
//
// ZoomCardFlowLayout.swift
//
//
// Created by Oni_01 on 12/05/15.
// Copyright (c) 2015 Andrea Altea. All rights reserved.
//
import UIKit
class ZoomCardFlowLayout: UICollectionViewFlowLayout {
override init() {
super.init();
setLayout();
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder);
setLayout();
}
func setLayout() {
self.minimumInteritemSpacing = 0;
self.minimumLineSpacing = 0;
self.scrollDirection = UICollectionViewScrollDirection.Horizontal;
self.sectionInset = UIEdgeInsetsMake(0, 38, 0, 38); //UIEdgeInsetsMake(15, 38, 10, 38);
}
override func prepareLayout() {
super.prepareLayout();
//Get Visible index paths
if let indexPaths = self.collectionView?.indexPathsForVisibleItems() {
//CollectionView Center
let center = self.collectionView?.center;
for obj in indexPaths {
let indexPath = obj as! NSIndexPath;
if let cell = self.collectionView?.cellForItemAtIndexPath(indexPath) {
//Get Cell Center in CollectionView and calculate distance
let cellCenter = self.collectionView!.convertPoint(cell.center, fromCoordinateSpace: self.collectionView!);
let contentOffset = self.collectionView!.contentOffset.x;
let xDistance = fabsf(Float(center!.x - cellCenter.x + contentOffset));
//Scale Factor based on view width
var scale = 1 - CGFloat(xDistance / 1000.0 /*multiplier*/);
scale = scale > 1 ? 1 : scale;
scale = scale < 0.85 ? 0.85 : scale;
cell.transform = CGAffineTransformMakeScale(scale, scale);
//Added shadow Effect
cell.layer.shadowRadius = (93.75 * (scale * scale)) - 73.75 * scale;
cell.layer.shadowOffset = CGSizeMake(0, 4.0 * scale);
}
}
}
}
override func collectionViewContentSize() -> CGSize {
var size = super.collectionViewContentSize();
if let view = self.collectionView {
let height = view.bounds.height - view.contentInset.top - view.contentInset.bottom;
size.height = height;
}
return size;
}
override func shouldInvalidateLayoutForBoundsChange(newBounds: CGRect) -> Bool {
return true;
}
//MARK: - Pagination
override func targetContentOffsetForProposedContentOffset(proposedContentOffset: CGPoint, withScrollingVelocity velocity: CGPoint) -> CGPoint {
if let attributes = self.layoutAttributesForElementsInRect(self.collectionView!.bounds) {
if attributes.count == 0 {
return proposedContentOffset;
} else {
var candidate = attributes.first!;
for attribute in attributes {
if attribute.representedElementCategory != .Cell {
continue;
}
if (velocity.x > 0.0 && attribute.center.x > candidate.center.x) ||
(velocity.x <= 0.0 && attribute.center.x < candidate.center.x) {
candidate = attribute;
}
}
return CGPointMake(candidate.center.x - self.collectionView!.bounds.size.width * 0.5,
proposedContentOffset.y);
}
}
return CGPointZero;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment