如果不取消第一个动画,则无法为同一属性设置动画。您应该选择使用 Core Animation 并制作两个动画或一个关键帧动画。
两个变换动画
通过创建一个用于缩放的动画(如果需要,您可以将其作为 z 平移)和一个用于旋转的动画,为它们提供非常具体的关键路径,它们不会相互抵消。
CABasicAnimation *scale = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
scale.fromValue = @0.75; // Your from value (not obvious from the question)
scale.toValue = @1.0;
scale.duration = 0.4;
scale.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
CAKeyframeAnimation *rotate = [CAKeyframeAnimation animationWithKeyPath:@"transform.rotation.x"];
rotate.values = @[@0.0, @(- 20 * M_PI / 180.0f), @0.0];
rotate.duration = 0.4;
rotate.timingFunctions = @[[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut],
[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];
[view.layer addAnimation:scale forKey:@"move forward by scaling"];
[view.layer addAnimation:rotate forKey:@"rotate back and forth"];
view.transform = CGAffineTransformIdentity; // Set end value (animation won't apply the value to the model)
单个关键帧动画
由于您拥有三个非常好的关键帧,因此在三个关键帧之间进行动画处理的代码将很容易阅读和理解。如果您想将缩放的时间与旋转的时间分开,您可能会失去一些控制。
CATransform3D firstFrame = CATransform3DMakeScale(0.75, 0.75, 1.0);
CATransform3D secondFrame = CATransform3DMakeScale(0.875, 0.875, 1.0); // halfway to 1.0 from 0.75
secondFrame = CATransform3DRotate(secondFrame, -20.0*M_PI/180.0, 1.0, 0.0, 0.0);
CATransform3D lastFrame = CATransform3DIdentity;
CAKeyframeAnimation *scaleAndRotate = [CAKeyframeAnimation animationWithKeyPath:@"transform"];
scaleAndRotate.values = @[[NSValue valueWithCATransform3D:firstFrame],
[NSValue valueWithCATransform3D:secondFrame],
[NSValue valueWithCATransform3D:lastFrame] ];
scaleAndRotate.duration = 1.0;
scaleAndRotate.timingFunctions = @[[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut],
[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];
[view.layer addAnimation:scaleAndRotate forKey:@"entire animation"];
view.transform = CGAffineTransformIdentity; // Set end value (animation won't apply the value to the model)
看法
在这两种情况下,我通过在动画视图的超层上设置 sublayerTransform 来进行透视(假设那里只有一个带有变换的子视图)
CATransform3D perspective = CATransform3DIdentity;
perspective.m34 = 1.0 / - 1800.0;
view.superview.layer.sublayerTransform = perspective;