UIView.animate(withDuration:2.0, animations: {
self.imageView.transform = CGAffineTransform(rotationAngle: (270 * CGFloat(2 * Double.pi)) / 360.0)
})
This is animating anti clockwise i want the animation clockwise.
UIView.animate(withDuration:2.0, animations: {
self.imageView.transform = CGAffineTransform(rotationAngle: (270 * CGFloat(2 * Double.pi)) / 360.0)
})
This is animating anti clockwise i want the animation clockwise.
Explanation
The problem with rotation is that it finds the shortest path to finish. So when your view is rotated by 0 degrees at the beginning, the shortest path to 270 degrees is rotate the view by -90 degrees.
You have to combine TWO animations to do this.
First: Rotate view by 180 + 1 degrees (Plus 1 tells the animation that you want to rotate clockwise - shorter path)
Second: Rotate view by 90 degrees
In code:
Let's say you have an UIView called rotateView.
The .identity checks if your rotateView is at its 'start position'.
To rotate by 270 degrees clockwise...
Core Animation will always take the shortest route to make the rotation work. So, if your object is straight and you rotate to 90 degrees (radians: pi/2), it will rotate clockwise. If your object is straight and you rotate to 270 degrees (radians: pi + pi/2 = 3pi/2) it will rotate counter-clockwise because it's the smallest possible animation.
So we first need to rotate by pi radians (from 0 -> pi, 0 -> 180) then rotate by another pi/2 radians (pi -> 3pi/2, 180 -> 270) to perform the full rotation.
Notes :
In iOS, coordinate system is flipped. So you go clockwise as your degree gains. It means that passing 270° will give you an angle, equivalent to 90° in standard coordinate system. Keep that in mind and provide needed angle accordingly.
Consider the following approach.
1) Handy extension for angle
2) Rotate to any angle with
CABasicAnimation
:Usage:
Passing negative value will rotate counter-clockwise.