I am using this code to rotate a view 360 degrees infinitely. But my view is not rotating:
func rotateImageView() {UIView.animate(withDuration: 3.0, delay: 0, options: [.repeat, .curveLinear], animations: {self.vinylView.transform = CGAffineTransform(rotationAngle: .pi * 2)})}
How to fix it?
Best Answer
Substitute pi * with /, delete repeat, add completion and recall the function like this:
private func rotateImageView() {UIView.animate(withDuration: 3, delay: 0, options: .curveLinear, animations: {self.vinylView.transform = self.vinylView.transform.rotated(by: .pi / 2)}) { (finished) inif finished {self.rotateImageView()}}}
Solution using CABasicAnimation
// Rotate vinvlViewvinylView.layer.add(CABasicAnimation.rotation, forKey: nil)extension CABasicAnimation {static let rotation : CABasicAnimation = {let animation = CABasicAnimation(keyPath: "transform.rotation.z")animation.repeatCount = .infinity // Rotate a view 360 degrees infinitelyanimation.fromValue = 0animation.toValue = CGFloat.pi * 2animation.duration = 3.0return animation}()}