This repository was 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
/
Copy pathStickerPickerExample.swift
209 lines (154 loc) · 6.22 KB
/
StickerPickerExample.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
/*
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 Foundation
import UIKit
import MaterialMotion
class StickerPickerExampleViewController: ExampleViewController, StickerListViewControllerDelegate {
var runtime: MotionRuntime!
override func viewDidLoad() {
super.viewDidLoad()
navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: .add, target: self, action: #selector(didTapAdd))
runtime = MotionRuntime(containerView: view)
}
fileprivate func didPickSticker(_ sticker: Sticker) {
let stickerView = UIView(frame: .init(x: 0, y: 0, width: 100, height: 100))
stickerView.backgroundColor = sticker.color
stickerView.center = .init(x: view.bounds.midX, y: view.bounds.midY)
view.addSubview(stickerView)
let direction = createProperty(withInitialValue: TransitionDirection.forward)
let spring = TransitionSpring(back: CGFloat(1.5), fore: 1, direction: direction)
runtime.add(spring, to: runtime.get(stickerView.layer).scale)
runtime.add(DirectlyManipulable(), to: stickerView)
}
func didTapAdd() {
let picker = StickerListViewController()
picker.delegate = self
present(picker, animated: true)
}
override func exampleInformation() -> ExampleInfo {
return .init(title: type(of: self).catalogBreadcrumbs().last!,
instructions: "Tap the plus to add a sticker.")
}
}
private let numberOfStickers = 3
private struct Sticker {
let color: UIColor
let uuid: String
fileprivate init(color: UIColor) {
self.color = color
self.uuid = NSUUID().uuidString
}
}
private class StickerAlbum {
let stickers = [
Sticker(color: .white),
Sticker(color: .primaryColor),
Sticker(color: .secondaryColor)
]
}
private class StickerCollectionViewCell: 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
}
}
private class StickerListViewController: UICollectionViewController {
let album = StickerAlbum()
var selectedSticker: Sticker?
weak var delegate: StickerListViewControllerDelegate?
init() {
super.init(collectionViewLayout: UICollectionViewFlowLayout())
transitionController.transition = ModalTransition()
modalPresentationStyle = .overCurrentContext
}
public required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override public func viewDidLoad() {
super.viewDidLoad()
collectionView!.backgroundColor = UIColor(white: 0, alpha: 0.25)
collectionView!.register(StickerViewCell.self,
forCellWithReuseIdentifier: stickerCellIdentifier)
}
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
layout.headerReferenceSize = .init(width: 0, height: 48)
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.stickers.count
}
public override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: stickerCellIdentifier,
for: indexPath) as! StickerViewCell
let sticker = album.stickers[indexPath.row]
cell.view.backgroundColor = sticker.color
return cell
}
public override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
selectedSticker = album.stickers[indexPath.row]
delegate?.didPickSticker(selectedSticker!)
dismiss(animated: true)
}
}
let stickerCellIdentifier = "stickerCell"
private class StickerViewCell: UICollectionViewCell {
let view = UIView()
override init(frame: CGRect) {
super.init(frame: frame)
view.frame = bounds.insetBy(dx: 8, dy: 8)
contentView.addSubview(view)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func layoutSubviews() {
super.layoutSubviews()
view.frame = bounds.insetBy(dx: 8, dy: 8)
}
}
private protocol StickerListViewControllerDelegate: NSObjectProtocol {
func didPickSticker(_ sticker: Sticker)
}
private class ModalTransition: Transition {
required init() {}
func willBeginTransition(withContext ctx: TransitionContext, runtime: MotionRuntime) -> [Stateful] {
let size = ctx.fore.preferredContentSize == .zero() ? ctx.fore.view.bounds.size : ctx.fore.preferredContentSize
if ctx.direction == .forward {
ctx.fore.view.bounds = CGRect(origin: .zero, size: size)
}
let spring = TransitionSpring<CGFloat>(back: 0, fore: 1, direction: ctx.direction)
runtime.add(spring, to: runtime.get(ctx.fore.view.layer).opacity)
return [spring]
}
}