react-native-marquee-animation 0.1.0

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.
Files changed (68) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +174 -0
  3. package/RNMarquee.podspec +27 -0
  4. package/THIRD_PARTY_NOTICES.md +8 -0
  5. package/android/build.gradle +58 -0
  6. package/android/settings.gradle +34 -0
  7. package/android/src/main/AndroidManifest.xml +1 -0
  8. package/android/src/main/java/com/marquee/MarqueeEvent.kt +44 -0
  9. package/android/src/main/java/com/marquee/MarqueeModels.kt +45 -0
  10. package/android/src/main/java/com/marquee/MarqueeMotion.kt +163 -0
  11. package/android/src/main/java/com/marquee/MarqueePackage.kt +16 -0
  12. package/android/src/main/java/com/marquee/MarqueeView.kt +376 -0
  13. package/android/src/main/java/com/marquee/MarqueeViewManager.kt +135 -0
  14. package/ios/MarqueeRenderer.swift +652 -0
  15. package/ios/RNMarqueeView.h +14 -0
  16. package/ios/RNMarqueeView.mm +118 -0
  17. package/lib/module/RNMarqueeViewNativeComponent.ts +32 -0
  18. package/lib/module/index.js +4 -0
  19. package/lib/module/index.js.map +1 -0
  20. package/lib/module/internal/events.js +10 -0
  21. package/lib/module/internal/events.js.map +1 -0
  22. package/lib/module/internal/geometry.js +10 -0
  23. package/lib/module/internal/geometry.js.map +1 -0
  24. package/lib/module/internal/options.js +29 -0
  25. package/lib/module/internal/options.js.map +1 -0
  26. package/lib/module/marquee.js +32 -0
  27. package/lib/module/marquee.js.map +1 -0
  28. package/lib/module/marquee.native.js +95 -0
  29. package/lib/module/marquee.native.js.map +1 -0
  30. package/lib/module/package.json +1 -0
  31. package/lib/module/types.js +4 -0
  32. package/lib/module/types.js.map +1 -0
  33. package/lib/module/use-reduced-motion.js +61 -0
  34. package/lib/module/use-reduced-motion.js.map +1 -0
  35. package/lib/module/view-props.js +5 -0
  36. package/lib/module/view-props.js.map +1 -0
  37. package/lib/typescript/package.json +1 -0
  38. package/lib/typescript/src/RNMarqueeViewNativeComponent.d.ts +28 -0
  39. package/lib/typescript/src/RNMarqueeViewNativeComponent.d.ts.map +1 -0
  40. package/lib/typescript/src/index.d.ts +3 -0
  41. package/lib/typescript/src/index.d.ts.map +1 -0
  42. package/lib/typescript/src/internal/events.d.ts +3 -0
  43. package/lib/typescript/src/internal/events.d.ts.map +1 -0
  44. package/lib/typescript/src/internal/geometry.d.ts +3 -0
  45. package/lib/typescript/src/internal/geometry.d.ts.map +1 -0
  46. package/lib/typescript/src/internal/options.d.ts +16 -0
  47. package/lib/typescript/src/internal/options.d.ts.map +1 -0
  48. package/lib/typescript/src/marquee.d.ts +469 -0
  49. package/lib/typescript/src/marquee.d.ts.map +1 -0
  50. package/lib/typescript/src/marquee.native.d.ts +470 -0
  51. package/lib/typescript/src/marquee.native.d.ts.map +1 -0
  52. package/lib/typescript/src/types.d.ts +46 -0
  53. package/lib/typescript/src/types.d.ts.map +1 -0
  54. package/lib/typescript/src/use-reduced-motion.d.ts +3 -0
  55. package/lib/typescript/src/use-reduced-motion.d.ts.map +1 -0
  56. package/lib/typescript/src/view-props.d.ts +4 -0
  57. package/lib/typescript/src/view-props.d.ts.map +1 -0
  58. package/package.json +166 -0
  59. package/src/RNMarqueeViewNativeComponent.ts +32 -0
  60. package/src/index.ts +12 -0
  61. package/src/internal/events.ts +12 -0
  62. package/src/internal/geometry.ts +14 -0
  63. package/src/internal/options.ts +48 -0
  64. package/src/marquee.native.tsx +144 -0
  65. package/src/marquee.tsx +35 -0
  66. package/src/types.ts +59 -0
  67. package/src/use-reduced-motion.ts +88 -0
  68. package/src/view-props.ts +24 -0
