@@ -19,15 +19,22 @@ public final class StreamShimmerView: UIView {
1919 private var gradientColor : UIColor = UIColor ( white: 1 , alpha: defaultHighlightAlpha)
2020 private var enabled = false
2121 private var lastAnimatedSize : CGSize = . zero
22+ private var isAppActive = true
2223
2324 public override init ( frame: CGRect ) {
2425 super. init ( frame: frame)
2526 setupLayers ( )
27+ setupLifecycleObservers ( )
2628 }
2729
2830 public required init ? ( coder: NSCoder ) {
2931 super. init ( coder: coder)
3032 setupLayers ( )
33+ setupLifecycleObservers ( )
34+ }
35+
36+ deinit {
37+ NotificationCenter . default. removeObserver ( self )
3138 }
3239
3340 public override func layoutSubviews( ) {
@@ -82,6 +89,33 @@ public final class StreamShimmerView: UIView {
8289 layer. addSublayer ( shimmerLayer)
8390 }
8491
92+ private func setupLifecycleObservers( ) {
93+ NotificationCenter . default. addObserver (
94+ self ,
95+ selector: #selector( handleWillEnterForeground) ,
96+ name: UIApplication . willEnterForegroundNotification,
97+ object: nil
98+ )
99+ NotificationCenter . default. addObserver (
100+ self ,
101+ selector: #selector( handleDidEnterBackground) ,
102+ name: UIApplication . didEnterBackgroundNotification,
103+ object: nil
104+ )
105+ }
106+
107+ @objc
108+ private func handleWillEnterForeground( ) {
109+ isAppActive = true
110+ updateLayersForCurrentState ( )
111+ }
112+
113+ @objc
114+ private func handleDidEnterBackground( ) {
115+ isAppActive = false
116+ stopAnimation ( )
117+ }
118+
85119 private func updateLayersForCurrentState( ) {
86120 let bounds = self . bounds
87121 guard !bounds. isEmpty else {
@@ -117,7 +151,7 @@ public final class StreamShimmerView: UIView {
117151 }
118152
119153 private func updateShimmerAnimation( for bounds: CGRect ) {
120- guard enabled, window != nil , bounds. width > 0 , bounds. height > 0 else {
154+ guard enabled, isAppActive , window != nil , bounds. width > 0 , bounds. height > 0 else {
121155 stopAnimation ( )
122156 return
123157 }
0 commit comments