基本关键帧动画(旋转)(Basic keyframe animation (rotation))

2019-06-23 22:31发布

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

(目的是能够通过弧大于180度,的钝角动画旋转而不是动画“欺骗”和走最短的路线,即,经由相反,急性更小的角度-时会发生只有一种[即目的地]关键帧。要围绕走“长”的方式,我想我需要通过一个额外的关键帧中途,沿所需的电弧。)

这里就是我这么远(这确实让图形到所需的旋转,通过最锐角):

#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];

据我了解,我不是在寻找沿路径动画(因为这是用于翻译,而不是旋转)...

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

Answer 1:

我想我已经得到了它。

这里的代码做(在这个例子中)全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];

谢谢!



Answer 2:

试试这个:

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"];


Answer 3:

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];


文章来源: Basic keyframe animation (rotation)