Reputation: 7605
I have defined a CAKeyframeAnimation
to rotate a CALayer
along z axis. For the animation to work, I've used the values
, keyTimes
and duration
properties of the animation object.
Below is what I've:
let rotationInDegrees: CGFloat = 7 * 360.0 // 7 times full rotation
let transformationAnimation = CAKeyframeAnimation(keyPath: "transform.rotation.z")
transformationAnimation.values = [0, rotationInDegrees * (CGFloat.pi / 180)]
transformationAnimation.keyTimes = [0, 1]
transformationAnimation.duration = 15
myLayer.add(transformationAnimation, forKey:"transformationAnimation")
Now I need to perform some other tasks when the layer rotates to every x
degrees. I can't find a way for my cause.
What do I need to do to be notified of every x
degrees change in the rotation?
I've tried KVO for value observation like:
token = transformationAnimation.observe(\.values) { (anim, values) in
print(anim.values)
}
The observation block never gets triggered.
Also tried similar approach answered in this question. But seems that the solutions provided there only work for "progress"
key not for "transform.rotation.z"
(also tried with "transform"
/ "transform.rotation"
key names but they don't also work. Even "transform"
emits values for progress ranging 0.0 ~ 1.0).
None of the above tries seem to work.
Upvotes: 0
Views: 189
Reputation: 1077
I think there can be one approach is using Timer. KVO is not available for these properties.
You can start the timer with the animation begin and continuously check for the status of the degree of rotation. Below is the code -
import PlaygroundSupport
import UIKit
class Animation: NSObject, CAAnimationDelegate {
var timer: Timer?
func doAnimation() {
let view = UIView.init(frame: CGRect(x: 0, y: 0, width: 200, height: 200))
view.backgroundColor = UIColor.red
let rotationInDegrees: CGFloat = 7 * 360.0 // 7 times full rotation
let transformationAnimation = CAKeyframeAnimation(keyPath: "transform.rotation.z")
transformationAnimation.values = [0, rotationInDegrees * (CGFloat.pi / 180)]
transformationAnimation.keyTimes = [0, 1]
transformationAnimation.duration = 15
transformationAnimation.delegate = self
timer = Timer.scheduledTimer(timeInterval: 1.0,
target: self,
selector: #selector(eventWith(timer:)),
userInfo: [ "animation" : transformationAnimation],
repeats: true)
view.layer.add(transformationAnimation, forKey:"transformationAnimation")
PlaygroundPage.current.needsIndefiniteExecution = true
PlaygroundPage.current.liveView = view
}
func animationDidStart(_ anim: CAAnimation) {
timer?.fire()
}
func animationDidStop(_ anim: CAAnimation, finished flag: Bool) {
timer?.invalidate()
timer = nil
}
// Timer expects @objc selector
@objc func eventWith(timer: Timer!) {
if let animation = timer.userInfo as? [String: CAAnimation] {
print(animation.values)
}
}
}
let ballAnimation = Animation()
ballAnimation.doAnimation()
Upvotes: 0