forked from seedante/SDECollectionViewAlbumTransition
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSDEPushAndPopAnimationController.swift
286 lines (219 loc) · 13 KB
/
SDEPushAndPopAnimationController.swift
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
//
// SDETransitionAnimator.swift
// CustomCollectionViewTransition
//
// Created by seedante on 15/7/11.
// Copyright © 2015年 seedante. All rights reserved.
//
import UIKit
class SDEPushAndPopAnimationController: NSObject, UIViewControllerAnimatedTransitioning {
let coverEdgeInSets = UIEdgeInsets(top: 10, left: 10, bottom: 10, right: 10)
let coverViewBackgroundColor = UIColor.darkGrayColor()
let horizontalCount = 5
var verticalCount = 1 //>= 1
var horizontalGap: CGFloat = 0
var verticalGap: CGFloat = 0
private let kAnimationDuration: Double = 0.8
private let kCellAnimationSmallDelta: Double = 0.01
private let kCellAnimationBigDelta: Double = 0.03
private var operation: UINavigationControllerOperation
init(operation: UINavigationControllerOperation){
self.operation = operation
super.init()
}
//MARK: Protocol Method
func transitionDuration(transitionContext: UIViewControllerContextTransitioning?) -> NSTimeInterval {
return kAnimationDuration
}
func animateTransition(transitionContext: UIViewControllerContextTransitioning) {
let containerView = transitionContext.containerView()
let fromVC = transitionContext.viewControllerForKey(UITransitionContextFromViewControllerKey) as? UICollectionViewController
let toVC = transitionContext.viewControllerForKey(UITransitionContextToViewControllerKey) as? UICollectionViewController
let fromView = transitionContext.viewForKey(UITransitionContextFromViewKey)
let toView = transitionContext.viewForKey(UITransitionContextToViewKey)
let duration = transitionDuration(transitionContext)
switch operation{
case .Push:
let selectedCell = fromVC?.collectionView?.cellForItemAtIndexPath(fromVC!.selectedIndexPath)
selectedCell?.hidden = true
let layoutAttributes = fromVC!.collectionView?.layoutAttributesForItemAtIndexPath(fromVC!.selectedIndexPath)
let areaRect = fromVC!.collectionView?.convertRect(layoutAttributes!.frame, toView: fromVC!.collectionView?.superview)
toVC!.coverRectInSuperview = areaRect!
//key code, the most important code here. without this line, you can't get visibleCells from UICollectionView.
//And, there are other ways, Just make view redraw.
toVC?.view.layoutIfNeeded()
setupVisibleCellsBeforePushToVC(toVC!)
containerView?.addSubview(toView!)
let fakeCoverView = createAndSetupFakeCoverView(fromVC!, toVC: toVC!)
UIView.setAnimationCurve(UIViewAnimationCurve.EaseOut)
let options: UIViewKeyframeAnimationOptions = [.BeginFromCurrentState, .OverrideInheritedDuration, .CalculationModeCubic, .CalculationModeCubicPaced]
UIView.animateKeyframesWithDuration(duration, delay: 0, options: options, animations: {
self.addkeyFrameAnimationForBackgroundColorInPush(fromVC!, toVC: toVC!)
self.addKeyFrameAnimationInPushForFakeCoverView(fakeCoverView)
self.addKeyFrameAnimationOnVisibleCellsInPushToVC(toVC!)
}, completion: { finished in
let isCancelled = transitionContext.transitionWasCancelled()
if isCancelled{
selectedCell?.hidden = false
}
transitionContext.completeTransition(!isCancelled)
})
case .Pop:
containerView?.insertSubview(toView!, belowSubview: fromView!)
let coverView = fromView?.viewWithTag(1000)
UIView.setAnimationCurve(UIViewAnimationCurve.EaseInOut)
UIView.animateKeyframesWithDuration(duration, delay: 0, options: UIViewKeyframeAnimationOptions(), animations: {
self.addkeyFrameAnimationForBackgroundColorInPop(fromVC!)
self.addKeyFrameAnimationInPopForFakeCoverView(coverView)
self.addKeyFrameAnimationOnVisibleCellsInPopFromVC(fromVC!)
}, completion: { finished in
let isCancelled = transitionContext.transitionWasCancelled()
if !isCancelled{
let selectedCell = toVC?.collectionView?.cellForItemAtIndexPath(toVC!.selectedIndexPath)
selectedCell?.hidden = false
}
transitionContext.completeTransition(!isCancelled)
})
default:break
}
}
//MARK: Push Transition Helper Method
private func createAndSetupFakeCoverView(fromVC: UICollectionViewController, toVC: UICollectionViewController) -> UIView?{
let selectedCell = fromVC.collectionView?.cellForItemAtIndexPath(fromVC.selectedIndexPath)
let snapshotCellView = selectedCell!.snapshotViewAfterScreenUpdates(false)
snapshotCellView.tag = 10
let coverContainerView = UIView(frame: snapshotCellView.frame)
coverContainerView.backgroundColor = coverViewBackgroundColor
coverContainerView.addSubview(snapshotCellView)
coverContainerView.tag = 1000
toVC.view.addSubview(coverContainerView)
coverContainerView.frame = toVC.coverRectInSuperview
let frame = coverContainerView.frame
coverContainerView.layer.anchorPoint = CGPointMake(0, 0.5)
coverContainerView.frame = frame
return coverContainerView
}
private func addKeyFrameAnimationInPushForFakeCoverView(coverView: UIView?){
UIView.addKeyframeWithRelativeStartTime(0, relativeDuration: 0.5, animations: {
var flipLeftTransform = CATransform3DIdentity
flipLeftTransform.m34 = -1.0 / 500.0
flipLeftTransform = CATransform3DRotate(flipLeftTransform, CGFloat(-M_PI), 0.0, 1.0, 0.0)
coverView?.layer.transform = flipLeftTransform
})
UIView.addKeyframeWithRelativeStartTime(0.45, relativeDuration: 0.05, animations: {
coverView?.alpha = 0
})
//fix Transparent Background In Flip Animation
let snapshotView = coverView?.viewWithTag(10)
UIView.addKeyframeWithRelativeStartTime(0.25, relativeDuration: 0.01, animations: {
snapshotView?.alpha = 0
})
}
private func addkeyFrameAnimationForBackgroundColorInPush(fromVC: UICollectionViewController, toVC: UICollectionViewController){
UIView.addKeyframeWithRelativeStartTime(0, relativeDuration: 1.0, animations: {
let toCollectionViewBackgroundColor = fromVC.collectionView?.backgroundColor
toVC.collectionView?.backgroundColor = toCollectionViewBackgroundColor
})
}
private func addKeyFrameAnimationOnVisibleCellsInPushToVC(toVC: UICollectionViewController){
let collectionView = toVC.collectionView!
for cell in collectionView.visibleCells(){
let indexPath = collectionView.indexPathForCell(cell)!
let layoutAttributes = collectionView.layoutAttributesForItemAtIndexPath(indexPath)
var column = indexPath.row / horizontalCount
let row = indexPath.row % horizontalCount
if (column + 1) * (row + 1) > horizontalCount * verticalCount{
column = ((column + 1) * (row + 1)) % (horizontalCount * verticalCount) / horizontalCount
}
let columns = verticalCount
let relativeStartTime = (self.kCellAnimationBigDelta * Double(indexPath.row % columns))
let relativeDuration = 0.5 - (self.kCellAnimationSmallDelta * Double(indexPath.row))
UIView.addKeyframeWithRelativeStartTime(0, relativeDuration: 0.7, animations: {
cell.alpha = 1
})
UIView.addKeyframeWithRelativeStartTime(0.5 + relativeStartTime, relativeDuration: relativeDuration, animations: {
cell.transform = CGAffineTransformScale(CGAffineTransformIdentity, 1, 1)
})
UIView.addKeyframeWithRelativeStartTime(0.5 + relativeStartTime, relativeDuration: relativeDuration, animations: {
cell.center = layoutAttributes!.center
})
}
}
private func setupVisibleCellsBeforePushToVC(toVC:UICollectionViewController){
if toVC.collectionView?.visibleCells().count > 0{
let areaRect = toVC.collectionView!.convertRect(toVC.coverRectInSuperview, fromView: toVC.collectionView!.superview)
let cellsAreaRect = UIEdgeInsetsInsetRect(areaRect, coverEdgeInSets)
let cellWidth = (cellsAreaRect.width - CGFloat(horizontalCount - 1) * horizontalGap) / CGFloat(horizontalCount)
let cellHeight = cellWidth
verticalCount = Int((cellsAreaRect.height + verticalGap) / (cellHeight + verticalGap))
for cell in toVC.collectionView!.visibleCells(){
let indexPath = toVC.collectionView!.indexPathForCell(cell)
var column = indexPath!.row / horizontalCount
let row = indexPath!.row % horizontalCount
if (column + 1) * (row + 1) > horizontalCount * verticalCount{
column = ((column + 1) * (row + 1)) % (horizontalCount * verticalCount) / horizontalCount
}
let centerY: CGFloat = cellsAreaRect.origin.y + cellHeight / 2 + CGFloat(column) * (cellHeight + verticalGap)
let centerX = cellsAreaRect.origin.x + cellWidth / 2 + CGFloat(row) * (cellWidth + horizontalGap)
cell.center = CGPoint(x: centerX, y: centerY)
let widthScale = cellWidth / cell.frame.width
let heightScale = cellHeight / cell.frame.height
cell.transform = CGAffineTransformScale(CGAffineTransformIdentity, widthScale, heightScale)
cell.alpha = 0
cell.layer.zPosition = CGFloat(100 - indexPath!.row)
}
}
}
//MARK: Pop Transition Helper Method
private func addkeyFrameAnimationForBackgroundColorInPop(fromVC: UICollectionViewController){
UIView.addKeyframeWithRelativeStartTime(0, relativeDuration: 0.5, animations: {
fromVC.collectionView?.backgroundColor = UIColor.clearColor()
})
}
private func addKeyFrameAnimationInPopForFakeCoverView(coverView: UIView?){
UIView.addKeyframeWithRelativeStartTime(0.5, relativeDuration: 0.1, animations: {
coverView?.alpha = 1
})
UIView.addKeyframeWithRelativeStartTime(0.5, relativeDuration: 0.5, animations: {
coverView?.layer.transform = CATransform3DIdentity
})
//fix Transparent Background In Flip Animation
let snapshotView = coverView?.viewWithTag(10)
UIView.addKeyframeWithRelativeStartTime(0.75, relativeDuration: 0.01, animations: {
snapshotView?.alpha = 1
})
}
private func addKeyFrameAnimationOnVisibleCellsInPopFromVC(fromVC: UICollectionViewController) {
let visibleCellIndexPaths = fromVC.collectionView?.indexPathsForVisibleItems()
if visibleCellIndexPaths?.count > 0{
//find minimal indexpath's row
let rows = visibleCellIndexPaths!.map({$0.row})
let minimalRow = rows.reduce(Int.max, combine: { $0 < $1 ? $0 : $1 })
let collectionView = fromVC.collectionView!
let areaRect = collectionView.convertRect(fromVC.coverRectInSuperview, fromView: fromVC.collectionView!.superview)
let cellsAreaRect = UIEdgeInsetsInsetRect(areaRect, coverEdgeInSets)
let cellWidth = (cellsAreaRect.width - CGFloat(horizontalCount - 1) * horizontalGap) / CGFloat(horizontalCount)
let cellHeight = cellWidth
verticalCount = Int((cellsAreaRect.height + verticalGap) / (cellHeight + verticalGap))
for indexPath in visibleCellIndexPaths!{
let relativeRow = indexPath.row - minimalRow
var column = relativeRow / horizontalCount
let row = relativeRow % horizontalCount
if (column + 1) * (row + 1) > horizontalCount * verticalCount{
column = ((column + 1) * (row + 1)) % (horizontalCount * verticalCount) / horizontalCount
}
let centerY: CGFloat = cellsAreaRect.origin.y + cellHeight / 2 + CGFloat(column) * (cellHeight + verticalGap)
let centerX = cellsAreaRect.origin.x + cellWidth / 2 + CGFloat(row) * (cellWidth + horizontalGap)
let cell = collectionView.cellForItemAtIndexPath(indexPath)!
let widthScale = cellWidth / cell.frame.width
let heightScale = cellHeight / cell.frame.height
let relativeStartTime = (self.kCellAnimationBigDelta * Double(relativeRow % verticalCount))
let relativeDuration = 0.5 - (self.kCellAnimationSmallDelta * Double(relativeRow))
UIView.addKeyframeWithRelativeStartTime(relativeStartTime, relativeDuration: relativeDuration, animations: {
cell.center = CGPoint(x: centerX, y: centerY)
cell.transform = CGAffineTransformScale(CGAffineTransformIdentity, widthScale, heightScale)
})
}
}
}
}