This repository has been archived by the owner on Aug 13, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 80
/
ContextualTransitionExample.swift
310 lines (241 loc) · 11.1 KB
/
ContextualTransitionExample.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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
/*
Copyright 2016-present The Material Motion Authors. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
import UIKit
import MaterialMotion
let numberOfImageAssets = 10
let numberOfPhotosInAlbum = 30
struct Photo {
let name: String
let image: UIImage
let uuid: String
fileprivate init(name: String) {
self.uuid = NSUUID().uuidString
self.name = name
// NOTE: In a real app you should never load images from disk on the UI thread like this.
// Instead, you should find some way to cache the thumbnails in memory and then asynchronously
// load the full-size photos from disk/network when needed. The photo library APIs provide
// exactly this sort of behavior (square thumbnails are accessible immediately on the UI thread
// while the full-sized photos need to be loaded asynchronously).
self.image = UIImage(named: "\(self.name).jpg")!
}
}
class PhotoAlbum {
let photos: [Photo]
let identifierToIndex: [String: Int]
init() {
var photos: [Photo] = []
var identifierToIndex: [String: Int] = [:]
for index in 0..<numberOfPhotosInAlbum {
let photo = Photo(name: "image\(index % numberOfImageAssets)")
photos.append(photo)
identifierToIndex[photo.uuid] = index
}
self.photos = photos
self.identifierToIndex = identifierToIndex
}
}
private let photoCellIdentifier = "photoCell"
private class PhotoCollectionViewCell: UICollectionViewCell {
let imageView = UIImageView()
override init(frame: CGRect) {
super.init(frame: frame)
imageView.contentMode = .scaleAspectFill
imageView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
imageView.frame = bounds
imageView.clipsToBounds = true
contentView.addSubview(imageView)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
func imageViewForTransition() -> UIImageView {
return imageView
}
}
public class ContextualTransitionExampleViewController: UICollectionViewController, TransitionContextViewRetriever {
let album = PhotoAlbum()
init() {
super.init(collectionViewLayout: UICollectionViewFlowLayout())
}
public required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override public func viewDidLoad() {
super.viewDidLoad()
collectionView!.backgroundColor = .white
collectionView!.register(PhotoCollectionViewCell.self,
forCellWithReuseIdentifier: photoCellIdentifier)
}
public override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
updateLayout()
}
func updateLayout() {
let layout = collectionView!.collectionViewLayout as! UICollectionViewFlowLayout
layout.sectionInset = .init(top: 4, left: 4, bottom: 4, right: 4)
layout.minimumInteritemSpacing = 4
layout.minimumLineSpacing = 4
let numberOfColumns: CGFloat = 3
let squareDimension = (view.bounds.width - layout.sectionInset.left - layout.sectionInset.right - (numberOfColumns - 1) * layout.minimumInteritemSpacing) / numberOfColumns
layout.itemSize = CGSize(width: squareDimension, height: squareDimension)
}
public override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return album.photos.count
}
public override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: photoCellIdentifier,
for: indexPath) as! PhotoCollectionViewCell
let photo = album.photos[indexPath.row]
cell.imageView.image = photo.image
return cell
}
public override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
let viewController = PhotoAlbumViewController(album: album)
viewController.currentPhoto = album.photos[indexPath.row]
present(viewController, animated: true)
}
public func contextViewForTransition(foreViewController: UIViewController) -> UIView? {
guard let photoViewController = foreViewController as? PhotoAlbumViewController else {
return nil
}
let currentPhoto = photoViewController.currentPhoto
guard let photoIndex = album.identifierToIndex[currentPhoto.uuid] else {
return nil
}
let photoIndexPath = IndexPath(item: photoIndex, section: 0)
guard let visibleView = collectionView?.cellForItem(at: photoIndexPath) else {
collectionView?.scrollToItem(at: photoIndexPath, at: .top, animated: false)
collectionView?.reloadItems(at: [photoIndexPath])
return collectionView?.cellForItem(at: photoIndexPath)
}
return visibleView
}
}
class PhotoAlbumViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate {
var collectionView: UICollectionView!
var currentPhoto: Photo
let album: PhotoAlbum
init(album: PhotoAlbum) {
self.album = album
self.currentPhoto = self.album.photos.first!
super.init(nibName: nil, bundle: nil)
transitionController.transition = PushBackTransition()
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func viewDidLoad() {
super.viewDidLoad()
automaticallyAdjustsScrollViewInsets = false
let layout = UICollectionViewFlowLayout()
layout.itemSize = view.bounds.size
layout.minimumInteritemSpacing = 0
layout.minimumLineSpacing = 8
layout.footerReferenceSize = CGSize(width: layout.minimumLineSpacing / 2,
height: view.bounds.size.height)
layout.headerReferenceSize = layout.footerReferenceSize
layout.scrollDirection = .horizontal
collectionView = UICollectionView(frame: view.bounds, collectionViewLayout: layout)
collectionView.isPagingEnabled = true
collectionView.backgroundColor = .backgroundColor
collectionView.showsHorizontalScrollIndicator = false
collectionView.dataSource = self
collectionView.delegate = self
collectionView.register(PhotoCollectionViewCell.self,
forCellWithReuseIdentifier: photoCellIdentifier)
var extendedBounds = view.bounds
extendedBounds.size.width = extendedBounds.width + layout.minimumLineSpacing
collectionView.bounds = extendedBounds
view.addSubview(collectionView)
transitionController.disableSimultaneousRecognition(of: collectionView.panGestureRecognizer)
for gesture in [UIPanGestureRecognizer(), UIPinchGestureRecognizer(), UIRotationGestureRecognizer()] {
transitionController.dismissWhenGestureRecognizerBegins(gesture)
view.addGestureRecognizer(gesture)
}
}
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
collectionView.center = CGPoint(x: view.bounds.midX, y: view.bounds.midY)
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
navigationController?.setNavigationBarHidden(true, animated: animated)
let photoIndex = album.photos.index { $0.image == currentPhoto.image }!
let indexPath = IndexPath(item: photoIndex, section: 0)
collectionView.scrollToItem(at: indexPath, at: .centeredHorizontally, animated: false)
}
override var preferredStatusBarStyle: UIStatusBarStyle {
return .lightContent
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return album.photos.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: photoCellIdentifier,
for: indexPath) as! PhotoCollectionViewCell
let photo = album.photos[indexPath.row]
cell.imageView.image = photo.image
cell.imageView.contentMode = .scaleAspectFit
return cell
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
dismiss(animated: true)
}
func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
currentPhoto = album.photos[indexPathForCurrentPhoto().item]
}
func indexPathForCurrentPhoto() -> IndexPath {
return collectionView.indexPathsForVisibleItems.first!
}
}
private class PushBackTransition: Transition {
func willBeginTransition(withContext ctx: TransitionContext, runtime: MotionRuntime) -> [Stateful] {
let foreVC = ctx.fore as! PhotoAlbumViewController
let foreImageView = (foreVC.collectionView.cellForItem(at: foreVC.indexPathForCurrentPhoto()) as! PhotoCollectionViewCell).imageView
let contextView = ctx.contextView() as! PhotoCollectionViewCell
let replicaView = ctx.replicator.replicate(view: contextView.imageView)
let imageSize = foreImageView.image!.size
let fitScale = min(foreImageView.bounds.width / imageSize.width,
foreImageView.bounds.height / imageSize.height)
let fitSize = CGSize(width: fitScale * imageSize.width, height: fitScale * imageSize.height)
let draggable = Draggable(withFirstGestureIn: ctx.gestureRecognizers)
runtime.add(SlopRegion(withTranslationOf: draggable.nextGestureRecognizer, size: 100), to: ctx.direction)
runtime.add(ChangeDirection(withVelocityOf: draggable.nextGestureRecognizer), to: ctx.direction)
let backPosition = contextView.superview!.convert(contextView.layer.position, to: ctx.containerView())
let forePosition = foreImageView.superview!.convert(foreImageView.layer.position, to: ctx.containerView())
let movement = TransitionSpring(back: backPosition, fore: forePosition, direction: ctx.direction)
let tossable = Tossable(spring: movement, draggable: draggable)
runtime.add(tossable, to: replicaView)
let size = TransitionSpring(back: contextView.bounds.size, fore: fitSize, direction: ctx.direction)
runtime.toggle(size, inReactionTo: draggable)
runtime.add(size, to: runtime.get(replicaView).layer.size)
let opacity = TransitionSpring<CGFloat>(back: 0, fore: 1, direction: ctx.direction)
runtime.add(opacity, to: runtime.get(ctx.fore.view.layer).opacity)
runtime.add(Hidden(), to: foreImageView)
return [tossable, size, opacity]
}
}
// TODO: The need here is we want to hide a given view will the transition is active. This
// implementation does not register a stream with the runtime.
private class Hidden: Interaction {
deinit {
for view in hiddenViews {
view.isHidden = false
}
}
func add(to view: UIView, withRuntime runtime: MotionRuntime, constraints: NoConstraints) {
view.isHidden = true
hiddenViews.insert(view)
}
var hiddenViews = Set<UIView>()
}