基本关键帧动画(旋转)

14 iphone core-animation

我正在尝试创建一个非常简单的关键帧动画,其中图形通过给定的中点从一个角度旋转到另一个角度.

(目的是能够通过大于180度的弧度的OBTUSE角度设置旋转动画,而不是使动画"作弊"并走向最短路径,即通过相反的ACUTE较小角度 - 这可能发生在只有一个[即目的地]关键帧.为了实现'长'的方式,我假设我需要在所需弧线的中途需要一个额外的关键帧.)

这是我到目前为止所获得的(通过最锐角将图形转换为所需的旋转):

#define DEGREES_TO_RADIANS(__ANGLE__) ((__ANGLE__) / 180.0 * M_PI)

...

[UIView beginAnimations:nil context:nil];
CGAffineTransform cgCTM = CGAffineTransformMakeRotation(DEGREES_TO_RADIANS(desiredEndingAngle));
[UIView setAnimationDuration:0.5];
graphic.transform = cgCTM;
[UIView commitAnimations];
Run Code Online (Sandbox Code Playgroud)

据我了解,我不是在寻找沿着Path的动画(因为那是翻译,而不是旋转)......

无论如何,任何帮助将非常感谢!提前致谢.

小智 18

我想我已经知道了.

这里的代码(在这个例子中)完成270度旋转(1.5*pi弧度),包括可以进一步定制的各种参数:

CALayer *layer = rotatingImage.layer;
CAKeyframeAnimation *animation;
animation = [CAKeyframeAnimation animationWithKeyPath:@"transform.rotation.z"];
animation.duration = 0.5f;
animation.cumulative = YES;
animation.repeatCount = 1;
animation.values = [NSArray arrayWithObjects:       // i.e., Rotation values for the 3 keyframes, in RADIANS
      [NSNumber numberWithFloat:0.0 * M_PI], 
      [NSNumber numberWithFloat:0.75 * M_PI], 
      [NSNumber numberWithFloat:1.5 * M_PI], nil]; 
animation.keyTimes = [NSArray arrayWithObjects:     // Relative timing values for the 3 keyframes
      [NSNumber numberWithFloat:0], 
      [NSNumber numberWithFloat:.5], 
      [NSNumber numberWithFloat:1.0], nil]; 
animation.timingFunctions = [NSArray arrayWithObjects:
      [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn],    // from keyframe 1 to keyframe 2
      [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut], nil]; // from keyframe 2 to keyframe 3
animation.removedOnCompletion = NO;
animation.fillMode = kCAFillModeForwards;

[layer addAnimation:animation forKey:nil];
Run Code Online (Sandbox Code Playgroud)

谢谢!