@@ -0,0 +1,652 @@
1
+ import QuartzCore
2
+ import UIKit
3
+
4
+ @objc public protocol MarqueeRendererDelegate: AnyObject {
5
+ func marqueeRenderer(_ renderer: MarqueeRenderer, didChangeState state: String)
6
+ func marqueeRenderer(
7
+ _ renderer: MarqueeRenderer,
8
+ didLayoutContentWidth contentWidth: CGFloat,
9
+ containerWidth: CGFloat
10
+ )
11
+ }
12
+
13
+ @objcMembers public final class MarqueeConfiguration: NSObject {
14
+ public var contentWidth: CGFloat = 0
15
+ public var spacing: CGFloat = 0
16
+ public var active = true
17
+ public var reduceMotion = false
18
+ public var speed: CGFloat = 25
19
+ public var direction = "left"
20
+ public var shortContentMode = "static"
21
+ public var contentAlignment = "start"
22
+ public var maxFlingVelocity: CGFloat = 3_200
23
+ public var deceleration: CGFloat = 0.9985
24
+ public var pauseOnPress = true
25
+ public var resumeDelay: TimeInterval = 0
26
+ }
27
+
28
+ enum MarqueeMath {
29
+ static func positiveModulo(_ value: CGFloat, modulus: CGFloat) -> CGFloat {
30
+ guard modulus.isFinite, modulus > 0, value.isFinite else { return 0 }
31
+ let result = value.truncatingRemainder(dividingBy: modulus)
32
+ return result >= 0 ? result : result + modulus
33
+ }
34
+
35
+ static func blendedVelocity(from: CGFloat, to: CGFloat, progress: CGFloat) -> CGFloat {
36
+ let clamped = min(1, max(0, progress))
37
+ let eased = clamped * clamped * (3 - 2 * clamped)
38
+ return from + (to - from) * eased
39
+ }
40
+
41
+ static func shouldBeginPan(
42
+ velocity: CGPoint,
43
+ startX: CGFloat,
44
+ containerWidth: CGFloat,
45
+ edgeInset: CGFloat = 24
46
+ ) -> Bool {
47
+ guard containerWidth > edgeInset * 2,
48
+ startX > edgeInset,
49
+ startX < containerWidth - edgeInset else {
50
+ return false
51
+ }
52
+ return abs(velocity.x) >= 20 && abs(velocity.x) > abs(velocity.y) * 1.15
53
+ }
54
+ }
55
+
56
+ private enum MarqueeState: String {
57
+ case idle
58
+ case running
59
+ case paused
60
+ case reducedMotion = "reducedMotion"
61
+ }
62
+
63
+ @objcMembers public final class MarqueeRenderer: UIView, UIGestureRecognizerDelegate {
64
+ public weak var delegate: MarqueeRendererDelegate?
65
+
66
+ public override class var layerClass: AnyClass { CAReplicatorLayer.self }
67
+
68
+ private var replicatorLayer: CAReplicatorLayer { layer as! CAReplicatorLayer }
69
+ private var contentView: UIView? { subviews.first }
70
+ private var configuration = MarqueeConfiguration()
71
+ private var period: CGFloat = 0
72
+ private var sourceBaseX: CGFloat = 0
73
+ private var isApplicationActive = true
74
+ private var isHolding = false
75
+ private var isDragging = false
76
+ private var panStartOffset: CGFloat = 0
77
+ private var displayLink: CADisplayLink?
78
+ private var lastDisplayTimestamp: CFTimeInterval = 0
79
+ private var inertiaVelocity: CGFloat = 0
80
+ private var isBlendingToAuto = false
81
+ private var blendStartVelocity: CGFloat = 0
82
+ private var blendElapsed: CFTimeInterval = 0
83
+ private var resumeWorkItem: DispatchWorkItem?
84
+ private var notificationTokens: [NSObjectProtocol] = []
85
+ private var contentBoundsObservation: NSKeyValueObservation?
86
+ private var sessionGeneration = 0
87
+ private var lastState: MarqueeState?
88
+ private var lastLayoutWidth: CGFloat = -1
89
+ private var lastContainerWidth: CGFloat = -1
90
+ private var lastReplicationContainerWidth: CGFloat = -1
91
+ private var autoAnimationRestartCount = 0
92
+ private var layoutEmissionCount = 0
93
+
94
+ private lazy var panGesture = UIPanGestureRecognizer(target: self, action: #selector(handlePan(_:)))
95
+ private lazy var holdGesture = UILongPressGestureRecognizer(
96
+ target: self,
97
+ action: #selector(handleHold(_:))
98
+ )
99
+
100
+ public override init(frame: CGRect) {
101
+ super.init(frame: frame)
102
+ setup()
103
+ }
104
+
105
+ public required init?(coder: NSCoder) {
106
+ super.init(coder: coder)
107
+ setup()
108
+ }
109
+
110
+ deinit {
111
+ notificationTokens.forEach(NotificationCenter.default.removeObserver)
112
+ }
113
+
114
+ public func applyConfiguration(_ value: MarqueeConfiguration) {
115
+ sanitizeConfiguration(value)
116
+ if let actualWidth = contentView?.bounds.width, actualWidth > geometryEpsilon {
117
+ value.contentWidth = actualWidth
118
+ }
119
+ let previous = configuration
120
+ let oldOffset = currentPhysicalOffset()
121
+ let geometryChanged = abs(previous.contentWidth - value.contentWidth) > geometryEpsilon
122
+ || abs(previous.spacing - value.spacing) > geometryEpsilon
123
+ || previous.direction != value.direction
124
+ || previous.shortContentMode != value.shortContentMode
125
+ || previous.contentAlignment != value.contentAlignment
126
+ let motionChanged = previous.speed != value.speed
127
+ || previous.active != value.active
128
+ || previous.reduceMotion != value.reduceMotion
129
+ || previous.pauseOnPress != value.pauseOnPress
130
+
131
+ configuration = value
132
+ holdGesture.isEnabled = value.pauseOnPress
133
+ if geometryChanged {
134
+ freezeMotion()
135
+ configureReplication(preservingOffset: oldOffset)
136
+ }
137
+ emitLayoutIfNeeded(force: geometryChanged)
138
+ if !geometryChanged,
139
+ !motionChanged,
140
+ contentView?.layer.animation(forKey: animationKey) != nil {
141
+ emitState(.running)
142
+ return
143
+ }
144
+ evaluateMotion()
145
+ }
146
+
147
+ public func resetForReuse() {
148
+ sessionGeneration += 1
149
+ cancelResume()
150
+ freezeMotion()
151
+ configuration = MarqueeConfiguration()
152
+ period = 0
153
+ sourceBaseX = 0
154
+ isHolding = false
155
+ isDragging = false
156
+ lastState = nil
157
+ lastLayoutWidth = -1
158
+ lastContainerWidth = -1
159
+ lastReplicationContainerWidth = -1
160
+ autoAnimationRestartCount = 0
161
+ layoutEmissionCount = 0
162
+ replicatorLayer.instanceCount = 1
163
+ replicatorLayer.instanceTransform = CATransform3DIdentity
164
+ contentView?.layer.transform = CATransform3DIdentity
165
+ }
166
+
167
+ public func invalidateRenderer() {
168
+ resetForReuse()
169
+ notificationTokens.forEach(NotificationCenter.default.removeObserver)
170
+ notificationTokens.removeAll()
171
+ contentBoundsObservation = nil
172
+ }
173
+
174
+ public override func didAddSubview(_ subview: UIView) {
175
+ super.didAddSubview(subview)
176
+ precondition(subviews.count <= 1, "Marquee accepts exactly one package-owned content container.")
177
+ subview.isUserInteractionEnabled = false
178
+ subview.accessibilityElementsHidden = true
179
+ contentBoundsObservation = subview.observe(\.bounds, options: [.new]) { [weak self, weak subview] _, _ in
180
+ DispatchQueue.main.async {
181
+ guard let self, let subview, subview.superview === self else { return }
182
+ self.synchronizeContentWidth(subview.bounds.width)
183
+ }
184
+ }
185
+ DispatchQueue.main.async { [weak self] in
186
+ guard let self, subview.superview === self else { return }
187
+ self.configureReplication(preservingOffset: self.currentPhysicalOffset())
188
+ self.evaluateMotion()
189
+ }
190
+ }
191
+
192
+ public override func willRemoveSubview(_ subview: UIView) {
193
+ if subview === contentView {
194
+ contentBoundsObservation = nil
195
+ freezeMotion()
196
+ period = 0
197
+ replicatorLayer.instanceCount = 1
198
+ }
199
+ super.willRemoveSubview(subview)
200
+ }
201
+
202
+ public override func layoutSubviews() {
203
+ super.layoutSubviews()
204
+ let containerWidthChanged = abs(bounds.width - lastReplicationContainerWidth) > geometryEpsilon
205
+ if containerWidthChanged {
206
+ let offset = currentPhysicalOffset()
207
+ freezeMotion()
208
+ lastReplicationContainerWidth = bounds.width
209
+ configureReplication(preservingOffset: offset)
210
+ }
211
+ emitLayoutIfNeeded()
212
+ if containerWidthChanged { evaluateMotion() }
213
+ }
214
+
215
+ public override func didMoveToWindow() {
216
+ super.didMoveToWindow()
217
+ if window == nil {
218
+ cancelResume()
219
+ freezeMotion()
220
+ emitCurrentState()
221
+ return
222
+ }
223
+ evaluateMotion()
224
+ }
225
+
226
+ public func gestureRecognizer(
227
+ _ gestureRecognizer: UIGestureRecognizer,
228
+ shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer
229
+ ) -> Bool {
230
+ gestureRecognizer === holdGesture || otherGestureRecognizer === holdGesture
231
+ }
232
+
233
+ public override func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool {
234
+ guard gestureRecognizer === panGesture else { return true }
235
+ let velocity = panGesture.velocity(in: self)
236
+ return MarqueeMath.shouldBeginPan(
237
+ velocity: velocity,
238
+ startX: panGesture.location(in: self).x,
239
+ containerWidth: bounds.width
240
+ )
241
+ }
242
+
243
+ private var geometryEpsilon: CGFloat {
244
+ 0.5 / max(1, window?.screen.scale ?? UIScreen.main.scale)
245
+ }
246
+
247
+ private var directionSign: CGFloat {
248
+ configuration.direction == "right" ? 1 : -1
249
+ }
250
+
251
+ private var effectiveReduceMotion: Bool {
252
+ configuration.reduceMotion || UIAccessibility.isReduceMotionEnabled
253
+ }
254
+
255
+ private var shouldRepeatShortContent: Bool {
256
+ configuration.shortContentMode == "repeat"
257
+ }
258
+
259
+ private var canScrollGeometry: Bool {
260
+ period > 0 && contentView != nil
261
+ && (configuration.contentWidth > bounds.width + geometryEpsilon || shouldRepeatShortContent)
262
+ }
263
+
264
+ private var shouldAnimate: Bool {
265
+ configuration.active && isApplicationActive && window != nil && !effectiveReduceMotion
266
+ && canScrollGeometry && configuration.speed > 0
267
+ }
268
+
269
+ private func setup() {
270
+ clipsToBounds = true
271
+ isAccessibilityElement = false
272
+ accessibilityElementsHidden = true
273
+ panGesture.delegate = self
274
+ holdGesture.delegate = self
275
+ addGestureRecognizer(panGesture)
276
+ addGestureRecognizer(holdGesture)
277
+ registerNotifications()
278
+ }
279
+
280
+ private func sanitizeConfiguration(_ value: MarqueeConfiguration) {
281
+ value.contentWidth = finiteNonnegative(value.contentWidth, fallback: 0)
282
+ value.spacing = finiteNonnegative(value.spacing, fallback: 0)
283
+ value.speed = finiteNonnegative(value.speed, fallback: 0)
284
+ value.maxFlingVelocity = finiteNonnegative(value.maxFlingVelocity, fallback: 0)
285
+ value.resumeDelay = value.resumeDelay.isFinite ? max(0, value.resumeDelay) : 0
286
+ value.deceleration = value.deceleration.isFinite
287
+ && value.deceleration > 0 && value.deceleration < 1
288
+ ? value.deceleration
289
+ : 0.9985
290
+ value.direction = value.direction == "right" ? "right" : "left"
291
+ value.shortContentMode = value.shortContentMode == "repeat" ? "repeat" : "static"
292
+ value.contentAlignment = ["center", "end"].contains(value.contentAlignment)
293
+ ? value.contentAlignment
294
+ : "start"
295
+ }
296
+
297
+ private func finiteNonnegative(_ value: CGFloat, fallback: CGFloat) -> CGFloat {
298
+ value.isFinite ? max(0, value) : fallback
299
+ }
300
+
301
+ private func synchronizeContentWidth(_ width: CGFloat) {
302
+ guard width.isFinite, width >= 0,
303
+ abs(configuration.contentWidth - width) > geometryEpsilon else {
304
+ return
305
+ }
306
+ let offset = currentPhysicalOffset()
307
+ freezeMotion()
308
+ configuration.contentWidth = width
309
+ configureReplication(preservingOffset: offset)
310
+ emitLayoutIfNeeded(force: true)
311
+ evaluateMotion()
312
+ }
313
+
314
+ private func configureReplication(preservingOffset offset: CGFloat) {
315
+ period = configuration.contentWidth > 0
316
+ ? configuration.contentWidth + configuration.spacing
317
+ : 0
318
+ guard contentView != nil, period > 0 else {
319
+ replicatorLayer.instanceCount = 1
320
+ replicatorLayer.instanceTransform = CATransform3DIdentity
321
+ sourceBaseX = 0
322
+ setContentOffset(0)
323
+ return
324
+ }
325
+
326
+ let needsLoop = configuration.contentWidth > bounds.width + geometryEpsilon
327
+ || shouldRepeatShortContent
328
+ let repeatCount = needsLoop
329
+ ? max(2, Int(ceil(max(0, bounds.width) / period)) + 2)
330
+ : 1
331
+ CATransaction.begin()
332
+ CATransaction.setDisableActions(true)
333
+ replicatorLayer.instanceCount = repeatCount
334
+ if configuration.direction == "right" && needsLoop {
335
+ replicatorLayer.instanceTransform = CATransform3DMakeTranslation(-period, 0, 0)
336
+ sourceBaseX = CGFloat(max(0, repeatCount - 2)) * period
337
+ } else {
338
+ replicatorLayer.instanceTransform = needsLoop
339
+ ? CATransform3DMakeTranslation(period, 0, 0)
340
+ : CATransform3DIdentity
341
+ sourceBaseX = needsLoop ? 0 : staticContentOffset()
342
+ }
343
+ setContentOffset(needsLoop ? offset : 0)
344
+ CATransaction.commit()
345
+ }
346
+
347
+ private func staticContentOffset() -> CGFloat {
348
+ let available = max(0, bounds.width - configuration.contentWidth)
349
+ switch configuration.contentAlignment {
350
+ case "center": return available / 2
351
+ case "end": return available
352
+ default: return 0
353
+ }
354
+ }
355
+
356
+ private func currentPhysicalOffset() -> CGFloat {
357
+ guard period > 0, let contentView else { return 0 }
358
+ let transform = contentView.layer.animation(forKey: animationKey) != nil
359
+ ? contentView.layer.presentation()?.transform ?? contentView.layer.transform
360
+ : contentView.layer.transform
361
+ return directionSign * MarqueeMath.positiveModulo(
362
+ directionSign * (transform.m41 - sourceBaseX),
363
+ modulus: period
364
+ )
365
+ }
366
+
367
+ private func setContentOffset(_ offset: CGFloat) {
368
+ guard let contentView else { return }
369
+ let normalized = canScrollGeometry && period > 0
370
+ ? directionSign * MarqueeMath.positiveModulo(directionSign * offset, modulus: period)
371
+ : 0
372
+ CATransaction.begin()
373
+ CATransaction.setDisableActions(true)
374
+ contentView.layer.transform = CATransform3DMakeTranslation(sourceBaseX + normalized, 0, 0)
375
+ CATransaction.commit()
376
+ }
377
+
378
+ private func startAutoAnimation() {
379
+ guard shouldAnimate, !isHolding, !isDragging, displayLink == nil, let contentView else {
380
+ emitCurrentState()
381
+ return
382
+ }
383
+ let offset = currentPhysicalOffset()
384
+ contentView.layer.removeAnimation(forKey: animationKey)
385
+ setContentOffset(offset)
386
+ let animation = CABasicAnimation(keyPath: "transform.translation.x")
387
+ animation.fromValue = sourceBaseX + offset
388
+ animation.toValue = sourceBaseX + offset + directionSign * period
389
+ animation.duration = CFTimeInterval(period / max(1, configuration.speed))
390
+ animation.repeatCount = .infinity
391
+ animation.timingFunction = CAMediaTimingFunction(name: .linear)
392
+ animation.isRemovedOnCompletion = true
393
+ contentView.layer.add(animation, forKey: animationKey)
394
+ autoAnimationRestartCount += 1
395
+ emitState(.running)
396
+ }
397
+
398
+ private func removeAutoAnimation(preservePresentation: Bool) {
399
+ let offset = preservePresentation ? currentPhysicalOffset() : 0
400
+ contentView?.layer.removeAnimation(forKey: animationKey)
401
+ setContentOffset(offset)
402
+ }
403
+
404
+ private func freezeMotion() {
405
+ stopDisplayLink()
406
+ removeAutoAnimation(preservePresentation: true)
407
+ }
408
+
409
+ private func evaluateMotion() {
410
+ guard !effectiveReduceMotion else {
411
+ cancelResume()
412
+ freezeMotion()
413
+ setContentOffset(0)
414
+ emitState(.reducedMotion)
415
+ return
416
+ }
417
+ guard canScrollGeometry else {
418
+ freezeMotion()
419
+ sourceBaseX = staticContentOffset()
420
+ setContentOffset(0)
421
+ emitState(.idle)
422
+ return
423
+ }
424
+ guard configuration.speed > 0 else {
425
+ freezeMotion()
426
+ emitState(.idle)
427
+ return
428
+ }
429
+ guard configuration.active, isApplicationActive, window != nil else {
430
+ freezeMotion()
431
+ emitState(.paused)
432
+ return
433
+ }
434
+ guard !isHolding, !isDragging else {
435
+ emitState(.paused)
436
+ return
437
+ }
438
+ startAutoAnimation()
439
+ }
440
+
441
+ @objc private func handleHold(_ gesture: UILongPressGestureRecognizer) {
442
+ guard configuration.pauseOnPress, canScrollGeometry, !effectiveReduceMotion else { return }
443
+ switch gesture.state {
444
+ case .began:
445
+ cancelResume()
446
+ isHolding = true
447
+ freezeMotion()
448
+ emitState(.paused)
449
+ case .ended, .cancelled, .failed:
450
+ isHolding = false
451
+ if !isDragging { scheduleResume() }
452
+ default:
453
+ break
454
+ }
455
+ }
456
+
457
+ @objc private func handlePan(_ gesture: UIPanGestureRecognizer) {
458
+ guard canScrollGeometry, !effectiveReduceMotion else { return }
459
+ switch gesture.state {
460
+ case .began:
461
+ cancelResume()
462
+ isDragging = true
463
+ freezeMotion()
464
+ panStartOffset = currentPhysicalOffset()
465
+ emitState(.paused)
466
+ case .changed:
467
+ setContentOffset(panStartOffset + gesture.translation(in: self).x)
468
+ case .ended:
469
+ isDragging = false
470
+ isHolding = false
471
+ beginInertia(velocity: gesture.velocity(in: self).x)
472
+ case .cancelled, .failed:
473
+ isDragging = false
474
+ isHolding = false
475
+ scheduleResume()
476
+ default:
477
+ break
478
+ }
479
+ }
480
+
481
+ private func beginInertia(velocity: CGFloat) {
482
+ let maximum = max(0, configuration.maxFlingVelocity)
483
+ let clampedVelocity = min(maximum, max(-maximum, velocity))
484
+ guard shouldAnimate else {
485
+ scheduleResume()
486
+ return
487
+ }
488
+ stopDisplayLink()
489
+ inertiaVelocity = clampedVelocity
490
+ isBlendingToAuto = false
491
+ blendElapsed = 0
492
+ lastDisplayTimestamp = 0
493
+ if abs(inertiaVelocity) < 8 {
494
+ blendStartVelocity = inertiaVelocity
495
+ isBlendingToAuto = true
496
+ }
497
+ let link = CADisplayLink(target: self, selector: #selector(stepInertia(_:)))
498
+ displayLink = link
499
+ link.add(to: .main, forMode: .common)
500
+ emitState(.running)
501
+ }
502
+
503
+ @objc private func stepInertia(_ link: CADisplayLink) {
504
+ if lastDisplayTimestamp == 0 {
505
+ lastDisplayTimestamp = link.timestamp
506
+ return
507
+ }
508
+ let elapsed = min(0.05, max(0, link.timestamp - lastDisplayTimestamp))
509
+ lastDisplayTimestamp = link.timestamp
510
+ if isBlendingToAuto {
511
+ blendElapsed = min(autoBlendDuration, blendElapsed + elapsed)
512
+ let velocity = MarqueeMath.blendedVelocity(
513
+ from: blendStartVelocity,
514
+ to: directionSign * configuration.speed,
515
+ progress: blendElapsed / autoBlendDuration
516
+ )
517
+ setContentOffset(currentPhysicalOffset() + velocity * elapsed)
518
+ if blendElapsed >= autoBlendDuration {
519
+ stopDisplayLink()
520
+ startAutoAnimation()
521
+ }
522
+ return
523
+ }
524
+ setContentOffset(currentPhysicalOffset() + inertiaVelocity * elapsed)
525
+ inertiaVelocity *= pow(configuration.deceleration, elapsed * 1_000)
526
+ if abs(inertiaVelocity) < max(8, configuration.speed * 2) {
527
+ blendStartVelocity = inertiaVelocity
528
+ blendElapsed = 0
529
+ isBlendingToAuto = true
530
+ }
531
+ }
532
+
533
+ private func stopDisplayLink() {
534
+ displayLink?.invalidate()
535
+ displayLink = nil
536
+ lastDisplayTimestamp = 0
537
+ inertiaVelocity = 0
538
+ isBlendingToAuto = false
539
+ blendStartVelocity = 0
540
+ blendElapsed = 0
541
+ }
542
+
543
+ private func scheduleResume() {
544
+ cancelResume()
545
+ let generation = sessionGeneration
546
+ let work = DispatchWorkItem { [weak self] in
547
+ guard let self, self.sessionGeneration == generation else { return }
548
+ self.evaluateMotion()
549
+ }
550
+ resumeWorkItem = work
551
+ DispatchQueue.main.asyncAfter(
552
+ deadline: .now() + max(0, configuration.resumeDelay),
553
+ execute: work
554
+ )
555
+ }
556
+
557
+ private func cancelResume() {
558
+ resumeWorkItem?.cancel()
559
+ resumeWorkItem = nil
560
+ }
561
+
562
+ private func emitCurrentState() {
563
+ if effectiveReduceMotion {
564
+ emitState(.reducedMotion)
565
+ return
566
+ }
567
+ if !canScrollGeometry || configuration.speed <= 0 {
568
+ emitState(.idle)
569
+ return
570
+ }
571
+ if !configuration.active || !isApplicationActive || window == nil {
572
+ emitState(.paused)
573
+ return
574
+ }
575
+ emitState(isHolding || isDragging ? .paused : .running)
576
+ }
577
+
578
+ private func emitState(_ state: MarqueeState) {
579
+ guard state != lastState else { return }
580
+ lastState = state
581
+ delegate?.marqueeRenderer(self, didChangeState: state.rawValue)
582
+ }
583
+
584
+ private func emitLayoutIfNeeded(force: Bool = false) {
585
+ let containerWidth = bounds.width
586
+ guard force || abs(configuration.contentWidth - lastLayoutWidth) > geometryEpsilon
587
+ || abs(containerWidth - lastContainerWidth) > geometryEpsilon else {
588
+ return
589
+ }
590
+ lastLayoutWidth = configuration.contentWidth
591
+ lastContainerWidth = containerWidth
592
+ layoutEmissionCount += 1
593
+ delegate?.marqueeRenderer(
594
+ self,
595
+ didLayoutContentWidth: configuration.contentWidth,
596
+ containerWidth: containerWidth
597
+ )
598
+ }
599
+
600
+ private func registerNotifications() {
601
+ let center = NotificationCenter.default
602
+ notificationTokens.append(center.addObserver(
603
+ forName: UIApplication.didEnterBackgroundNotification,
604
+ object: nil,
605
+ queue: .main
606
+ ) { [weak self] _ in
607
+ self?.isApplicationActive = false
608
+ self?.cancelResume()
609
+ self?.freezeMotion()
610
+ self?.emitCurrentState()
611
+ })
612
+ notificationTokens.append(center.addObserver(
613
+ forName: UIApplication.didBecomeActiveNotification,
614
+ object: nil,
615
+ queue: .main
616
+ ) { [weak self] _ in
617
+ self?.isApplicationActive = true
618
+ self?.evaluateMotion()
619
+ })
620
+ notificationTokens.append(center.addObserver(
621
+ forName: UIAccessibility.reduceMotionStatusDidChangeNotification,
622
+ object: nil,
623
+ queue: .main
624
+ ) { [weak self] _ in
625
+ self?.evaluateMotion()
626
+ })
627
+ }
628
+
629
+ var testingContentWidth: CGFloat { configuration.contentWidth }
630
+ var testingState: String? { lastState?.rawValue }
631
+ var testingHasAutoAnimation: Bool {
632
+ contentView?.layer.animation(forKey: animationKey) != nil
633
+ }
634
+ var testingAutoAnimationRestartCount: Int { autoAnimationRestartCount }
635
+ var testingPhysicalOffset: CGFloat { currentPhysicalOffset() }
636
+ var testingInertiaVelocity: CGFloat { inertiaVelocity }
637
+ var testingLayoutEmissionCount: Int { layoutEmissionCount }
638
+ var testingPeriod: CGFloat { period }
639
+ var testingReplicaOrigins: [CGFloat] {
640
+ (0..<replicatorLayer.instanceCount).map { instance in
641
+ sourceBaseX + CGFloat(instance) * replicatorLayer.instanceTransform.m41
642
+ }
643
+ }
644
+ func testingBeginInertia(_ velocity: CGFloat) { beginInertia(velocity: velocity) }
645
+ func testingSetOffset(_ offset: CGFloat) {
646
+ contentView?.layer.removeAnimation(forKey: animationKey)
647
+ setContentOffset(offset)
648
+ }
649
+
650
+ private let autoBlendDuration: CFTimeInterval = 0.35
651
+ private let animationKey = "marquee.auto"
652
+ }
@@ -0,0 +1,14 @@
1
+ #import <React/RCTViewComponentView.h>
2
+ #import <UIKit/UIKit.h>
3
+
4
+ #ifndef RNMarqueeView_h
5
+ #define RNMarqueeView_h
6
+
7
+ NS_ASSUME_NONNULL_BEGIN
8
+
9
+ @interface RNMarqueeView : RCTViewComponentView
10
+ @end
11
+
12
+ NS_ASSUME_NONNULL_END
13
+
14
+ #endif