I'm using CGAffineTransformMakeRotation
to rotate a UIView
inside an animation block, and I want to rotate it counterclockwise for 180º
.
However when I put
myView.animateWithDuration(0.5)
myView.transform = CGAffineTransformMakeRotation(CGFloat(-M_PI))
it still rotates clockwise. What's confusing me is that if I write "manually" the value of M_PI
, it works as expected
// this will rotate counterclockwise
myView.transform = CGAffineTransformMakeRotation(CGFloat(-3.14159265358979))
Why this behavior?
Edit: added I'm using an animation block
As I stated in my first comment and as Segii already wrote: the animation will take the shortest distance between where it's value is at now and where it is trying to go. The best solution imho is to use CABasicAnimation
for forcing a counterclockwise rotation:
let anim = CABasicAnimation(keyPath: "transform.rotation")
anim.fromValue = M_PI
anim.toValue = 0
anim.additive = true
anim.duration = 2.0
myView.layer.addAnimation(anim, forKey: "rotate")
myView.transform = CGAffineTransformMakeRotation(CGFloat(-M_PI))
This will rotate it by 180 counter clockwise in one go.
The angle you specify in the last line + the fromValue
is the starting angle -> 0. And then the rotation will go the distance (toValue - fromValue) = 0 - M_PI = - M_PI
-> counter clockwise 180 degree rotation.
As I understood, you're trying to rotate view within animation block. animateWithDuration
will always perform animation with the shortest way. So, M_PI and -M_PI gives the same destination position. The reason, why you get expected animation direction when setting it manually is that your manual value is really smaller that M_PI, so the shortest way to rotate is counterclockwise.
To get expected behaviour, you'll have to chain at least two animations, splitting you rotation angle. For example
//first animation block
myView.transform = CGAffineTransformMakeRotation(CGFloat(-M_PI/2))CGAffineTransformMakeRotation;
//second animation block
myView.transform = CGAffineTransformMakeRotation(CGFloat(-M_PI/2))CGAffineTransformMakeRotation;
Swift 3.0
myView.transform = CGAffineTransform(rotationAngle: CGFloat(-M_PI/2))
Or, much more reliable, use CAKeyframeAnimation
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With