-
-
Notifications
You must be signed in to change notification settings - Fork 127
Expand file tree
/
Copy pathUIKitShowcaseViewController.swift
More file actions
468 lines (404 loc) · 19 KB
/
UIKitShowcaseViewController.swift
File metadata and controls
468 lines (404 loc) · 19 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
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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
import UIKit
import DSWaveformImage
import DSWaveformImageViews
/// Modernized, card-based UIKit showcase. Mirrors the visual language of the SwiftUI gallery so
/// the two reference implementations feel like siblings, while staying entirely UIKit.
final class UIKitShowcaseViewController: UIViewController {
private let waveformImageDrawer = WaveformImageDrawer()
private let audioManager = SCAudioManager()
private let audioURL = Bundle.main.url(forResource: "example_stereo", withExtension: "m4a")!
// Static section
private let circularStaticView = UIImageView()
private let linearStaticView = WaveformImageView(frame: .zero)
private let blendedStaticView = UIImageView()
private let blendedBackgroundView = UIImageView()
// Live recording
private let liveWaveformView = WaveformLiveView(frame: .zero)
private let recordButton = UIButton(type: .system)
// Progress
private let progressBaseImageView = UIImageView()
private let progressOverlayImageView = UIImageView()
private var progress: Double = 0.4
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .systemBackground
title = "UIKit"
setupLayout()
audioManager.recordingDelegate = self
audioManager.prepareAudioRecording()
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
renderStaticImages()
renderProgressImages()
}
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
updateProgressMask()
}
// MARK: - Layout
private func setupLayout() {
let scrollView = UIScrollView()
scrollView.translatesAutoresizingMaskIntoConstraints = false
scrollView.alwaysBounceVertical = true
view.addSubview(scrollView)
let contentStack = UIStackView()
contentStack.translatesAutoresizingMaskIntoConstraints = false
contentStack.axis = .vertical
contentStack.spacing = 28
contentStack.alignment = .fill
scrollView.addSubview(contentStack)
contentStack.addArrangedSubview(makeHero())
contentStack.addArrangedSubview(makeStaticSection())
contentStack.addArrangedSubview(makeLiveRecordingSection())
contentStack.addArrangedSubview(makeProgressSection())
NSLayoutConstraint.activate([
scrollView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor),
scrollView.leadingAnchor.constraint(equalTo: view.leadingAnchor),
scrollView.trailingAnchor.constraint(equalTo: view.trailingAnchor),
scrollView.bottomAnchor.constraint(equalTo: view.bottomAnchor),
contentStack.topAnchor.constraint(equalTo: scrollView.contentLayoutGuide.topAnchor, constant: 16),
contentStack.bottomAnchor.constraint(equalTo: scrollView.contentLayoutGuide.bottomAnchor, constant: -32),
contentStack.leadingAnchor.constraint(equalTo: scrollView.contentLayoutGuide.leadingAnchor, constant: 20),
contentStack.trailingAnchor.constraint(equalTo: scrollView.contentLayoutGuide.trailingAnchor, constant: -20),
contentStack.widthAnchor.constraint(equalTo: scrollView.frameLayoutGuide.widthAnchor, constant: -40)
])
}
private func makeHero() -> UIView {
let title = UILabel()
title.text = "DSWaveformImage"
title.font = .systemFont(ofSize: 34, weight: .bold)
title.numberOfLines = 0
let subtitle = UILabel()
subtitle.text = "Same library, pure UIKit composition — drawers, views, and live capture."
subtitle.font = .preferredFont(forTextStyle: .callout)
subtitle.textColor = .secondaryLabel
subtitle.numberOfLines = 0
let stack = UIStackView(arrangedSubviews: [title, subtitle])
stack.axis = .vertical
stack.spacing = 8
return stack
}
private func makeStaticSection() -> UIView {
let header = makeSectionHeader(
title: "Static rendering",
systemImage: "waveform",
subtitle: "Drop a Configuration and a renderer into WaveformImageDrawer — same audio, three looks."
)
circularStaticView.contentMode = .scaleToFill
let circular = makeCard(
title: "Circular · gradient damping",
caption: "CircularWaveformRenderer() + .gradient([…])",
content: aspectRatio(circularStaticView, 1)
)
linearStaticView.contentMode = .scaleToFill
let linear = makeCard(
title: "Linear · striped",
caption: "WaveformImageView with .striped(.init(color: …))",
content: aspectRatio(linearStaticView, 16.0/9.0)
)
blendedBackgroundView.image = UIImage(named: "background")
blendedBackgroundView.contentMode = .scaleAspectFill
blendedBackgroundView.clipsToBounds = true
blendedStaticView.contentMode = .scaleToFill
blendedStaticView.layer.compositingFilter = "overlayBlendMode"
let blendContainer = UIView()
blendContainer.translatesAutoresizingMaskIntoConstraints = false
blendContainer.clipsToBounds = true
blendContainer.layer.cornerRadius = 10
blendContainer.layer.cornerCurve = .continuous
[blendedBackgroundView, blendedStaticView].forEach {
$0.translatesAutoresizingMaskIntoConstraints = false
blendContainer.addSubview($0)
}
NSLayoutConstraint.activate([
blendedBackgroundView.topAnchor.constraint(equalTo: blendContainer.topAnchor),
blendedBackgroundView.leadingAnchor.constraint(equalTo: blendContainer.leadingAnchor),
blendedBackgroundView.trailingAnchor.constraint(equalTo: blendContainer.trailingAnchor),
blendedBackgroundView.bottomAnchor.constraint(equalTo: blendContainer.bottomAnchor),
blendedStaticView.topAnchor.constraint(equalTo: blendContainer.topAnchor),
blendedStaticView.leadingAnchor.constraint(equalTo: blendContainer.leadingAnchor),
blendedStaticView.trailingAnchor.constraint(equalTo: blendContainer.trailingAnchor),
blendedStaticView.bottomAnchor.constraint(equalTo: blendContainer.bottomAnchor),
blendContainer.heightAnchor.constraint(equalTo: blendContainer.widthAnchor, multiplier: 9.0/16.0)
])
let blended = makeCard(
title: "Composited on backdrop",
caption: "layer.compositingFilter = \"overlayBlendMode\"",
content: blendContainer
)
return makeSection(header: header, cards: [circular, linear, blended])
}
private func makeLiveRecordingSection() -> UIView {
let header = makeSectionHeader(
title: "Live recording",
systemImage: "mic.circle",
subtitle: "WaveformLiveView ingests microphone samples — the heavy lifting is one .add(samples:) call."
)
liveWaveformView.configuration = .init(
style: .striped(.init(color: .systemIndigo, width: 3, spacing: 3)),
damping: .init(percentage: 0.125, sides: .both)
)
liveWaveformView.translatesAutoresizingMaskIntoConstraints = false
liveWaveformView.backgroundColor = .clear
liveWaveformView.heightAnchor.constraint(equalToConstant: 120).isActive = true
var buttonConfig = UIButton.Configuration.borderedProminent()
buttonConfig.title = "Start Recording"
buttonConfig.image = UIImage(systemName: "record.circle")
buttonConfig.imagePadding = 6
buttonConfig.baseBackgroundColor = .systemRed
recordButton.configuration = buttonConfig
recordButton.addTarget(self, action: #selector(didTapRecording), for: .touchUpInside)
let stack = UIStackView(arrangedSubviews: [liveWaveformView, recordButton])
stack.axis = .vertical
stack.spacing = 14
stack.alignment = .center
// Stretch the waveform full width inside the card's content column.
liveWaveformView.setContentHuggingPriority(.defaultLow, for: .horizontal)
liveWaveformView.widthAnchor.constraint(equalTo: stack.widthAnchor).isActive = true
return makeSection(
header: header,
cards: [makeCard(
title: nil,
caption: "WaveformLiveView().add(samples: [Float])",
content: stack
)]
)
}
private func makeProgressSection() -> UIView {
let header = makeSectionHeader(
title: "Progress",
systemImage: "play.circle.fill",
subtitle: "Render the waveform once, then mask a tinted copy with a layer mask that tracks playback."
)
let stackedView = UIView()
stackedView.translatesAutoresizingMaskIntoConstraints = false
[progressBaseImageView, progressOverlayImageView].forEach {
$0.translatesAutoresizingMaskIntoConstraints = false
$0.contentMode = .scaleAspectFit
stackedView.addSubview($0)
}
NSLayoutConstraint.activate([
progressBaseImageView.topAnchor.constraint(equalTo: stackedView.topAnchor),
progressBaseImageView.leadingAnchor.constraint(equalTo: stackedView.leadingAnchor),
progressBaseImageView.trailingAnchor.constraint(equalTo: stackedView.trailingAnchor),
progressBaseImageView.bottomAnchor.constraint(equalTo: stackedView.bottomAnchor),
progressOverlayImageView.topAnchor.constraint(equalTo: stackedView.topAnchor),
progressOverlayImageView.leadingAnchor.constraint(equalTo: stackedView.leadingAnchor),
progressOverlayImageView.trailingAnchor.constraint(equalTo: stackedView.trailingAnchor),
progressOverlayImageView.bottomAnchor.constraint(equalTo: stackedView.bottomAnchor),
stackedView.heightAnchor.constraint(equalToConstant: 110)
])
var shuffleConfig = UIButton.Configuration.tinted()
shuffleConfig.title = "Shuffle progress"
shuffleConfig.image = UIImage(systemName: "dice.fill")
shuffleConfig.imagePadding = 6
let shuffleButton = UIButton(configuration: shuffleConfig, primaryAction: UIAction { [weak self] _ in
self?.shuffleProgress()
})
let stack = UIStackView(arrangedSubviews: [stackedView, shuffleButton])
stack.axis = .vertical
stack.spacing = 14
stack.alignment = .center
stackedView.widthAnchor.constraint(equalTo: stack.widthAnchor).isActive = true
return makeSection(
header: header,
cards: [makeCard(
title: nil,
caption: "imageView.layer.mask = CAShapeLayer(rect: …, width: w * progress)",
content: stack
)]
)
}
// MARK: - Card / Section primitives
private func makeSection(header: UIView, cards: [UIView]) -> UIStackView {
let stack = UIStackView(arrangedSubviews: [header] + cards)
stack.axis = .vertical
stack.spacing = 12
stack.alignment = .fill
return stack
}
private func makeSectionHeader(title: String, systemImage: String, subtitle: String) -> UIView {
let icon = UIImageView(image: UIImage(systemName: systemImage))
icon.tintColor = .tintColor
icon.translatesAutoresizingMaskIntoConstraints = false
icon.setContentHuggingPriority(.required, for: .horizontal)
let titleLabel = UILabel()
titleLabel.text = title
titleLabel.font = .systemFont(ofSize: 22, weight: .semibold)
titleLabel.numberOfLines = 0
let titleRow = UIStackView(arrangedSubviews: [icon, titleLabel])
titleRow.axis = .horizontal
titleRow.spacing = 8
titleRow.alignment = .firstBaseline
let subtitleLabel = UILabel()
subtitleLabel.text = subtitle
subtitleLabel.font = .preferredFont(forTextStyle: .callout)
subtitleLabel.textColor = .secondaryLabel
subtitleLabel.numberOfLines = 0
let stack = UIStackView(arrangedSubviews: [titleRow, subtitleLabel])
stack.axis = .vertical
stack.spacing = 4
stack.alignment = .fill
stack.setCustomSpacing(2, after: titleRow)
return stack
}
private func makeCard(title: String?, caption: String?, content: UIView) -> UIView {
let card = UIView()
card.backgroundColor = .secondarySystemBackground
card.layer.cornerRadius = 16
card.layer.cornerCurve = .continuous
card.layer.borderWidth = 0.5
card.layer.borderColor = UIColor.systemGray.withAlphaComponent(0.3).cgColor
let inner = UIStackView()
inner.translatesAutoresizingMaskIntoConstraints = false
inner.axis = .vertical
inner.spacing = 12
inner.alignment = .fill
if title != nil || caption != nil {
let header = UIStackView()
header.axis = .vertical
header.spacing = 2
header.alignment = .leading
if let title {
let titleLabel = UILabel()
titleLabel.text = title
titleLabel.font = .systemFont(ofSize: 15, weight: .semibold)
titleLabel.numberOfLines = 0
header.addArrangedSubview(titleLabel)
}
if let caption {
let captionLabel = UILabel()
captionLabel.text = caption
captionLabel.font = UIFont.monospacedSystemFont(ofSize: 12, weight: .regular)
captionLabel.textColor = .secondaryLabel
captionLabel.numberOfLines = 0
header.addArrangedSubview(captionLabel)
}
inner.addArrangedSubview(header)
}
content.translatesAutoresizingMaskIntoConstraints = false
inner.addArrangedSubview(content)
card.addSubview(inner)
NSLayoutConstraint.activate([
inner.topAnchor.constraint(equalTo: card.topAnchor, constant: 16),
inner.leadingAnchor.constraint(equalTo: card.leadingAnchor, constant: 16),
inner.trailingAnchor.constraint(equalTo: card.trailingAnchor, constant: -16),
inner.bottomAnchor.constraint(equalTo: card.bottomAnchor, constant: -16)
])
return card
}
private func aspectRatio(_ view: UIView, _ ratio: CGFloat) -> UIView {
let wrapper = UIView()
view.translatesAutoresizingMaskIntoConstraints = false
wrapper.addSubview(view)
NSLayoutConstraint.activate([
view.topAnchor.constraint(equalTo: wrapper.topAnchor),
view.leadingAnchor.constraint(equalTo: wrapper.leadingAnchor),
view.trailingAnchor.constraint(equalTo: wrapper.trailingAnchor),
view.bottomAnchor.constraint(equalTo: wrapper.bottomAnchor),
wrapper.heightAnchor.constraint(equalTo: wrapper.widthAnchor, multiplier: 1.0 / ratio)
])
return wrapper
}
// MARK: - Static rendering
private func renderStaticImages() {
guard circularStaticView.bounds.size != .zero else { return }
renderCircularStatic()
renderBlendedStatic()
renderLinearStatic()
}
private func renderCircularStatic() {
Task {
let image = try? await waveformImageDrawer.waveformImage(
fromAudioAt: audioURL,
with: .init(
size: circularStaticView.bounds.size,
style: .gradient([
UIColor(red: 255/255, green: 159/255, blue: 28/255, alpha: 1),
UIColor(red: 255/255, green: 191/255, blue: 105/255, alpha: 1),
.red
]),
damping: .init(percentage: 0.2, sides: .right, easing: { x in pow(x, 4) })
),
renderer: CircularWaveformRenderer()
)
await MainActor.run { self.circularStaticView.image = image }
}
}
private func renderLinearStatic() {
linearStaticView.configuration = .init(
backgroundColor: .clear,
style: .striped(.init(color: UIColor(red: 51/255, green: 92/255, blue: 103/255, alpha: 1), width: 5, spacing: 5)),
verticalScalingFactor: 0.5
)
linearStaticView.waveformAudioURL = audioURL
}
private func renderBlendedStatic() {
Task {
let image = try? await waveformImageDrawer.waveformImage(
fromAudioAt: audioURL,
with: .init(size: blendedStaticView.bounds.size, style: .filled(.black)),
position: .top
)
await MainActor.run { self.blendedStaticView.image = image }
}
}
// MARK: - Live recording
@objc private func didTapRecording() {
if audioManager.recording() {
audioManager.stopRecording()
} else {
liveWaveformView.reset()
audioManager.startRecording()
}
updateRecordButtonTitle()
}
private func updateRecordButtonTitle() {
var config = recordButton.configuration
let isRecording = audioManager.recording()
config?.title = isRecording ? "Stop Recording" : "Start Recording"
config?.image = UIImage(systemName: isRecording ? "stop.circle.fill" : "record.circle")
recordButton.configuration = config
}
// MARK: - Progress
private func renderProgressImages() {
guard progressBaseImageView.bounds.size != .zero else { return }
Task {
let image = try? await waveformImageDrawer.waveformImage(
fromAudioAt: audioURL,
with: .init(size: progressBaseImageView.bounds.size, style: .filled(.systemGray3))
)
await MainActor.run {
self.progressBaseImageView.image = image
self.progressOverlayImageView.image = image?.withTintColor(.systemIndigo, renderingMode: .alwaysTemplate)
self.updateProgressMask()
}
}
}
private func shuffleProgress() {
progress = .random(in: 0...1)
updateProgressMask()
}
private func updateProgressMask() {
let bounds = progressOverlayImageView.bounds
guard bounds.width > 0 else { return }
let maskLayer = CAShapeLayer()
maskLayer.path = CGPath(rect: CGRect(x: 0, y: 0, width: bounds.width * progress, height: bounds.height), transform: nil)
progressOverlayImageView.layer.mask = maskLayer
}
}
extension UIKitShowcaseViewController: RecordingDelegate {
func audioManager(_ manager: SCAudioManager!, didAllowRecording success: Bool) {
if !success {
preconditionFailure("Recording must be allowed in Settings to work.")
}
}
func audioManager(_ manager: SCAudioManager!, didFinishRecordingSuccessfully success: Bool) {
updateRecordButtonTitle()
}
func audioManager(_ manager: SCAudioManager!, didUpdateRecordProgress progress: CGFloat) {
let linear = 1 - pow(10, manager.lastAveragePower() / 20)
liveWaveformView.add(samples: [Float(linear), Float(linear), Float(linear)])
}
}