两个 UIViewController 之间的 3D 门打开动画
之前已经有人问过这个问题,但据我所知,在 stackoverflow 上找不到示例代码,也没有正确的解决方案。我有一个解决方案,但它看起来很糟糕。我将不胜感激任何输入和修改以获得更逼真的 3D 门打开/书籍封面动画。
目的是在 UIViewController 之间创建动画,以便获得就像打开门或书本封面一样的效果。 ViewController 2 是静态的并且在后台。在其上方放置 ViewController 1,它覆盖 ViewController 2。动画将打开 ViewController 1(就像一本精装书)并显示 ViewController 2(可以说是您的第一页,或者门后面的任何内容)。
首先要注意的是,您无法使用 UINavigationController 执行此操作,因为很难覆盖自定义动画。有关如何设置两个 ViewController 的正确教程可以在这里找到: ViewController 动画
因此,一旦一切设置完毕,这是我的自定义动画,看起来很糟糕。基本上看起来好像您正在向左挤压书的门/封面。恐怕没有 3D 感觉。欢迎任何有关如何改进这一点的建议:
-(void)openDoorTo:(UIViewController *)aController duration:(float)aDuration {
[aController viewWillAppear:YES];
[activeController viewWillDisappear:YES];
[self.view insertSubview:aController.view belowSubview:activeController.view]; // so that it is below activeController
[aController viewDidAppear:YES];
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:aDuration];
aController.view.transform = CGAffineTransformMakeTranslation(0,0);
CATransform3D _3Dt = CATransform3DIdentity;
_3Dt = CATransform3DTranslate(_3Dt, -320, 0, 0);
_3Dt = CATransform3DRotate(_3Dt, M_PI * 1.5, 0.0, 1, 0.0);
_3Dt = CATransform3DTranslate(_3Dt, 320, 0, 0);
activeController.view.layer.transform = _3Dt;
[UIView commitAnimations];
[self performSelector:@selector(animationDone:) withObject:aController afterDelay:aDuration];
}
我认为主要问题是我真的不知道如何处理 CATransform3DTranslate
和 CATransform3DRotate
。
这里有一些关于此的帖子,但我真的不知道如何将它们应用到 3D 开门器:
This has been asked before, but there is no sample code to be found on stackoverflow and no proper solutions as far as I'm aware. I have a solution, but it looks crap. I'd be grateful for any input and modifications to get a more realistic 3D door open / book cover animation.
Aim is to have an animation between UIViewControllers so that you get the effect as if you were opening a door or a book cover. ViewController 2 is static and in the background. Above it you place ViewController 1 which covers ViewController 2. The animation will open ViewController 1 (like a hardcover book) and reveal ViewController 2 (your first page so to say, or whatever is behind the door).
First thing to note is that you can't do this with an UINavigationController as it is difficult to overwrite the custom animations. A proper tutorial on how to set up our two ViewControllers can be found here: ViewController Animations
So once everything is setup, here is my custom animation which looks crap. It basically looks as if you are squeezing the door / cover of the book to the left. There is no 3D feel to it, I'm afraid. Any suggestions of how to make this better would be welcome:
-(void)openDoorTo:(UIViewController *)aController duration:(float)aDuration {
[aController viewWillAppear:YES];
[activeController viewWillDisappear:YES];
[self.view insertSubview:aController.view belowSubview:activeController.view]; // so that it is below activeController
[aController viewDidAppear:YES];
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:aDuration];
aController.view.transform = CGAffineTransformMakeTranslation(0,0);
CATransform3D _3Dt = CATransform3DIdentity;
_3Dt = CATransform3DTranslate(_3Dt, -320, 0, 0);
_3Dt = CATransform3DRotate(_3Dt, M_PI * 1.5, 0.0, 1, 0.0);
_3Dt = CATransform3DTranslate(_3Dt, 320, 0, 0);
activeController.view.layer.transform = _3Dt;
[UIView commitAnimations];
[self performSelector:@selector(animationDone:) withObject:aController afterDelay:aDuration];
}
I think the main problem is that I don't really know how to handle CATransform3DTranslate
and CATransform3DRotate
.
Here are some posts on this, but I can't really see how to apply them to a 3D Door opener:
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您必须应用一些小技巧才能使 3D 工作:
这会创建透视变换。 1000 表示观察者到对象的距离(您可以尝试使用此值以获得平滑的外观)。
You have to apply a little trick to get 3D to work:
This creates perspective transform. 1000 represents the distance from the viewer to the objects (you can experiment with this value to get a smooth look).