Skip to content

Instantly share code, notes, and snippets.

@edudnyk
Created July 30, 2019 00:08
Show Gist options
  • Save edudnyk/320b7dbec9a8f591a740377477c8b504 to your computer and use it in GitHub Desktop.
Save edudnyk/320b7dbec9a8f591a740377477c8b504 to your computer and use it in GitHub Desktop.
private static func animationsForAlphaSwap(forKeyPath keyPath: String,
fromValue: Any?,
toValue: Any?)->[CABasicAnimation] {
let duration = CATransaction.animationDuration()
guard duration > 0 else { return [CABasicAnimation]() }
let alphaSwapDuration = min(LabelLayerFromToAlphaSwapAnimationDuration, 0.5 * duration)
let alphaPersistDuration = (duration - alphaSwapDuration) / 2.0
let persistFromAlphaAnimation = CABasicAnimation(forKeyPath: keyPath,
fromValue: fromValue,
toValue: fromValue,
duration: alphaPersistDuration)
let swapAlphaAnimation = CABasicAnimation(forKeyPath: keyPath,
fromValue: fromValue,
toValue: toValue,
duration:alphaSwapDuration)
let persistToAlphaAnimation = CABasicAnimation(forKeyPath: keyPath,
fromValue: toValue,
toValue: toValue,
duration:alphaPersistDuration)
persistFromAlphaAnimation.beginTime = 0
swapAlphaAnimation.beginTime = alphaPersistDuration
persistToAlphaAnimation.beginTime = alphaSwapDuration + alphaPersistDuration
return [persistFromAlphaAnimation,
swapAlphaAnimation,
persistToAlphaAnimation]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment