Rotate the image by 180 degrees

Swift 3

UIView.animate(withDuration:0.1, animations: {
     self.arrowIcon.transform = CGAffineTransform(rotationAngle: CGFloat(Double.pi))
})

To rotate an image you could use this snippet:

UIView.animateWithDuration(2, animations: {
     self.imageView.transform = CGAffineTransformMakeRotation(CGFloat(M_PI))
})

You can adjust the animation seconds (currently 2).

To set the image back again use the following code:

UIView.animateWithDuration(2, animations: {
     self.imageV.transform = CGAffineTransform.identity
})

Swift 4.x version:

Rotate:

UIView.animate(withDuration: 2) {
    self.imageView.transform = CGAffineTransform(rotationAngle: .pi)
}

Set the image to it´s normal state:

UIView.animate(withDuration: 2) {
    self.imageView.transform = CGAffineTransform.identity
}

First of all, if you want to rotate 180 degrees, that has to translate to radians. 180 degrees in radians is pi. 360 degrees would be 2 * pi. 180 * pi would make your animation spin around 90 times in one second and end up with the original orientation.

You can do something like this,

let rotationAnimation = CABasicAnimation(keyPath: "transform.rotation")
rotationAnimation.fromValue = 0.0
rotationAnimation.toValue = M_PI
rotationAnimation.duration = 1.0

self.arrowImageView.layer.addAnimation(rotationAnimation, forKey: nil)

hope this will help :)

Tags:

Ios

Swift