gpt4 book ai didi

iOS:同步缩放和 3D 旋转动画

转载 作者:可可西里 更新时间:2023-11-01 03:02:43 25 4
gpt4 key购买 nike

我制作了一个视频来展示我正在尝试完成的动画:Here it is.

请注意,视频展示了从侧面看到的 Action 。相机图标代表用户的 POV。它基本上是通过缩放变换完成的 Z 轴之间平移的模拟,同时沿 X 轴发生独立的两步 3D 旋转。

看起来和听起来很简单,对吧?错误的。这是理想的代码,它不起作用:

[UIView animateWithDuration:0.4 delay:0.0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
view.transform = CGAffineTransformMakeScale(1.0, 1.0);
} completion:^(BOOL finished) {}];

[UIView animateWithDuration:0.2 delay:0.0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
CATransform3D rotation = CATransform3DIdentity;
rotation.m34 = 1.0 / - 1800;
rotation = CATransform3DRotate(rotation, - 20 * M_PI / 180.0f, 1, 0, 0);

view.layer.transform = rotation;

} completion:^(BOOL finished) {
[UIView animateWithDuration:0.2 delay:0.0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
view.layer.transform = CATransform3DIdentity;

} completion:^(BOOL finished) {}];
}];

事实证明,如果您这样做,3D 旋转将被完全忽略。我尝试了很多其他方法,但都失败了。

一些要求:

  • Scale 最好是 CGAffineTransform
  • 缓动应该如图所示,尤其是在轮换上

当然,如果出现需要更改其中一些内容的解决方案,我可以适应。

在此先感谢您的帮助。如果您需要澄清,请务必询问。

最佳答案

如果不取消第一个动画,就不能为相同的属性设置动画。您应该使用 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)

透视

在这两种情况下,我都通过在动画 View 的超层上设置 sublayerTransform 来实现透视(假设那里只有一个带有变换的 subview )

CATransform3D perspective = CATransform3DIdentity;
perspective.m34 = 1.0 / - 1800.0;
view.superview.layer.sublayerTransform = perspective;

关于iOS:同步缩放和 3D 旋转动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14016969/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com