3D Door Open Animation between two UIViewControllers

故事扮演 提交于 2020-01-10 09:21:12

问题


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:

3D Door - but Axis in the middle

3D Unfolding


回答1:


You have to apply a little trick to get 3D to work:

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

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).



来源:https://stackoverflow.com/questions/7685547/3d-door-open-animation-between-two-uiviewcontrollers

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!