-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMyFlowLayout.swift
More file actions
78 lines (55 loc) · 2.5 KB
/
MyFlowLayout.swift
File metadata and controls
78 lines (55 loc) · 2.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
//
// MyFlowLayout.swift
// NoteSwap
//
// Created by Nick Dugal on 4/17/16.
// Copyright © 2016 Nick Dugal. All rights reserved.
//
import UIKit
protocol PinterestLayoutDelegate {
// 1
func collectionView(collectionView:UICollectionView, heightForPhotoAtIndexPath indexPath:NSIndexPath, withWidth:CGFloat) -> CGFloat
// 2
func collectionView(collectionView: UICollectionView, heightForAnnotationAtIndexPath indexPath: NSIndexPath, withWidth width: CGFloat) -> CGFloat
}
//CLASS TO ADD FUNCTIONALITY TO CELLS IN COLLECTION VIEW. PROPERTIES(INITIAL ONES) ARE TO STORE INFO ON CELL BEING MANIPULATED, SCALE VALUE FOR RESIZE, AND CUR LOC OF CELL
class MyFlowLayout: UICollectionViewFlowLayout {
var currentCellPath: NSIndexPath?
var currentCellCenter: CGPoint?
var currentCellScale: CGFloat?
var numberOfColumns = 2
var cellPadding: CGFloat = 6.0
private var cache = [UICollectionViewLayoutAttributes]()
func setCurrentCellScale(scale: CGFloat) {
currentCellScale = scale
self.invalidateLayout()
}
func setCurrentCellCenter(origin: CGPoint) {
currentCellCenter = origin
self.invalidateLayout()
}
override func layoutAttributesForItemAtIndexPath(indexPath: NSIndexPath) -> UICollectionViewLayoutAttributes? {
//Allows class to know what attributes are normally in order to customize them
let attributes = super.layoutAttributesForItemAtIndexPath(indexPath)
self.modifyLayoutAttributes(attributes!)
return attributes
}
override func layoutAttributesForElementsInRect(rect: CGRect) -> [UICollectionViewLayoutAttributes]? {
let allAtttributesInRect = super.layoutAttributesForElementsInRect(rect)
for cellAttributes in allAtttributesInRect! {
self.modifyLayoutAttributes(cellAttributes )
}
return allAtttributesInRect!
}
//This is where the layout attributes for the cell the user is currently manipulating on the screen are modified.
func modifyLayoutAttributes(layoutattributes:
UICollectionViewLayoutAttributes) {
if layoutattributes.indexPath == currentCellPath {
layoutattributes.transform3D =
CATransform3DMakeScale(currentCellScale!,
currentCellScale!, 1.0)
layoutattributes.center = currentCellCenter!
layoutattributes.zIndex = 1
}
}
}