@@ -47,26 +47,12 @@ class VHSGlitchAnimation: BaseWindowAnimation, WindowAnimation {
4747 }
4848 layer. add ( blurAnim, forKey: " blur " )
4949
50- let posterAnim = createAnimation ( keyPath: " filters.CIColorPosterize.inputLevels " , from: 8.0 , to: 256.0 , duration: duration)
51- posterAnim. fillMode = . forwards
52- posterAnim. isRemovedOnCompletion = false
53- layer. add ( posterAnim, forKey: " level " )
54-
55- let motionAnim = createAnimation ( keyPath: " filters.CIMotionBlur.inputRadius " , from: 10.0 , to: 0.0 , duration: duration)
56- motionAnim. fillMode = . forwards
57- motionAnim. isRemovedOnCompletion = false
58- layer. add ( motionAnim, forKey: " motion " )
59-
50+ addForwardAnimation ( keyPath: " filters.CIColorPosterize.inputLevels " , from: 8.0 , to: 256.0 , duration: duration, to: layer)
51+ addForwardAnimation ( keyPath: " filters.CIMotionBlur.inputRadius " , from: 10.0 , to: 0.0 , duration: duration, to: layer)
52+
6053 // Animate Overlays Opacity
61- let scanlineAnim = createAnimation ( keyPath: " opacity " , from: 0.3 , to: 0.0 , duration: duration)
62- scanlineAnim. fillMode = . forwards
63- scanlineAnim. isRemovedOnCompletion = false
64- scanline. add ( scanlineAnim, forKey: " opacity " )
65-
66- let noiseAnim = createAnimation ( keyPath: " opacity " , from: 0.15 , to: 0.0 , duration: duration)
67- noiseAnim. fillMode = . forwards
68- noiseAnim. isRemovedOnCompletion = false
69- noise. add ( noiseAnim, forKey: " opacity " )
54+ addForwardAnimation ( keyPath: " opacity " , from: 0.3 , to: 0.0 , duration: duration, to: scanline)
55+ addForwardAnimation ( keyPath: " opacity " , from: 0.15 , to: 0.0 , duration: duration, to: noise)
7056
7157 scanline. opacity = 0.0
7258 noise. opacity = 0.0
@@ -115,28 +101,15 @@ class VHSGlitchAnimation: BaseWindowAnimation, WindowAnimation {
115101 noise. removeFromSuperlayer ( )
116102 completion ? ( )
117103 }
104+
118105 layer. add ( blurAnim, forKey: " blur " )
119106
120- let posterAnim = createAnimation ( keyPath: " filters.CIColorPosterize.inputLevels " , from: 256.0 , to: 8.0 , duration: duration)
121- posterAnim. fillMode = . forwards
122- posterAnim. isRemovedOnCompletion = false
123- layer. add ( posterAnim, forKey: " level " )
124-
125- let motionAnim = createAnimation ( keyPath: " filters.CIMotionBlur.inputRadius " , from: 0.0 , to: 10.0 , duration: duration)
126- motionAnim. fillMode = . forwards
127- motionAnim. isRemovedOnCompletion = false
128- layer. add ( motionAnim, forKey: " motion " )
129-
107+ addForwardAnimation ( keyPath: " filters.CIColorPosterize.inputLevels " , from: 256.0 , to: 8.0 , duration: duration, to: layer)
108+ addForwardAnimation ( keyPath: " filters.CIMotionBlur.inputRadius " , from: 0.0 , to: 10.0 , duration: duration, to: layer)
109+
130110 // Animate Overlays Opacity
131- let scanlineAnim = createAnimation ( keyPath: " opacity " , from: 0.0 , to: 0.3 , duration: duration)
132- scanlineAnim. fillMode = . forwards
133- scanlineAnim. isRemovedOnCompletion = false
134- scanline. add ( scanlineAnim, forKey: " opacity " )
135-
136- let noiseAnim = createAnimation ( keyPath: " opacity " , from: 0.0 , to: 0.15 , duration: duration)
137- noiseAnim. fillMode = . forwards
138- noiseAnim. isRemovedOnCompletion = false
139- noise. add ( noiseAnim, forKey: " opacity " )
111+ addForwardAnimation ( keyPath: " opacity " , from: 0.0 , to: 0.3 , duration: duration, to: scanline)
112+ addForwardAnimation ( keyPath: " opacity " , from: 0.0 , to: 0.15 , duration: duration, to: noise)
140113
141114 scanline. opacity = 0.3
142115 noise. opacity = 0.15
@@ -145,4 +118,21 @@ class VHSGlitchAnimation: BaseWindowAnimation, WindowAnimation {
145118
146119 animateAlpha ( contentView: contentView, from: 1.0 , to: 0.0 , duration: duration)
147120 }
121+
122+ // MARK: - Private
123+
124+ private func addForwardAnimation( keyPath: String ,
125+ from fromValue: Any ,
126+ to toValue: Any ,
127+ duration: TimeInterval ,
128+ to targetLayer: CALayer ) {
129+ let anim = createAnimation ( keyPath: keyPath,
130+ from: fromValue,
131+ to: toValue,
132+ duration: duration)
133+
134+ anim. fillMode = . forwards
135+ anim. isRemovedOnCompletion = false
136+ targetLayer. add ( anim, forKey: nil )
137+ }
148138}
0 commit comments