@@ -13,8 +13,8 @@ public final class StreamShimmerView: UIView {
1313 private static let midHighlightAlpha : CGFloat = 0.48
1414 private static let innerHighlightAlpha : CGFloat = 0.72
1515 private static let defaultHighlightAlpha : CGFloat = 0.35
16+ private static let defaultShimmerDuration : CFTimeInterval = 1.2
1617 private static let shimmerStripWidthRatio : CGFloat = 1.25
17- private static let shimmerDuration : CFTimeInterval = 1.2
1818 private static let shimmerAnimationKey = " stream_shimmer_translate_x "
1919
2020 private let baseLayer = CALayer ( )
@@ -23,6 +23,8 @@ public final class StreamShimmerView: UIView {
2323 private var baseColor : UIColor = UIColor ( white: 1 , alpha: 0 )
2424 private var gradientColor : UIColor = UIColor ( white: 1 , alpha: defaultHighlightAlpha)
2525 private var enabled = false
26+ private var shimmerDuration : CFTimeInterval = defaultShimmerDuration
27+ private var lastAnimatedDuration : CFTimeInterval = 0
2628 private var lastAnimatedSize : CGSize = . zero
2729 private var isAppActive = true
2830
@@ -74,16 +76,19 @@ public final class StreamShimmerView: UIView {
7476 public func apply(
7577 baseColor: UIColor ,
7678 gradientColor: UIColor ,
79+ durationMilliseconds: Double ,
7780 enabled: Bool
7881 ) {
7982 self . baseColor = baseColor
8083 self . gradientColor = gradientColor
84+ shimmerDuration = Self . normalizedDuration ( milliseconds: durationMilliseconds)
8185 self . enabled = enabled
8286 updateLayersForCurrentState ( )
8387 }
8488
8589 public func stopAnimation( ) {
8690 shimmerLayer. removeAnimation ( forKey: Self . shimmerAnimationKey)
91+ lastAnimatedDuration = 0
8792 lastAnimatedSize = . zero
8893 }
8994
@@ -172,7 +177,10 @@ public final class StreamShimmerView: UIView {
172177 }
173178
174179 // If an animation already exists for the same size, keep it running instead of restarting.
175- if shimmerLayer. animation ( forKey: Self . shimmerAnimationKey) != nil , lastAnimatedSize == bounds. size {
180+ if shimmerLayer. animation ( forKey: Self . shimmerAnimationKey) != nil ,
181+ lastAnimatedSize == bounds. size,
182+ lastAnimatedDuration == shimmerDuration
183+ {
176184 return
177185 }
178186
@@ -183,14 +191,20 @@ public final class StreamShimmerView: UIView {
183191 let animation = CABasicAnimation ( keyPath: " transform.translation.x " )
184192 animation. fromValue = 0
185193 animation. toValue = bounds. width + shimmerWidth
186- animation. duration = Self . shimmerDuration
194+ animation. duration = shimmerDuration
187195 animation. repeatCount = . infinity
188196 animation. timingFunction = CAMediaTimingFunction ( name: . linear)
189197 animation. isRemovedOnCompletion = true
190198 shimmerLayer. add ( animation, forKey: Self . shimmerAnimationKey)
199+ lastAnimatedDuration = shimmerDuration
191200 lastAnimatedSize = bounds. size
192201 }
193202
203+ private static func normalizedDuration( milliseconds: Double ) -> CFTimeInterval {
204+ guard milliseconds > 0 else { return defaultShimmerDuration }
205+ return milliseconds / 1000
206+ }
207+
194208 private func color( _ color: UIColor , alphaFactor: CGFloat ) -> UIColor {
195209 // Preserve the resolved color channels and shape only alpha for smooth highlight falloff.
196210 let resolvedColor = color. resolvedColor ( with: traitCollection)
0 commit comments