Skip to content

Commit 026f57b

Browse files
committed
Optimize dimming animation with keyframe animation
Replace separate fade-in and fade-out animations with a single CAKeyframeAnimation for better performance and smoother animation.
1 parent 36015e7 commit 026f57b

File tree

1 file changed

+7
-19
lines changed

1 file changed

+7
-19
lines changed

Signal/ConversationView/CellViews/CVColorOrGradientView.swift

Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -337,25 +337,13 @@ public class CVColorOrGradientView: ManualLayoutViewWithLayer {
337337

338338
dimmerLayer.removeAllAnimations()
339339

340-
// Animate fade-in.
341-
let fadeIn = CABasicAnimation(keyPath: #keyPath(CALayer.opacity))
342-
fadeIn.fromValue = 0
343-
fadeIn.toValue = 1
344-
fadeIn.duration = stepDuration
345-
fadeIn.fillMode = .forwards
346-
fadeIn.isRemovedOnCompletion = false
347-
dimmerLayer.add(fadeIn, forKey: "fadeIn")
348-
349-
// Schedule fade-out after delay.
350-
DispatchQueue.main.asyncAfter(deadline: .now() + 2 * stepDuration) {
351-
let fadeOut = CABasicAnimation(keyPath: #keyPath(CALayer.opacity))
352-
fadeOut.fromValue = 1
353-
fadeOut.toValue = 0
354-
fadeOut.duration = stepDuration
355-
fadeOut.fillMode = .forwards
356-
fadeOut.isRemovedOnCompletion = false
357-
dimmerLayer.add(fadeOut, forKey: "fadeOut")
358-
}
340+
let animation = CAKeyframeAnimation(keyPath: #keyPath(CALayer.opacity))
341+
animation.values = [0, 1, 1, 0]
342+
animation.keyTimes = [0, 1.0 / 3.0, 2.0 / 3.0, 1].map { NSNumber(value: $0) }
343+
animation.duration = stepDuration * 3
344+
animation.fillMode = .forwards
345+
animation.isRemovedOnCompletion = false
346+
dimmerLayer.add(animation, forKey: "dimming")
359347
}
360348
}
361349

0 commit comments

Comments
 (0)