@onekeyfe/react-native-bottom-tabs 6.7.0-62 → 6.7.0-63
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/android/src/main/java/com/onekey/bottomtabs/BottomTabsDrawerManager.kt +15 -0
- package/android/src/main/java/com/onekey/bottomtabs/BottomTabsDrawerView.kt +276 -54
- package/docs/SPEC.md +105 -18
- package/ios/RCTBottomTabsDrawerComponentView.mm +24 -0
- package/ios/RCTBottomTabsDrawerContainerView.swift +314 -48
- package/ios/tests/BottomTabsDrawerReduceMotionTests.swift +231 -0
- package/lib/module/BottomTabs.js +27 -2
- package/lib/module/BottomTabsDrawerNativeComponent.ts +3 -0
- package/lib/module/index.js +1 -0
- package/lib/typescript/src/BottomTabs.d.ts +19 -1
- package/lib/typescript/src/BottomTabsDrawerNativeComponent.d.ts +5 -0
- package/lib/typescript/src/index.d.ts +1 -0
- package/package.json +1 -1
- package/react-native-bottom-tabs.podspec +6 -0
- package/src/BottomTabs.tsx +48 -3
- package/src/BottomTabsDrawerNativeComponent.ts +3 -0
- package/src/index.tsx +1 -0
|
@@ -8,6 +8,7 @@ final class RCTBottomTabsDrawerContainerView: UIView, UIGestureRecognizerDelegat
|
|
|
8
8
|
guard enabled != oldValue else { return }
|
|
9
9
|
interruptGesture()
|
|
10
10
|
stopAnimation()
|
|
11
|
+
removeFadeSnapshot()
|
|
11
12
|
stableOpen = false
|
|
12
13
|
setProgress(0)
|
|
13
14
|
if enabled && open { settle(to: true, velocity: 0, user: false) }
|
|
@@ -23,11 +24,29 @@ final class RCTBottomTabsDrawerContainerView: UIView, UIGestureRecognizerDelegat
|
|
|
23
24
|
@objc var swipeEnabled = false
|
|
24
25
|
@objc var hapticFeedbackEnabled = false
|
|
25
26
|
@objc var bottomBarHeight: Double = 0
|
|
27
|
+
/// "light" or "dark" forces that palette; anything else follows the trait collection.
|
|
28
|
+
@objc var colorScheme = "system" {
|
|
29
|
+
didSet {
|
|
30
|
+
guard colorScheme != oldValue else { return }
|
|
31
|
+
applyAppearance()
|
|
32
|
+
setProgress(appliedValue, overshoot: appliedOvershoot)
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
@objc var progressEventsEnabled = false {
|
|
36
|
+
didSet {
|
|
37
|
+
guard progressEventsEnabled != oldValue else { return }
|
|
38
|
+
// A new subscriber starts from the current position.
|
|
39
|
+
reportedProgress = -1
|
|
40
|
+
if progressEventsEnabled { reportProgress() }
|
|
41
|
+
}
|
|
42
|
+
}
|
|
26
43
|
@objc var onDrawerOpenChange: ((Bool) -> Void)?
|
|
44
|
+
@objc var onDrawerProgress: ((Double) -> Void)?
|
|
27
45
|
|
|
28
46
|
private let menu = UIView()
|
|
29
|
-
private let menuShade = UIView()
|
|
30
47
|
private let foreground = UIView()
|
|
48
|
+
// Clips the page to its rounded corners; the unclipped foreground casts the shadow.
|
|
49
|
+
private let pageClip = UIView()
|
|
31
50
|
private let foregroundContent = UIView()
|
|
32
51
|
private let dim = UIButton(type: .custom)
|
|
33
52
|
private var slots: [UIView] = []
|
|
@@ -45,30 +64,91 @@ final class RCTBottomTabsDrawerContainerView: UIView, UIGestureRecognizerDelegat
|
|
|
45
64
|
private var animationTarget = false
|
|
46
65
|
private var animationUser = false
|
|
47
66
|
private var previousSize = CGSize.zero
|
|
67
|
+
private var animationDuration: CFTimeInterval = 0
|
|
68
|
+
private var scrimMaxAlpha: CGFloat = 0.58
|
|
69
|
+
private var pageShadowOpacity: Float = 0
|
|
70
|
+
private var pageHairline = false
|
|
71
|
+
private var reportedProgress: CGFloat = -1
|
|
72
|
+
// The last requested position, kept so a layout pass mid-settle does not
|
|
73
|
+
// clip the spring's overshoot back to fully open.
|
|
74
|
+
private var appliedValue: CGFloat = 0
|
|
75
|
+
private var appliedOvershoot = false
|
|
76
|
+
// Running speed of the current animation, for handing off to a retarget.
|
|
77
|
+
private var trackedValue: CGFloat = 0
|
|
78
|
+
private var trackedTime: CFTimeInterval = 0
|
|
79
|
+
private var trackedVelocity: CGFloat = 0
|
|
80
|
+
private var shadowPathRadius: CGFloat = -1
|
|
81
|
+
// An in-flight Reduce Motion cross-fade: its one snapshot, the state it
|
|
82
|
+
// lands on and whether that is a user transition. Like a running settle, it
|
|
83
|
+
// updates `stableOpen` and reports only when it lands.
|
|
84
|
+
private var fadeSnapshot: UIView?
|
|
85
|
+
private var fadeTarget = false
|
|
86
|
+
private var fadeUser = false
|
|
87
|
+
// Hooks for native tests: they set Reduce Motion, and run without an app
|
|
88
|
+
// host, where UIKit cannot snapshot a view.
|
|
89
|
+
var isReduceMotionEnabled: () -> Bool = { UIAccessibility.isReduceMotionEnabled }
|
|
90
|
+
var makeFadeSnapshot: (UIView) -> UIView? = { $0.snapshotView(afterScreenUpdates: false) }
|
|
91
|
+
|
|
92
|
+
// Measured frame by frame from the reference recording: the page travels
|
|
93
|
+
// 80% of the width, keeps a fixed display-like corner, washes toward the
|
|
94
|
+
// background linearly, and casts a soft shadow onto the menu below it (a
|
|
95
|
+
// hairline in dark mode).
|
|
96
|
+
private static let menuWidthFraction: CGFloat = 0.8
|
|
97
|
+
private static let pageCornerRadius: CGFloat = 62
|
|
98
|
+
private static let menuMinScale: CGFloat = 0.97
|
|
99
|
+
// Released drags settle on a slightly underdamped spring: about 2pt of
|
|
100
|
+
// overshoot after a fling, none after a slow release.
|
|
101
|
+
private static let springFrequency: CGFloat = 25.5
|
|
102
|
+
private static let springDamping: CGFloat = 0.84
|
|
103
|
+
private var menuWidth: CGFloat { bounds.width * Self.menuWidthFraction }
|
|
48
104
|
|
|
49
105
|
override init(frame: CGRect) {
|
|
50
106
|
super.init(frame: frame)
|
|
51
107
|
clipsToBounds = true
|
|
52
108
|
addSubview(menu)
|
|
53
|
-
menuShade.backgroundColor = .black
|
|
54
|
-
menuShade.isUserInteractionEnabled = false
|
|
55
|
-
menu.addSubview(menuShade)
|
|
56
109
|
addSubview(foreground)
|
|
57
|
-
foreground.addSubview(
|
|
58
|
-
|
|
110
|
+
foreground.addSubview(pageClip)
|
|
111
|
+
pageClip.addSubview(foregroundContent)
|
|
59
112
|
dim.accessibilityLabel = NSLocalizedString("Close menu", comment: "Close the tab drawer")
|
|
60
113
|
dim.addTarget(self, action: #selector(closeFromUser), for: .touchUpInside)
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
114
|
+
pageClip.addSubview(dim)
|
|
115
|
+
pageClip.layer.maskedCorners = [.layerMinXMinYCorner, .layerMinXMaxYCorner]
|
|
116
|
+
pageClip.layer.cornerCurve = .circular
|
|
117
|
+
pageClip.clipsToBounds = true
|
|
118
|
+
foreground.layer.shadowColor = UIColor.black.cgColor
|
|
119
|
+
foreground.layer.shadowOffset = .zero
|
|
120
|
+
foreground.layer.shadowRadius = 18
|
|
121
|
+
foreground.layer.shadowOpacity = 0
|
|
65
122
|
pan = UIPanGestureRecognizer(target: self, action: #selector(handlePan(_:)))
|
|
66
123
|
pan.maximumNumberOfTouches = 1
|
|
67
124
|
pan.delegate = self
|
|
68
125
|
addGestureRecognizer(pan)
|
|
126
|
+
applyAppearance()
|
|
69
127
|
setProgress(0)
|
|
70
128
|
}
|
|
71
129
|
|
|
130
|
+
private func applyAppearance() {
|
|
131
|
+
let dark: Bool
|
|
132
|
+
switch colorScheme {
|
|
133
|
+
case "dark": dark = true
|
|
134
|
+
case "light": dark = false
|
|
135
|
+
default: dark = traitCollection.userInterfaceStyle == .dark
|
|
136
|
+
}
|
|
137
|
+
// The page washes toward white in light mode and toward gray in dark mode.
|
|
138
|
+
dim.backgroundColor = dark ? UIColor(white: 36.0 / 255.0, alpha: 1) : .white
|
|
139
|
+
scrimMaxAlpha = dark ? 0.58 : 0.5
|
|
140
|
+
pageShadowOpacity = dark ? 0 : 0.16
|
|
141
|
+
pageHairline = dark
|
|
142
|
+
pageClip.layer.borderColor = UIColor(white: 1, alpha: 0.17).cgColor
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {
|
|
146
|
+
super.traitCollectionDidChange(previousTraitCollection)
|
|
147
|
+
guard traitCollection.hasDifferentColorAppearance(comparedTo: previousTraitCollection) else { return }
|
|
148
|
+
applyAppearance()
|
|
149
|
+
setProgress(appliedValue, overshoot: appliedOvershoot)
|
|
150
|
+
}
|
|
151
|
+
|
|
72
152
|
required init?(coder: NSCoder) { fatalError("init(coder:) has not been implemented") }
|
|
73
153
|
|
|
74
154
|
@objc func insertChild(_ child: UIView, atIndex index: Int) {
|
|
@@ -87,13 +167,14 @@ final class RCTBottomTabsDrawerContainerView: UIView, UIGestureRecognizerDelegat
|
|
|
87
167
|
for (index, child) in slots.enumerated() {
|
|
88
168
|
(index == 0 ? menu : foregroundContent).insertSubview(child, at: 0)
|
|
89
169
|
}
|
|
90
|
-
menu.bringSubviewToFront(menuShade)
|
|
91
170
|
setNeedsLayout()
|
|
92
171
|
}
|
|
93
172
|
|
|
94
173
|
override func layoutSubviews() {
|
|
95
174
|
super.layoutSubviews()
|
|
96
175
|
if previousSize != bounds.size {
|
|
176
|
+
// A cross-fade's image is of the old size, so it lands at once.
|
|
177
|
+
completeFade()
|
|
97
178
|
let dragging = pan.state == .began || pan.state == .changed
|
|
98
179
|
let target = displayLink != nil ? animationTarget : (dragging ? gestureReturnOpen : stableOpen)
|
|
99
180
|
let user = displayLink != nil && animationUser && previousSize != .zero
|
|
@@ -104,14 +185,16 @@ final class RCTBottomTabsDrawerContainerView: UIView, UIGestureRecognizerDelegat
|
|
|
104
185
|
}
|
|
105
186
|
menu.transform = .identity
|
|
106
187
|
foreground.transform = .identity
|
|
107
|
-
menu.frame = CGRect(x: 0, y: 0, width:
|
|
188
|
+
menu.frame = CGRect(x: 0, y: 0, width: menuWidth, height: bounds.height)
|
|
108
189
|
foreground.frame = bounds
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
190
|
+
pageClip.frame = foreground.bounds
|
|
191
|
+
foregroundContent.frame = pageClip.bounds
|
|
192
|
+
dim.frame = pageClip.bounds
|
|
193
|
+
// Rebuilt for the new bounds by setProgress below.
|
|
194
|
+
shadowPathRadius = -1
|
|
112
195
|
if slots.count > 0 { slots[0].frame = menu.bounds }
|
|
113
196
|
if slots.count > 1 { slots[1].frame = foreground.bounds }
|
|
114
|
-
setProgress(
|
|
197
|
+
setProgress(appliedValue, overshoot: appliedOvershoot)
|
|
115
198
|
}
|
|
116
199
|
|
|
117
200
|
override func didMoveToWindow() {
|
|
@@ -119,6 +202,7 @@ final class RCTBottomTabsDrawerContainerView: UIView, UIGestureRecognizerDelegat
|
|
|
119
202
|
if window == nil {
|
|
120
203
|
interruptGesture()
|
|
121
204
|
stopAnimation()
|
|
205
|
+
removeFadeSnapshot()
|
|
122
206
|
setProgress(stableOpen && enabled ? 1 : 0)
|
|
123
207
|
} else if enabled && stableOpen != open {
|
|
124
208
|
settle(to: open, velocity: 0, user: false)
|
|
@@ -131,13 +215,23 @@ final class RCTBottomTabsDrawerContainerView: UIView, UIGestureRecognizerDelegat
|
|
|
131
215
|
return true
|
|
132
216
|
}
|
|
133
217
|
|
|
218
|
+
// While a cross-fade runs, the views under the snapshot already show the
|
|
219
|
+
// final state, so a touch would reach a control that is not where it
|
|
220
|
+
// appears. The host takes those touches itself (the snapshot cannot: its
|
|
221
|
+
// alpha is already 0 for hit-testing once the fade starts). The pan is on
|
|
222
|
+
// the host, so a drag still takes over the fade.
|
|
223
|
+
override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? {
|
|
224
|
+
let hit = super.hitTest(point, with: event)
|
|
225
|
+
return hit != nil && fadeSnapshot != nil ? self : hit
|
|
226
|
+
}
|
|
227
|
+
|
|
134
228
|
func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldReceive touch: UITouch) -> Bool {
|
|
135
229
|
guard enabled else { return false }
|
|
136
230
|
let start = touch.location(in: self)
|
|
137
231
|
guard start.y < bounds.height - CGFloat(max(0, bottomBarHeight)) else { return false }
|
|
138
232
|
guard !isInsideNativeTabBar(touch.view) else { return false }
|
|
139
233
|
touchTarget = touch.view
|
|
140
|
-
if progress > 0 { return start.x >=
|
|
234
|
+
if progress > 0 { return start.x >= menuWidth * progress }
|
|
141
235
|
return swipeEnabled && !touchPathContainsExclusiveControl()
|
|
142
236
|
}
|
|
143
237
|
|
|
@@ -150,7 +244,7 @@ final class RCTBottomTabsDrawerContainerView: UIView, UIGestureRecognizerDelegat
|
|
|
150
244
|
let start = CGPoint(x: location.x - translation.x, y: location.y - translation.y)
|
|
151
245
|
guard start.y < bounds.height - CGFloat(max(0, bottomBarHeight)) else { return false }
|
|
152
246
|
if progress > 0 {
|
|
153
|
-
return start.x >=
|
|
247
|
+
return start.x >= menuWidth * progress
|
|
154
248
|
}
|
|
155
249
|
return swipeEnabled && velocity.x > 0 && !touchPathCanScrollRight()
|
|
156
250
|
}
|
|
@@ -244,8 +338,18 @@ final class RCTBottomTabsDrawerContainerView: UIView, UIGestureRecognizerDelegat
|
|
|
244
338
|
@objc private func handlePan(_ recognizer: UIPanGestureRecognizer) {
|
|
245
339
|
switch recognizer.state {
|
|
246
340
|
case .began:
|
|
247
|
-
|
|
341
|
+
// The drag takes over a settle or cross-fade in flight and drops its
|
|
342
|
+
// completion. Cancelling returns to a programmatic target, else to the
|
|
343
|
+
// stable state.
|
|
344
|
+
if displayLink != nil && !animationUser {
|
|
345
|
+
gestureReturnOpen = animationTarget
|
|
346
|
+
} else if fadeSnapshot != nil && !fadeUser {
|
|
347
|
+
gestureReturnOpen = fadeTarget
|
|
348
|
+
} else {
|
|
349
|
+
gestureReturnOpen = stableOpen
|
|
350
|
+
}
|
|
248
351
|
stopAnimation()
|
|
352
|
+
removeFadeSnapshot()
|
|
249
353
|
gestureStartProgress = progress
|
|
250
354
|
case .changed:
|
|
251
355
|
guard recognizer.numberOfTouches <= 1 else {
|
|
@@ -253,10 +357,10 @@ final class RCTBottomTabsDrawerContainerView: UIView, UIGestureRecognizerDelegat
|
|
|
253
357
|
settle(to: gestureReturnOpen, velocity: 0, user: false)
|
|
254
358
|
return
|
|
255
359
|
}
|
|
256
|
-
setProgress(gestureStartProgress + recognizer.translation(in: self).x / max(1,
|
|
360
|
+
setProgress(gestureStartProgress + recognizer.translation(in: self).x / max(1, menuWidth))
|
|
257
361
|
case .ended:
|
|
258
|
-
let velocity = min(
|
|
259
|
-
settle(to: progress + velocity * 0.12 >= 0.5, velocity: velocity, user: true)
|
|
362
|
+
let velocity = min(12, max(-12, recognizer.velocity(in: self).x / max(1, menuWidth)))
|
|
363
|
+
settle(to: progress + velocity * 0.12 >= 0.5, velocity: velocity, user: true, tween: false)
|
|
260
364
|
case .cancelled, .failed:
|
|
261
365
|
if !suppressCancellation { settle(to: gestureReturnOpen, velocity: 0, user: false) }
|
|
262
366
|
default: break
|
|
@@ -277,49 +381,208 @@ final class RCTBottomTabsDrawerContainerView: UIView, UIGestureRecognizerDelegat
|
|
|
277
381
|
settle(to: false, velocity: 0, user: true)
|
|
278
382
|
}
|
|
279
383
|
|
|
280
|
-
private func setProgress(_ value: CGFloat) {
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
384
|
+
private func setProgress(_ value: CGFloat, overshoot: Bool = false) {
|
|
385
|
+
appliedValue = value
|
|
386
|
+
appliedOvershoot = overshoot
|
|
387
|
+
let raw = enabled ? value : 0
|
|
388
|
+
progress = min(1, max(0, raw))
|
|
389
|
+
let visible = progress > 0
|
|
390
|
+
// Only the settle spring may carry the page a few points past open.
|
|
391
|
+
let travel = overshoot ? min(1.03, max(0, raw)) : progress
|
|
392
|
+
foreground.transform = CGAffineTransform(translationX: menuWidth * travel, y: 0)
|
|
393
|
+
// Rounds in over the first 10% so screens with other corner radii never pop.
|
|
394
|
+
let cornerRadius = visible ? Self.pageCornerRadius * min(1, progress / 0.1) : 0
|
|
395
|
+
pageClip.layer.cornerRadius = cornerRadius
|
|
396
|
+
// The shadow follows the same corner while it rounds in.
|
|
397
|
+
if cornerRadius != shadowPathRadius {
|
|
398
|
+
shadowPathRadius = cornerRadius
|
|
399
|
+
foreground.layer.shadowPath = UIBezierPath(
|
|
400
|
+
roundedRect: foreground.bounds,
|
|
401
|
+
byRoundingCorners: [.topLeft, .bottomLeft],
|
|
402
|
+
cornerRadii: CGSize(width: cornerRadius, height: cornerRadius)
|
|
403
|
+
).cgPath
|
|
404
|
+
}
|
|
405
|
+
pageClip.layer.borderWidth = visible && pageHairline ? 1 / max(1, traitCollection.displayScale) : 0
|
|
406
|
+
foreground.layer.shadowOpacity = visible ? pageShadowOpacity : 0
|
|
407
|
+
dim.alpha = scrimMaxAlpha * progress
|
|
408
|
+
dim.isHidden = !visible
|
|
409
|
+
foregroundContent.isUserInteractionEnabled = !visible
|
|
410
|
+
foregroundContent.accessibilityElementsHidden = visible
|
|
411
|
+
// The menu grows from its left-center edge and fades in over whatever is
|
|
412
|
+
// behind the host, fully visible by 60% open.
|
|
413
|
+
let minScale = isReduceMotionEnabled() ? 1 : Self.menuMinScale
|
|
414
|
+
let scale = minScale + (1 - minScale) * progress
|
|
415
|
+
menu.transform = CGAffineTransform(translationX: -(1 - scale) * menu.bounds.width / 2, y: 0)
|
|
416
|
+
.scaledBy(x: scale, y: scale)
|
|
417
|
+
menu.alpha = min(1, progress / 0.6)
|
|
418
|
+
menu.isHidden = !visible
|
|
419
|
+
menu.accessibilityElementsHidden = !visible
|
|
420
|
+
reportProgress()
|
|
421
|
+
}
|
|
422
|
+
|
|
423
|
+
/// Sends the current position again, for an event emitter attached after it was reported.
|
|
424
|
+
@objc func resendProgress() {
|
|
425
|
+
reportedProgress = -1
|
|
426
|
+
reportProgress()
|
|
292
427
|
}
|
|
293
428
|
|
|
294
|
-
private func
|
|
429
|
+
private func reportProgress() {
|
|
430
|
+
guard progressEventsEnabled else { return }
|
|
431
|
+
if progress != reportedProgress && (abs(progress - reportedProgress) >= 0.002 || progress == 0 || progress == 1) {
|
|
432
|
+
reportedProgress = progress
|
|
433
|
+
onDrawerProgress?(Double(progress))
|
|
434
|
+
}
|
|
435
|
+
}
|
|
436
|
+
|
|
437
|
+
// cubic-bezier(0.55, 0.6, 0.2, 1), fitted to the reference tap open and close.
|
|
438
|
+
private static func pageCurve(_ x: CGFloat) -> CGFloat {
|
|
439
|
+
func bezier(_ t: CGFloat, _ p1: CGFloat, _ p2: CGFloat) -> CGFloat {
|
|
440
|
+
let u = 1 - t
|
|
441
|
+
return 3 * u * u * t * p1 + 3 * u * t * t * p2 + t * t * t
|
|
442
|
+
}
|
|
443
|
+
var low: CGFloat = 0
|
|
444
|
+
var high: CGFloat = 1
|
|
445
|
+
for _ in 0..<24 {
|
|
446
|
+
let mid = (low + high) / 2
|
|
447
|
+
if bezier(mid, 0.55, 0.2) < x { low = mid } else { high = mid }
|
|
448
|
+
}
|
|
449
|
+
return bezier((low + high) / 2, 0.6, 1)
|
|
450
|
+
}
|
|
451
|
+
|
|
452
|
+
// Taps and programmatic changes ease over a fixed time; a released drag
|
|
453
|
+
// keeps its finger velocity through a slightly underdamped spring.
|
|
454
|
+
private func settle(to requested: Bool, velocity: CGFloat, user: Bool, tween: Bool = true) {
|
|
455
|
+
let target = enabled && requested
|
|
456
|
+
// An animation already heading there keeps running; the newer request
|
|
457
|
+
// still decides whether its completion is a user event.
|
|
458
|
+
if displayLink != nil && animationTarget == target {
|
|
459
|
+
animationUser = user
|
|
460
|
+
return
|
|
461
|
+
}
|
|
462
|
+
// Reversing mid-flight continues from the page's on-screen position and
|
|
463
|
+
// speed, so the motion bends instead of stopping dead.
|
|
464
|
+
let retarget = displayLink != nil
|
|
465
|
+
let from = retarget ? appliedValue : progress
|
|
466
|
+
let carried = retarget ? trackedVelocity : velocity
|
|
295
467
|
stopAnimation()
|
|
296
|
-
let target = enabled && target
|
|
297
468
|
guard bounds.width > 0, window != nil else {
|
|
469
|
+
removeFadeSnapshot()
|
|
298
470
|
stableOpen = target
|
|
299
471
|
setProgress(target ? 1 : 0)
|
|
300
472
|
return
|
|
301
473
|
}
|
|
302
|
-
if progress == (target ? 1 : 0) {
|
|
474
|
+
if !retarget && progress == (target ? 1 : 0) {
|
|
475
|
+
// A cross-fade landing there keeps fading; as for a running animation,
|
|
476
|
+
// the newer request decides whether its completion is a user event.
|
|
477
|
+
if fadeSnapshot != nil {
|
|
478
|
+
fadeUser = user
|
|
479
|
+
return
|
|
480
|
+
}
|
|
303
481
|
finish(target: target, user: user)
|
|
304
482
|
return
|
|
305
483
|
}
|
|
306
|
-
|
|
307
|
-
|
|
484
|
+
if tween && !retarget && isReduceMotionEnabled() && crossFade(to: target, user: user) {
|
|
485
|
+
return
|
|
486
|
+
}
|
|
487
|
+
removeFadeSnapshot()
|
|
488
|
+
animationFrom = from
|
|
489
|
+
animationVelocity = min(12, max(-12, carried))
|
|
308
490
|
animationTarget = target
|
|
309
491
|
animationUser = user
|
|
492
|
+
// Closing runs about 20% shorter than opening.
|
|
493
|
+
let fullDuration: CGFloat = target ? 0.36 : 0.29
|
|
494
|
+
animationDuration = tween && !retarget ? max(0.12, fullDuration * abs((target ? 1 : 0) - from)) : 0
|
|
310
495
|
animationStart = CACurrentMediaTime()
|
|
496
|
+
trackedValue = from
|
|
497
|
+
trackedTime = animationStart
|
|
498
|
+
trackedVelocity = animationVelocity
|
|
311
499
|
let link = CADisplayLink(target: self, selector: #selector(tick))
|
|
312
500
|
link.add(to: .main, forMode: .common)
|
|
313
501
|
displayLink = link
|
|
314
502
|
}
|
|
315
503
|
|
|
504
|
+
// Reduce Motion: taps and programmatic changes cross-fade into place
|
|
505
|
+
// instead of sliding the whole page.
|
|
506
|
+
private func crossFade(to target: Bool, user: Bool) -> Bool {
|
|
507
|
+
// Captures what is on screen, including any fade still in progress, which
|
|
508
|
+
// this one then replaces: the host keeps at most one snapshot.
|
|
509
|
+
guard let snapshot = makeFadeSnapshot(self) else { return false }
|
|
510
|
+
removeFadeSnapshot()
|
|
511
|
+
snapshot.frame = bounds
|
|
512
|
+
snapshot.isUserInteractionEnabled = false
|
|
513
|
+
addSubview(snapshot)
|
|
514
|
+
fadeSnapshot = snapshot
|
|
515
|
+
fadeTarget = target
|
|
516
|
+
fadeUser = user
|
|
517
|
+
// The final state shows now, under the snapshot. The transition completes
|
|
518
|
+
// (stable state, callback and haptic) only when the fade does.
|
|
519
|
+
setProgress(target ? 1 : 0)
|
|
520
|
+
UIView.animate(withDuration: 0.2, delay: 0, options: [.curveEaseOut], animations: {
|
|
521
|
+
snapshot.alpha = 0
|
|
522
|
+
}, completion: { [weak self] _ in
|
|
523
|
+
snapshot.removeFromSuperview()
|
|
524
|
+
guard let self, self.fadeSnapshot === snapshot else { return }
|
|
525
|
+
self.completeFade()
|
|
526
|
+
})
|
|
527
|
+
return true
|
|
528
|
+
}
|
|
529
|
+
|
|
530
|
+
/// Lands an in-flight cross-fade on its target, as a settle's last frame does.
|
|
531
|
+
private func completeFade() {
|
|
532
|
+
guard fadeSnapshot != nil else { return }
|
|
533
|
+
let target = fadeTarget
|
|
534
|
+
let user = fadeUser
|
|
535
|
+
removeFadeSnapshot()
|
|
536
|
+
finish(target: target, user: user)
|
|
537
|
+
}
|
|
538
|
+
|
|
539
|
+
/// Drops the cross-fade without landing it (a newer transition, a drag,
|
|
540
|
+
/// disabling or detach), so its completion is never reported.
|
|
541
|
+
private func removeFadeSnapshot() {
|
|
542
|
+
guard let snapshot = fadeSnapshot else { return }
|
|
543
|
+
// Cleared first, so the completion handler that removing the animation
|
|
544
|
+
// triggers does not land it.
|
|
545
|
+
fadeSnapshot = nil
|
|
546
|
+
snapshot.layer.removeAllAnimations()
|
|
547
|
+
snapshot.removeFromSuperview()
|
|
548
|
+
}
|
|
549
|
+
|
|
316
550
|
@objc private func tick() {
|
|
317
|
-
let
|
|
551
|
+
let now = CACurrentMediaTime()
|
|
552
|
+
let elapsed = CGFloat(now - animationStart)
|
|
318
553
|
let target: CGFloat = animationTarget ? 1 : 0
|
|
319
|
-
let
|
|
320
|
-
let
|
|
321
|
-
|
|
322
|
-
if
|
|
554
|
+
let value: CGFloat
|
|
555
|
+
let isSpring = animationDuration == 0
|
|
556
|
+
var tweenDone = false
|
|
557
|
+
if !isSpring {
|
|
558
|
+
let x = min(1, elapsed / CGFloat(animationDuration))
|
|
559
|
+
value = animationFrom + (target - animationFrom) * Self.pageCurve(x)
|
|
560
|
+
tweenDone = x >= 1
|
|
561
|
+
} else {
|
|
562
|
+
let w = Self.springFrequency
|
|
563
|
+
let a = animationFrom - target
|
|
564
|
+
if isReduceMotionEnabled() {
|
|
565
|
+
// Critically damped: same speed, no bounce.
|
|
566
|
+
value = target + (a + (animationVelocity + w * a) * elapsed) * exp(-w * elapsed)
|
|
567
|
+
} else {
|
|
568
|
+
let z = Self.springDamping
|
|
569
|
+
let wd = w * sqrt(1 - z * z)
|
|
570
|
+
let decay = exp(-z * w * elapsed)
|
|
571
|
+
value = target + decay * (a * cos(wd * elapsed) + (animationVelocity + z * w * a) / wd * sin(wd * elapsed))
|
|
572
|
+
}
|
|
573
|
+
}
|
|
574
|
+
if now > trackedTime {
|
|
575
|
+
trackedVelocity = (value - trackedValue) / CGFloat(now - trackedTime)
|
|
576
|
+
}
|
|
577
|
+
trackedValue = value
|
|
578
|
+
trackedTime = now
|
|
579
|
+
// Reduce Motion never carries the page past its target.
|
|
580
|
+
setProgress(value, overshoot: isSpring && !isReduceMotionEnabled())
|
|
581
|
+
// A spring ends once it is visually at rest, not after a fixed time.
|
|
582
|
+
let done = isSpring
|
|
583
|
+
? (abs(value - target) < 0.0005 && abs(trackedVelocity) < 0.02) || elapsed >= 0.6
|
|
584
|
+
: tweenDone
|
|
585
|
+
if done {
|
|
323
586
|
let target = animationTarget
|
|
324
587
|
let user = animationUser
|
|
325
588
|
stopAnimation()
|
|
@@ -331,10 +594,13 @@ final class RCTBottomTabsDrawerContainerView: UIView, UIGestureRecognizerDelegat
|
|
|
331
594
|
let changed = stableOpen != target
|
|
332
595
|
stableOpen = target
|
|
333
596
|
setProgress(target ? 1 : 0)
|
|
334
|
-
if user
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
597
|
+
if user { reportCompletion(target: target, changed: changed) }
|
|
598
|
+
}
|
|
599
|
+
|
|
600
|
+
private func reportCompletion(target: Bool, changed: Bool) {
|
|
601
|
+
guard changed || target != open else { return }
|
|
602
|
+
if hapticFeedbackEnabled && changed { UIImpactFeedbackGenerator(style: .light).impactOccurred() }
|
|
603
|
+
onDrawerOpenChange?(target)
|
|
338
604
|
}
|
|
339
605
|
|
|
340
606
|
private func stopAnimation() {
|