-
Notifications
You must be signed in to change notification settings - Fork 36
/
CB3DSelectCell.swift
273 lines (237 loc) · 12 KB
/
CB3DSelectCell.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
//
// CBAnimationSelectionCell.swift
// AnimatedCollection
//
// Created by Anton Skopin on 22/12/2018.
// Copyright © 2018 cuberto. All rights reserved.
//
import UIKit
class CB3DSelectCell: UICollectionViewCell {
enum OffsetDirection {
case left
case right
}
var offsetDirection: OffsetDirection = .right
var animationDuration: CFTimeInterval = 0.35
var maxCornerRadius: CGFloat = 14.0
var selectionColor: UIColor = #colorLiteral(red: 1, green: 0.737254902, blue: 0.2549019608, alpha: 1)
var selectionTimingFunction: CAMediaTimingFunction = CAMediaTimingFunction(name: .easeOut)
private static let animationKey: String = "CBAnimationSelectionCellSelectAnimation"
private var snapshotContainer: UIView = {
let view = UIView()
view.layer.shadowColor = UIColor.black.cgColor
view.translatesAutoresizingMaskIntoConstraints = false
return view
}()
private var snapshotView: UIImageView = {
let imageView = UIImageView()
imageView.layer.shadowColor = UIColor.black.cgColor
imageView.translatesAutoresizingMaskIntoConstraints = false
imageView.clipsToBounds = true
return imageView
}()
private var overlayView: UIView = UIView()
private var overlaySideView: UIView = UIView()
private var csCenterX: NSLayoutConstraint?
private var csCenterY: NSLayoutConstraint?
private var csOverlaySideViewLeft: NSLayoutConstraint?
private var csOverlaySideViewRight: NSLayoutConstraint?
override init(frame: CGRect) {
super.init(frame: frame)
configureViews()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
configureViews()
}
private func configureViews() {
configureOverlay()
configureSnapshot()
}
private func configureOverlay() {
addSubview(overlayView)
overlayView.translatesAutoresizingMaskIntoConstraints = false
overlayView.topAnchor.constraint(equalTo: topAnchor).isActive = true
overlayView.leadingAnchor.constraint(equalTo: leadingAnchor).isActive = true
overlayView.trailingAnchor.constraint(equalTo: trailingAnchor).isActive = true
overlayView.bottomAnchor.constraint(equalTo: bottomAnchor).isActive = true
overlayView.isHidden = true
overlayView.addSubview(overlaySideView)
overlaySideView.translatesAutoresizingMaskIntoConstraints = false
overlaySideView.topAnchor.constraint(equalTo: overlayView.topAnchor).isActive = true
overlaySideView.bottomAnchor.constraint(equalTo: overlayView.bottomAnchor).isActive = true
overlaySideView.widthAnchor.constraint(equalToConstant: 5.0).isActive = true
csOverlaySideViewLeft = overlaySideView.leadingAnchor.constraint(equalTo: overlayView.leadingAnchor)
csOverlaySideViewRight = overlaySideView.trailingAnchor.constraint(equalTo: overlayView.trailingAnchor)
csOverlaySideViewLeft?.isActive = true
}
private func configureSnapshot() {
snapshotContainer.addSubview(snapshotView)
snapshotView.topAnchor.constraint(equalTo: snapshotContainer.topAnchor).isActive = true
snapshotView.leadingAnchor.constraint(equalTo: snapshotContainer.leadingAnchor).isActive = true
snapshotView.trailingAnchor.constraint(equalTo: snapshotContainer.trailingAnchor).isActive = true
snapshotView.bottomAnchor.constraint(equalTo: snapshotContainer.bottomAnchor).isActive = true
let gesture = UITapGestureRecognizer(target: self, action: #selector(snapshotTapped))
snapshotContainer.addGestureRecognizer(gesture)
}
@objc private func snapshotTapped() {
guard _selected else { return }
deselect(animated: true)
if let collectionView = superview as? UICollectionView,
let indexPath = collectionView.indexPath(for: self) {
collectionView.deselectItem(at: indexPath, animated: true)
}
}
var _selected: Bool = false
func deselect(animated: Bool) {
guard _selected else { return }
_selected = false
let finishDeselection: ()->Void = { [weak self] in
if let csCenterX = self?.csCenterX {
self?.snapshotView.superview?.removeConstraint(csCenterX)
}
if let csCenterY = self?.csCenterY {
self?.snapshotView.superview?.removeConstraint(csCenterY)
}
self?.csCenterX = nil
self?.csCenterY = nil
self?.overlayView.isHidden = true
self?.snapshotContainer.removeFromSuperview()
self?.snapshotContainer.layer.removeAllAnimations()
self?.snapshotView.layer.removeAllAnimations()
self?.snapshotContainer.layer.transform = CATransform3DIdentity
self?.overlayView.isHidden = true
}
guard animated else {
finishDeselection()
return
}
let presentationLayer = snapshotContainer.layer.presentation() ?? snapshotContainer.layer
let currentTransform: CGAffineTransform = CATransform3DGetAffineTransform(presentationLayer.transform)
let animationGroup = CAAnimationGroup()
let translate = CABasicAnimation(keyPath: "transform.translation")
translate.fromValue = CGSize(width: currentTransform.tx, height: currentTransform.ty)
translate.toValue = CGSize.zero
translate.timingFunction = selectionTimingFunction
let shadowOpacity = CABasicAnimation(keyPath: "shadowOpacity")
shadowOpacity.fromValue = presentationLayer.shadowOpacity
shadowOpacity.toValue = 0.0
shadowOpacity.timingFunction = selectionTimingFunction
let shadowOffset = CABasicAnimation(keyPath: "shadowOffset")
shadowOffset.fromValue = presentationLayer.shadowOffset
shadowOffset.toValue = CGSize.zero
shadowOffset.timingFunction = selectionTimingFunction
let shadowRadius = CABasicAnimation(keyPath: "shadowRadius")
shadowRadius.fromValue = presentationLayer.shadowRadius
shadowRadius.toValue = 0.0
shadowRadius.timingFunction = selectionTimingFunction
let cornerRadius = CABasicAnimation(keyPath: "cornerRadius")
cornerRadius.fromValue = presentationLayer.shadowRadius
cornerRadius.toValue = layer.cornerRadius
cornerRadius.isRemovedOnCompletion = false
cornerRadius.timingFunction = selectionTimingFunction
cornerRadius.duration = animationDuration
cornerRadius.fillMode = .forwards
let animations: [CAAnimation] = [translate, shadowOpacity, shadowOffset, shadowRadius, cornerRadius]
animationGroup.animations = animations
animationGroup.timingFunction = selectionTimingFunction
animationGroup.isRemovedOnCompletion = false
animationGroup.duration = animationDuration
animationGroup.fillMode = .forwards
CATransaction.begin()
CATransaction.setCompletionBlock {
finishDeselection()
}
snapshotView.layer.add(cornerRadius, forKey: type(of: self).animationKey)
snapshotContainer.layer.add(animationGroup, forKey: type(of: self).animationKey)
CATransaction.commit()
}
func select(animated: Bool) {
guard !_selected else { return }
_selected = true
guard let superview = superview else {
return
}
let wasHidden = layer.isHidden
layer.isHidden = false
UIGraphicsBeginImageContextWithOptions(CGSize(width: frame.width, height: frame.height), false, UIScreen.main.scale)
layer.render(in: UIGraphicsGetCurrentContext()!)
let capturedImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
layer.isHidden = wasHidden
overlayView.backgroundColor = superview.backgroundColor
overlaySideView.backgroundColor = selectionColor
overlayView.isHidden = false
bringSubviewToFront(overlayView)
snapshotView.image = capturedImage
superview.addSubview(snapshotContainer)
superview.bringSubviewToFront(snapshotContainer)
snapshotContainer.widthAnchor.constraint(equalTo: widthAnchor).isActive = true
snapshotContainer.heightAnchor.constraint(equalTo: heightAnchor).isActive = true
if let csCenterX = csCenterX {
snapshotContainer.superview?.removeConstraint(csCenterX)
}
if let csCenterY = csCenterY {
snapshotContainer.superview?.removeConstraint(csCenterY)
}
csCenterX = snapshotContainer.centerXAnchor.constraint(equalTo: centerXAnchor)
csCenterY = snapshotContainer.centerYAnchor.constraint(equalTo: centerYAnchor)
csCenterX?.isActive = true
csCenterY?.isActive = true
snapshotContainer.layoutIfNeeded()
let horOffsetMultiplier: CGFloat
switch offsetDirection {
case .left:
horOffsetMultiplier = -1.0
csOverlaySideViewRight?.isActive = true
csOverlaySideViewLeft?.isActive = false
case .right:
horOffsetMultiplier = 1.0
csOverlaySideViewLeft?.isActive = true
csOverlaySideViewRight?.isActive = false
}
if animated {
let animationGroup = CAAnimationGroup()
let translate = CABasicAnimation(keyPath: "transform.translation")
translate.fromValue = CGSize.zero
translate.toValue = CGSize(width: horOffsetMultiplier * frame.width * 0.2,
height: -frame.height * 0.2)
translate.timingFunction = selectionTimingFunction
let shadowOpacity = CABasicAnimation(keyPath: "shadowOpacity")
shadowOpacity.fromValue = 0
shadowOpacity.toValue = 0.3
shadowOpacity.timingFunction = selectionTimingFunction
let shadowOffset = CABasicAnimation(keyPath: "shadowOffset")
shadowOffset.fromValue = CGSize.zero
shadowOffset.toValue = CGSize(width: horOffsetMultiplier * -20, height: 45)
shadowOffset.timingFunction = selectionTimingFunction
let shadowRadius = CABasicAnimation(keyPath: "shadowRadius")
shadowRadius.fromValue = 0
shadowRadius.toValue = 35.0
shadowRadius.timingFunction = selectionTimingFunction
let cornerRadius = CABasicAnimation(keyPath: "cornerRadius")
cornerRadius.fromValue = layer.cornerRadius
cornerRadius.toValue = maxCornerRadius
cornerRadius.timingFunction = selectionTimingFunction
cornerRadius.isRemovedOnCompletion = false
cornerRadius.fillMode = .forwards
cornerRadius.duration = animationDuration
let animations: [CAAnimation] = [translate, shadowOpacity, shadowOffset, shadowRadius, cornerRadius]
animationGroup.animations = animations
animationGroup.timingFunction = selectionTimingFunction
animationGroup.isRemovedOnCompletion = false
animationGroup.fillMode = .forwards
animationGroup.duration = animationDuration
snapshotContainer.layer.add(animationGroup, forKey: type(of: self).animationKey)
snapshotView.layer.add(cornerRadius, forKey: type(of: self).animationKey)
} else {
snapshotContainer.layer.transform = CATransform3DTranslate(CATransform3DIdentity,
horOffsetMultiplier * frame.width * 0.2,
-frame.height * 0.2, 0)
snapshotContainer.layer.shadowOpacity = 0.3
snapshotContainer.layer.shadowOffset = CGSize(width: horOffsetMultiplier * -20.0, height: 45.0)
snapshotContainer.layer.shadowRadius = 35.0
snapshotContainer.layer.cornerRadius = maxCornerRadius
}
}
}