Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CGAffineTransformMakeRotation before a CABasicAnimation

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?

like image 317
Brian Liang Avatar asked Mar 04 '26 07:03

Brian Liang


1 Answers

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.

like image 100
jv42 Avatar answered Mar 05 '26 20:03

jv42



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!