3
votes

I'm performing a rotation of an UIImageView in place first before performing a rotation animation:

// rotate to the left by 90 degrees
someView.transform = CGAffineTransformMakeRotation((-0.5)*M_PI);

Then calling a rotation on the view by 180 degrees... but it seems like it is rotating the image starting from the original position as if it was not initially rotated.

- (void)rotateIndicatorToAngle: (UIView *)view angle: (NSNumber *)angleInDegrees
{
    CALayer *layer = view.layer;
    CGFloat duration = 5.0;
    CGFloat radians = [self ConvertDegreesToRadians: [angleInDegrees floatValue]];
    CABasicAnimation* rotationAnimation;
    rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
    rotationAnimation.toValue = [NSNumber numberWithFloat: radians];
    rotationAnimation.duration = duration;
    rotationAnimation.cumulative = YES;
    rotationAnimation.repeatCount = 1.0;
    rotationAnimation.removedOnCompletion = NO;
    rotationAnimation.fillMode = kCAFillModeForwards;
    rotationAnimation.timingFunction = [CAMediaTimingFunction
functionWithName:kCAMediaTimingFunctionEaseOut];

[layer addAnimation: rotationAnimation forKey: @"rotationAnimation"];
}

What gives?

2
hi, I try to use your code, but after I call this method, my UIView is moving to top left corner of my super view and then center.x and y become negative values. Why?Dima Deplov

2 Answers

4
votes

rotationAnimation.cumulative = YES;

Have you tried using rotationAnimation.additive = YES; instead of cumulative?

2
votes

You could use the class methods from UIView to easily animate your view:

[UIView beginAnimation: @"Rotate" context: nil];
[UIView setAnimationDuration: 5.0f];
[UIView setAnimationCurve: UIViewAnimationCurveEaseOut];

CGFloat radians = [self ConvertDegreesToRadians: [angleInDegrees floatValue]];
view.transform = CGAffineTransformMakeRotation(radians);

[UIView commitAnimation];

That's what I'm using most of the time, no need to use layers.

UPDATE: I mean, I find it easier not to use layers in that case, no pejorative value on using layers.