-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathSTBaseViewController.swift
More file actions
531 lines (476 loc) · 23.5 KB
/
Copy pathSTBaseViewController.swift
File metadata and controls
531 lines (476 loc) · 23.5 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
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
//
// STBaseViewController.swift
// STBaseProject
//
// Created by 寒江孤影 on 2018/3/14.
//
import Combine
import UIKit
public enum STNavBtnShowType {
case none
case showBothBtn
case showLeftBtn
case showRightBtn
case onlyShowTitle
}
/// 默认导航栏按钮与标题布局数值;与 `STBaseViewController` 内置约束实现配套。
/// 若子类重写 `st_leftButtonConstraints` / `st_rightButtonConstraints` / `st_titleLabelConstraints`,请同步调整标题最大宽度等约束。
public enum STDefaultNavigationBarItemMetrics {
public static let horizontalInset: CGFloat = 8
public static let buttonMinTouchSize: CGFloat = 44
/// 按钮与标题之间的默认间距(与 `horizontalInset` 一致)
public static let titleToButtonSpacing: CGFloat = horizontalInset
/// 单侧为标题预留的横向空间:边距 + 最小触摸宽度
public static var reservedWidthPerSide: CGFloat { horizontalInset + buttonMinTouchSize }
/// `titleLabel.width <= container.width + constant` 中的 constant(两侧各 `reservedWidthPerSide`)
public static var titleMaxWidthLayoutConstant: CGFloat { -2 * reservedWidthPerSide }
}
open class STBaseViewController: UIViewController {
public private(set) var titleLabel = STLabel()
public private(set) var leftBtn = STIconBtn(type: .custom)
public private(set) var rightBtn = STIconBtn(type: .custom)
public private(set) var navigationBarView = UIView()
public private(set) var navigationBarItemsView = UIView()
public private(set) var navGradientBar: STGradientNavigationBar?
public var navBarBackgroundColor: UIColor = .white {
didSet {
if self.liquidGlassContainerView == nil {
self.navigationBarView.backgroundColor = self.navBarBackgroundColor
}
}
}
public var navBarTitleColor: UIColor = .black {
didSet { self.titleLabel.textColor = self.navBarTitleColor }
}
public var buttonTitleColor: UIColor = .systemBlue {
didSet {
self.leftBtn.setTitleColor(self.buttonTitleColor, for: .normal)
self.rightBtn.setTitleColor(self.buttonTitleColor, for: .normal)
}
}
public var buttonTitleFont: UIFont = .st_systemFont(ofSize: 16) {
didSet {
self.leftBtn.titleLabel?.font = self.buttonTitleFont
self.rightBtn.titleLabel?.font = self.buttonTitleFont
}
}
public var navBarTitleFont: UIFont = .st_boldSystemFont(ofSize: 20) {
didSet { self.titleLabel.font = self.navBarTitleFont }
}
public var navBarHeight: CGFloat { STDeviceAdapter.navigationBarHeight }
public var leftBtnImage: UIImage? {
didSet {
self.leftBtn.setImage(self.leftBtnImage, for: .normal)
}
}
public var rightBtnImage: UIImage? {
didSet {
self.rightBtn.setImage(self.rightBtnImage, for: .normal)
}
}
public var leftBtnTitle: String? {
didSet {
self.leftBtn.setTitle(self.leftBtnTitle, for: .normal)
}
}
public var rightBtnTitle: String? {
didSet {
self.rightBtn.setTitle(self.rightBtnTitle, for: .normal)
}
}
public var statusBarHidden: Bool = false
public var statusBarStyle: UIStatusBarStyle = .default
open var prefersSystemNavigationBarHidden: Bool { true }
public var contentTopAnchor: NSLayoutYAxisAnchor { self.navigationBarView.bottomAnchor }
private var appearanceCancellable: AnyCancellable?
private var contentOffsetObservation: NSKeyValueObservation?
private var lastAppliedInterfaceStyle: UIUserInterfaceStyle?
public var leftBtnConstraints: [NSLayoutConstraint] = [] {
didSet {
NSLayoutConstraint.deactivate(oldValue)
NSLayoutConstraint.activate(self.leftBtnConstraints)
}
}
public var rightBtnConstraints: [NSLayoutConstraint] = [] {
didSet {
NSLayoutConstraint.deactivate(oldValue)
NSLayoutConstraint.activate(self.rightBtnConstraints)
}
}
public var titleLabelConstraints: [NSLayoutConstraint] = [] {
didSet {
NSLayoutConstraint.deactivate(oldValue)
NSLayoutConstraint.activate(self.titleLabelConstraints)
}
}
open override func viewDidLoad() {
super.viewDidLoad()
self.setupNavigationBar()
self.setupAppearanceObservation()
self.setupLocalizationObservation()
self.applyDefaultNavigationBarStyle()
self.st_updateLocalizedTexts()
}
open override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
if let nav = self.navigationController,
nav.isNavigationBarHidden != self.prefersSystemNavigationBarHidden {
nav.setNavigationBarHidden(self.prefersSystemNavigationBarHidden, animated: animated)
}
}
open override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
if self.view.subviews.last !== self.navigationBarView {
self.view.bringSubviewToFront(self.navigationBarView)
}
}
deinit {
self.contentOffsetObservation?.invalidate()
NotificationCenter.default.removeObserver(self, name: .stLanguageDidChange, object: nil)
}
private func setupNavigationBar() {
self.navigationBarView.translatesAutoresizingMaskIntoConstraints = false
self.navigationBarItemsView.translatesAutoresizingMaskIntoConstraints = false
self.titleLabel.translatesAutoresizingMaskIntoConstraints = false
self.leftBtn.translatesAutoresizingMaskIntoConstraints = false
self.rightBtn.translatesAutoresizingMaskIntoConstraints = false
self.view.addSubview(self.navigationBarView)
self.navigationBarView.addSubview(self.navigationBarItemsView)
NSLayoutConstraint.activate([
self.navigationBarView.topAnchor.constraint(equalTo: self.view.topAnchor),
self.navigationBarView.leadingAnchor.constraint(equalTo: self.view.leadingAnchor),
self.navigationBarView.trailingAnchor.constraint(equalTo: self.view.trailingAnchor),
self.navigationBarView.bottomAnchor.constraint(equalTo: self.navigationBarItemsView.bottomAnchor)
])
NSLayoutConstraint.activate([
self.navigationBarItemsView.topAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.topAnchor),
self.navigationBarItemsView.heightAnchor.constraint(equalToConstant: STDeviceAdapter.navigationBarContentHeight),
self.navigationBarItemsView.leadingAnchor.constraint(equalTo: self.navigationBarView.leadingAnchor),
self.navigationBarItemsView.trailingAnchor.constraint(equalTo: self.navigationBarView.trailingAnchor)
])
self.navigationBarItemsView.addSubview(self.titleLabel)
self.configureDefaultTitleLabelAppearance()
self.titleLabelConstraints = self.st_titleLabelConstraints(in: self.navigationBarItemsView)
self.navigationBarItemsView.addSubview(self.leftBtn)
self.leftBtnConstraints = self.st_leftButtonConstraints(in: self.navigationBarItemsView)
self.navigationBarItemsView.addSubview(self.rightBtn)
self.rightBtnConstraints = self.st_rightButtonConstraints(in: self.navigationBarItemsView)
self.leftBtn.addTarget(self, action: #selector(self.onLeftBtnTap), for: .touchUpInside)
self.rightBtn.addTarget(self, action: #selector(self.onRightBtnTap), for: .touchUpInside)
self.view.bringSubviewToFront(self.navigationBarView)
}
private func configureDefaultTitleLabelAppearance() {
self.titleLabel.numberOfLines = 1
self.titleLabel.lineBreakMode = .byTruncatingTail
self.titleLabel.textAlignment = .center
self.titleLabel.setContentCompressionResistancePriority(.defaultLow, for: .horizontal)
self.titleLabel.setContentHuggingPriority(.defaultLow, for: .horizontal)
}
/// 子类重写此方法以自定义左按钮约束
open func st_leftButtonConstraints(in container: UIView) -> [NSLayoutConstraint] {
let inset = STDefaultNavigationBarItemMetrics.horizontalInset
let minSize = STDefaultNavigationBarItemMetrics.buttonMinTouchSize
let gap = STDefaultNavigationBarItemMetrics.titleToButtonSpacing
let avoidTitleOverlap = self.leftBtn.trailingAnchor.constraint(lessThanOrEqualTo: self.titleLabel.leadingAnchor, constant: -gap)
avoidTitleOverlap.priority = .defaultHigh
return [
self.leftBtn.leadingAnchor.constraint(equalTo: container.leadingAnchor, constant: inset),
self.leftBtn.centerYAnchor.constraint(equalTo: container.centerYAnchor),
self.leftBtn.widthAnchor.constraint(greaterThanOrEqualToConstant: minSize),
self.leftBtn.heightAnchor.constraint(equalToConstant: minSize),
avoidTitleOverlap
]
}
/// 子类重写此方法以自定义右按钮约束
open func st_rightButtonConstraints(in container: UIView) -> [NSLayoutConstraint] {
let inset = STDefaultNavigationBarItemMetrics.horizontalInset
let minSize = STDefaultNavigationBarItemMetrics.buttonMinTouchSize
let gap = STDefaultNavigationBarItemMetrics.titleToButtonSpacing
let avoidTitleOverlap = self.rightBtn.leadingAnchor.constraint(greaterThanOrEqualTo: self.titleLabel.trailingAnchor, constant: gap)
avoidTitleOverlap.priority = .defaultHigh
return [
self.rightBtn.trailingAnchor.constraint(equalTo: container.trailingAnchor, constant: -inset),
self.rightBtn.centerYAnchor.constraint(equalTo: container.centerYAnchor),
self.rightBtn.widthAnchor.constraint(greaterThanOrEqualToConstant: minSize),
self.rightBtn.heightAnchor.constraint(equalToConstant: minSize),
avoidTitleOverlap
]
}
/// 子类重写此方法以自定义标题约束
open func st_titleLabelConstraints(in container: UIView) -> [NSLayoutConstraint] {
return [
self.titleLabel.centerYAnchor.constraint(equalTo: container.centerYAnchor),
self.titleLabel.centerXAnchor.constraint(equalTo: container.centerXAnchor),
self.titleLabel.widthAnchor.constraint(
lessThanOrEqualTo: container.widthAnchor,
constant: STDefaultNavigationBarItemMetrics.titleMaxWidthLayoutConstant
)
]
}
/// 替换左按钮约束;自动停用旧约束、启用新约束
@discardableResult
public func st_replaceLeftButtonConstraints(_ builder: (UIView) -> [NSLayoutConstraint]) -> Self {
self.leftBtnConstraints = builder(self.navigationBarItemsView)
return self
}
/// 替换右按钮约束;自动停用旧约束、启用新约束
@discardableResult
public func st_replaceRightButtonConstraints(_ builder: (UIView) -> [NSLayoutConstraint]) -> Self {
self.rightBtnConstraints = builder(self.navigationBarItemsView)
return self
}
/// 替换标题约束;自动停用旧约束、启用新约束
@discardableResult
public func st_replaceTitleLabelConstraints(_ builder: (UIView) -> [NSLayoutConstraint]) -> Self {
self.titleLabelConstraints = builder(self.navigationBarItemsView)
return self
}
private func applyDefaultNavigationBarStyle() {
if self.liquidGlassContainerView == nil {
self.navigationBarView.backgroundColor = self.navBarBackgroundColor
}
self.titleLabel.textColor = self.navBarTitleColor
self.titleLabel.font = self.navBarTitleFont
self.leftBtn.titleLabel?.font = self.buttonTitleFont
self.rightBtn.titleLabel?.font = self.buttonTitleFont
self.leftBtn.setTitleColor(self.buttonTitleColor, for: .normal)
self.rightBtn.setTitleColor(self.buttonTitleColor, for: .normal)
}
private func setupLocalizationObservation() {
NotificationCenter.default.addObserver(
self,
selector: #selector(self.st_updateLocalizedTexts),
name: .stLanguageDidChange,
object: nil
)
}
private func setupAppearanceObservation() {
self.st_refreshAppearance(animated: false)
self.appearanceCancellable = STAppearanceManager.shared.appearanceModePublisher
.dropFirst()
.receive(on: RunLoop.main)
.sink { [weak self] _ in
self?.st_refreshAppearance(animated: true)
}
}
/// 外观变化回调,子类重写以自定义颜色处理逻辑
open func st_appearanceDidChange(resolvedStyle: UIUserInterfaceStyle) {}
open override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {
super.traitCollectionDidChange(previousTraitCollection)
guard STAppearanceManager.shared.currentMode == .system else { return }
let previousStyle = previousTraitCollection?.userInterfaceStyle ?? .unspecified
let currentStyle = self.traitCollection.userInterfaceStyle
if previousStyle != currentStyle && previousStyle != .unspecified {
self.st_refreshAppearance(animated: true)
}
}
private func st_refreshAppearance(animated: Bool = false) {
let style = STAppearanceManager.shared.resolvedInterfaceStyle(for: self.traitCollection)
let targetOverride: UIUserInterfaceStyle
switch STAppearanceManager.shared.currentMode {
case .system: targetOverride = .unspecified
case .light: targetOverride = .light
case .dark: targetOverride = .dark
}
if self.overrideUserInterfaceStyle != targetOverride {
self.overrideUserInterfaceStyle = targetOverride
}
let resolvedStyle: UIUserInterfaceStyle = style == .unspecified ? .light : style
if self.lastAppliedInterfaceStyle == resolvedStyle { return }
self.lastAppliedInterfaceStyle = resolvedStyle
let applyBlock = { [weak self] in
guard let strongSelf = self else { return }
strongSelf.st_appearanceDidChange(resolvedStyle: resolvedStyle)
}
if animated {
UIView.transition(
with: self.view, duration: 0.25,
options: [.transitionCrossDissolve, .allowUserInteraction],
animations: applyBlock,
completion: nil)
} else {
applyBlock()
}
}
public func st_showNavBtnType(type: STNavBtnShowType) {
let hideBar = (type == .none)
self.navigationBarView.isHidden = hideBar
self.navigationBarItemsView.isHidden = hideBar
switch type {
case .showLeftBtn:
self.leftBtn.isHidden = false
self.rightBtn.isHidden = true
case .showRightBtn:
self.leftBtn.isHidden = true
self.rightBtn.isHidden = false
case .showBothBtn:
self.leftBtn.isHidden = false
self.rightBtn.isHidden = false
case .onlyShowTitle, .none:
self.leftBtn.isHidden = true
self.rightBtn.isHidden = true
}
}
@discardableResult
open func st_setTitle(_ text: String) -> Self {
self.titleLabel.text = text
return self
}
@discardableResult
open func st_setNavigationBarColor(_ color: UIColor) -> Self {
self.navBarBackgroundColor = color
return self
}
@discardableResult
open func st_setLeftBtn(image: UIImage? = nil, title: String? = nil) -> Self {
if let image { self.leftBtnImage = image }
if let title { self.leftBtnTitle = title }
return self
}
@discardableResult
open func st_setRightBtn(image: UIImage? = nil, title: String? = nil) -> Self {
if let image { self.rightBtnImage = image }
if let title { self.rightBtnTitle = title }
return self
}
@discardableResult
open func st_enableGradientNavigationBar(startColor: UIColor, endColor: UIColor) -> Self {
self.navGradientBar?.removeFromSuperview()
let gradient = STGradientNavigationBar()
gradient.startColor = startColor
gradient.endColor = endColor
gradient.translatesAutoresizingMaskIntoConstraints = false
// 始终插在 gradient 层次最底部(液态玻璃容器若存在则在其下方),并在玻璃启用时默认隐藏,
// 与 st_enableLiquidGlass() 的语义保持一致:"Liquid Glass 优先,Gradient 让位"
self.navigationBarView.insertSubview(gradient, at: 0)
if self.liquidGlassContainerView != nil {
gradient.isHidden = true
}
NSLayoutConstraint.activate([
gradient.topAnchor.constraint(equalTo: self.navigationBarView.topAnchor),
gradient.bottomAnchor.constraint(equalTo: self.navigationBarView.bottomAnchor),
gradient.leadingAnchor.constraint(equalTo: self.navigationBarView.leadingAnchor),
gradient.trailingAnchor.constraint(equalTo: self.navigationBarView.trailingAnchor)
])
self.navGradientBar = gradient
return self
}
@discardableResult
open func st_linkScrollAlpha(_ scrollView: UIScrollView, threshold: CGFloat = 120) -> Self {
self.contentOffsetObservation?.invalidate()
let clampedThreshold = max(1, threshold)
self.contentOffsetObservation = scrollView.observe(
\.contentOffset, options: [.new, .initial],
changeHandler: { [weak self] scroll, _ in
guard let self = self else { return }
let alpha = max(0, min(1, scroll.contentOffset.y / clampedThreshold))
self.navigationBarView.alpha = alpha
self.navGradientBar?.alpha = alpha
})
return self
}
@objc open func onLeftBtnTap() {
if let nav = self.navigationController, nav.viewControllers.count > 1 {
nav.popViewController(animated: true)
} else {
self.dismiss(animated: true)
}
}
@objc open func onRightBtnTap() {}
open override var preferredStatusBarStyle: UIStatusBarStyle { self.statusBarStyle }
open override var prefersStatusBarHidden: Bool { self.statusBarHidden }
}
extension STBaseViewController {
public static func st_setAppearanceMode(_ mode: STAppearanceMode, animated: Bool = true) {
STAppearanceManager.shared.apply(mode: mode)
}
public static func st_getCurrentAppearanceMode() -> STAppearanceMode {
STAppearanceManager.shared.currentMode
}
}
// MARK: - Liquid Glass
extension STBaseViewController {
private static var liquidGlassKey: UInt8 = 0
fileprivate var liquidGlassContainerView: UIView? {
get { objc_getAssociatedObject(self, &Self.liquidGlassKey) as? UIView }
set { objc_setAssociatedObject(self, &Self.liquidGlassKey, newValue, .OBJC_ASSOCIATION_RETAIN_NONATOMIC) }
}
public var isLiquidGlassEnabled: Bool {
if #available(iOS 26.0, *) { return self.liquidGlassContainerView != nil }
return false
}
@discardableResult
public func st_enableLiquidGlass() -> Self {
guard #available(iOS 26.0, *) else { return self }
guard self.liquidGlassContainerView == nil else { return self }
let container = UIVisualEffectView(effect: STGlassEffectFactory.makeVisualEffect())
container.translatesAutoresizingMaskIntoConstraints = false
self.navigationBarView.insertSubview(container, at: 0)
NSLayoutConstraint.activate([
container.topAnchor.constraint(equalTo: self.navigationBarView.topAnchor),
container.bottomAnchor.constraint(equalTo: self.navigationBarView.bottomAnchor),
container.leadingAnchor.constraint(equalTo: self.navigationBarView.leadingAnchor),
container.trailingAnchor.constraint(equalTo: self.navigationBarView.trailingAnchor)
])
// 把标题/按钮迁入 effect 的 contentView,让系统玻璃对其内容做 vibrancy 自适应。
// removeFromSuperview 会一并销毁 items view 的所有约束(含决定 navigationBarView 高度的 bottom 约束),必须重建。
self.navigationBarItemsView.removeFromSuperview()
container.contentView.addSubview(self.navigationBarItemsView)
NSLayoutConstraint.activate([
self.navigationBarView.bottomAnchor.constraint(equalTo: self.navigationBarItemsView.bottomAnchor),
self.navigationBarItemsView.topAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.topAnchor),
self.navigationBarItemsView.heightAnchor.constraint(equalToConstant: STDeviceAdapter.navigationBarContentHeight),
self.navigationBarItemsView.leadingAnchor.constraint(equalTo: container.contentView.leadingAnchor),
self.navigationBarItemsView.trailingAnchor.constraint(equalTo: container.contentView.trailingAnchor)
])
self.liquidGlassContainerView = container
self.navigationBarView.backgroundColor = .clear
self.navGradientBar?.isHidden = true
return self
}
public func st_disableLiquidGlass() {
guard self.liquidGlassContainerView != nil else { return }
// 迁回 navigationBarView 并重建完整约束(含决定 nav 栏高度的 bottom 约束)
self.navigationBarItemsView.removeFromSuperview()
self.navigationBarView.addSubview(self.navigationBarItemsView)
NSLayoutConstraint.activate([
self.navigationBarView.bottomAnchor.constraint(equalTo: self.navigationBarItemsView.bottomAnchor),
self.navigationBarItemsView.topAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.topAnchor),
self.navigationBarItemsView.heightAnchor.constraint(equalToConstant: STDeviceAdapter.navigationBarContentHeight),
self.navigationBarItemsView.leadingAnchor.constraint(equalTo: self.navigationBarView.leadingAnchor),
self.navigationBarItemsView.trailingAnchor.constraint(equalTo: self.navigationBarView.trailingAnchor)
])
self.liquidGlassContainerView?.removeFromSuperview()
self.liquidGlassContainerView = nil
self.navigationBarView.backgroundColor = self.navBarBackgroundColor
self.navGradientBar?.isHidden = false
}
/// 滚动到阈值以上时淡入玻璃,回到顶部时还原为透明;期间标题/按钮始终可见。
/// 优于 `st_linkScrollAlpha`:后者连 title/button 也一起透明掉。
@discardableResult
open func st_linkLiquidGlassVisibility(_ scrollView: UIScrollView, threshold: CGFloat = 20) -> Self {
self.contentOffsetObservation?.invalidate()
let clampedThreshold = max(1, threshold)
// 初始:默认隐藏玻璃,等滚动再淡入
if let container = self.liquidGlassContainerView as? UIVisualEffectView {
container.effect = nil
}
var lastVisible = false
self.contentOffsetObservation = scrollView.observe(
\.contentOffset, options: [.new],
changeHandler: { [weak self] scroll, _ in
guard let self = self else { return }
let adjusted = scroll.contentOffset.y + scroll.adjustedContentInset.top
let shouldShow = adjusted > clampedThreshold
if lastVisible == shouldShow { return }
lastVisible = shouldShow
let targetEffect: UIVisualEffect? = shouldShow ? STGlassEffectFactory.makeVisualEffect() : nil
UIView.animate(withDuration: 0.22) {
if let container = self.liquidGlassContainerView as? UIVisualEffectView {
container.effect = targetEffect
}
}
})
return self
}
}