I'm working on a Swift animation and I'm currently trying to refactor the animation code since my I'm trying to use the CAkeyframeAnimation but now I struggle with how to convert UIView animate method to CAKeyframeAnimation.
This is the one using UIView animate function.
class myCustomView: UIView {
func showPointOfInterest(at point: CGPoint, hideViewAfterAnimation: Bool) {
if isPreviousAnimationPresenting() { layer.removeAllAnimations() }
center = point
transform = CGAffineTransform(scaleX: 1.5, y: 1.5)
self.alpha = 1
UIView.animate(withDuration: 0.3, delay: 0, options: [.curveEaseInOut]) {
self.transform = CGAffineTransform(scaleX: 0.9, y: 0.9)
} completion: { animated in
if !animated {
return
} else {
UIView.animate(withDuration: 0.1, delay: 0, options: [.curveEaseInOut]) {
self.transform = .identity
} completion: { animated in
if !animated {
return
} else {
UIView.animate(withDuration: 1.5, delay: 0, options: [.curveEaseInOut]) {
self.alpha = 0.5
} completion: { animated in
if !animated {
return
} else {
UIView.animate(withDuration: 0.5, delay: 0, options: [.curveEaseInOut]) {
self.alpha = hideViewAfterAnimation ? 0 : 0.5
}
}
}
}
}
}
}
}
Generally, these are the things I struggled with...
I'm not sure what is the keyPath is gonna be. In my UIView animate function, I change the transform and alpha value. I saw examples put string for the keyPath, like
CAKeyframeAnimation(keyPath: "position.x"), so what should I put for my keyPath?Since I want to change the size of the view by using CGAffineTransform in the UIView animate function, do I put
CGAffineTransform(scaleX:, y: )in thetransformKeyframeAnimation.values?func showPointOfInterest(at point: CGPoint, hideViewAfterAnimation: Bool) { if isPreviousAnimationPresenting() { layer.removeAllAnimations() } center = point transform = CGAffineTransform(scaleX: 1.5, y: 1.5) alpha = 1 let transformKeyframeAnimation = CAKeyframeAnimation(keyPath: "transform") transformKeyframeAnimation.values = [CGAffineTransform(scaleX: 0.9, y: 0.9), CGAffineTransform(scaleX: 1, y: 1)] transformKeyframeAnimation.duration = 1.0 transformKeyframeAnimation.isRemovedOnCompletion = hideViewAfterAnimation self.layer.add(transformKeyframeAnimation, forKey: "transform") }