John John - 6 months ago 10
iOS Question

Why UIImageView rotates only once?

On button click I'm using this code line to rotate my UIImageView. But it works only once. How can I make it infinity times in every button click?

self.imageView.transform = CGAffineTransformMakeRotation(CGFloat(M_PI_2))

Answer

You must use CGAffineTransformRotate instead of CGAffineTransformMakeRotation:

func rotateImageView() {   
     UIView.animateWithDuration(1, delay: 0, options: UIViewAnimationOptions.CurveLinear, animations: { () -> Void in
                self.imageView.transform = CGAffineTransformRotate(self.imageView.transform, CGFloat(M_PI_2))
                }) { (finished) -> Void in
                    if finished {
                        self.rotateImageView()
                    }
        }
}

If you want to stop it you can call:

self.imageView.layer.removeAllAnimations()