Search code examples
iosswiftuiviewios8cgaffinetransform

CGAffineTransformMakeRotation with negative M_PI


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


Solution

  • 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.