Why does My Rotation only go Anti Clockwise when I want to go Clockwise?

Just ran into this and while @radicalraid's solution works fine, I prefer adjusting the starting angle before rotation vs rotating to an angle less than the desired end point. Also, you can animate CALayer transforms using animation blocks.

My code:

// iOS will rotate the shortest distance to the end angle,
// using counter-clockwise if equal.
// Force the rotation to open clockwise and close counter-clockwise by bumping
// the initial angle
CGFloat startRadians, endRadians;
if (isOpening) {
    startRadians = 0.01f;
    endRadians = M_PI;

}
else {
    startRadians = M_PI - 0.01f;
    endRadians = 0.0f;
}

self.myImageView.layer.affineTransform = CGAffineTransformMakeRotation(startRadians);

[UIView animateWithDuration:0.3f
                 animations:^{
                     self.myImageView.layer.affineTransform = CGAffineTransformMakeRotation(endRadians);
                 }];

My experience with Core animatinon does not agree with that Apple quote. What I've found is that the only thing that matters is the final angle value. Positive pi and negative pi rotations result in the same transformation matrix, so the system just goes counter-clockwize regardless.

What I've done to go rotations in a specific direction, or rotations of a full 360 degrees, is to create an explicit CABasicAnimation using a type like transform.rotation.z, with the fill mode set to kCAFillModeForwards, and only provide a toValue. That causes the animation to start from the previous value. You can then set up a rotation that is an even fraction of your total desired rotation, and give it a repeat count.

For example, the code below creates an animation that rotates 5 quarter turns, or 450 degrees. If you wanted to do 180 degrees clockwise, you could a rotation of pi/2 with a repeat count of 2. For counterclockwise, use -pi/2 with a repeat count of 2.

  CABasicAnimation* rotate =  [CABasicAnimation animationWithKeyPath: 
    @"transform.rotation.z"];
  rotate.removedOnCompletion = FALSE;
  rotate.fillMode = kCAFillModeForwards;

  //Do a series of 5 quarter turns for a total of a 1.25 turns
  //(2PI is a full turn, so pi/2 is a quarter turn)
  [rotate setToValue: [NSNumber numberWithFloat: -M_PI / 2]];
  rotate.repeatCount = 5;

  rotate.duration = duration/rotate.repeatCount * 2 ;
  rotate.beginTime = start;
  rotate.cumulative = TRUE;
  rotate.timingFunction = [CAMediaTimingFunction 
    functionWithName:kCAMediaTimingFunctionLinear];

EDIT: Instead of -M_PI use -3 * M_PI, for anti-clockwise

and remove '-' sign for opposite direction.


The angle is the same. To go full circle you need 2 * Pi, so half a circle is Pi. If you take -Pi it would end at the same point.

EDIT: If you need to make it turn clockwise you can go slightly off -Pi. Instead of -M_PI use -3.141593 for example.