基本关键帧动画(旋转)

时间:2009-06-23 07:51:01

标签: iphone core-animation

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

(目的是能够通过 OBTUSE 角度大于180度的弧度来制作旋转动画,而不是让动画'作弊'并走最短的路线,即通过对面,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];

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

无论如何,任何帮助都会非常感激!提前谢谢。

3 个答案:

答案 0 :(得分: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];

谢谢!

答案 1 :(得分:1)

试试这个:

UIImageView* rotatingImage = [[UIImageView alloc] init]];
[rotatingImage setImage:[UIImage imageNamed:@"someImage.png"]];

CATransform3D rotationTransform = CATransform3DMakeRotation(1.0f * M_PI, 0, 0, 1.0);
CABasicAnimation* rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform"];

rotationAnimation.toValue = [NSValue valueWithCATransform3D:rotationTransform];
rotationAnimation.duration = 0.25f;
rotationAnimation.cumulative = YES;
rotationAnimation.repeatCount = 1;

[rotatingImage.layer addAnimation:rotationAnimation forKey:@"rotationAnimation"];

答案 2 :(得分:1)

CGMutablePathRef path = CGPathCreateMutable();
CGPathMoveToPoint(path, NULL, 100, 100);
CGPathAddQuadCurveToPoint(path, NULL, 100, 100, 100, 615);
CGPathAddQuadCurveToPoint(path, NULL, 100, 615, 900, 615);
CGPathAddQuadCurveToPoint(path, NULL, 900, 615, 900, 100);
CGPathAddQuadCurveToPoint(path, NULL, 900, 100, 100, 80);
CAKeyframeAnimation *pathAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
pathAnimation.path = path;
pathAnimation.duration = 10.0;
[someLayer addAnimation:pathAnimation forKey:nil];