My app shows an animation of rotating fan blades in front of the fan body and background The image of the blades are in a UIImageView and I have no problem making that view rotate nicely. But I'd like to add some detail by applying a drop shadow to the spinning blades.
The code below works fine, but the shadow rotates along with blades and of course that doesn't look realistic. How can I make the light source hold still and recalculate the shadow for each frame of animation?
func startRotating(_ duration: Double = 1) {
let kAnimationKey = "rotation"
if self.layer.animation(forKey: kAnimationKey) == nil {
self.layer.shadowColor = UIColor.black.cgColor
self.layer.shadowOpacity = 0.8
self.layer.shadowOffset = CGSize(width: 1, height: 1)
self.layer.shadowRadius = 2
let animate = CABasicAnimation(keyPath: "transform.rotation")
animate.duration = duration
animate.repeatCount = Float.infinity
animate.fromValue = 0.0
animate.toValue = Float(.pi * 2.0)
self.layer.add(animate, forKey: kAnimationKey)
}
}