4

之前有人问过这个问题,但据我所知,在 stackoverflow 上找不到示例代码,也没有合适的解决方案。我有一个解决方案,但它看起来很糟糕。我将不胜感激任何输入和修​​改,以获得更逼真的 3D 开门/书籍封面动画。

目的是在 UIViewControllers 之间有一个动画,这样你就可以得到效果,就好像你在打开一扇门或一本书的封面一样。ViewController 2 是静态的并且在后台。在它上面放置 ViewController 1,它覆盖 ViewController 2。动画将打开 ViewController 1(就像一本精装书)并显示 ViewController 2(可以说是你的第一页,或者门后面的任何东西)。

首先要注意的是,您不能使用 UINavigationController 执行此操作,因为很难覆盖自定义动画。关于如何设置我们的两个 ViewController 的正确教程可以在这里找到:ViewController Animations

所以一旦一切都设置好了,这是我的自定义动画,看起来很糟糕。它基本上看起来好像你在向左挤压书的门/封面。恐怕没有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];


}

我认为主要问题是我真的不知道如何处理CATransform3DTranslateCATransform3DRotate.

这里有一些关于此的帖子,但我真的不知道如何将它们应用于 3D 开门器:

3D 门 - 但轴在中间

3D展开

4

1 回答 1

8

你必须应用一个小技巧来让 3D 工作:

CATransform3D _3Dt = CATransform3DIdentity;
_3Dt.m34 = 1.0 / -1000;

这会创建透视变换。1000 表示从观察者到对象的距离(您可以试验这个值以获得平滑的外观)。

于 2011-10-07T09:47:58.177 回答