自定义 UINavigationController 动画:CATransition

发布于 2024-12-08 11:33:03 字数 1072 浏览 0 评论 0原文

我有一个 UINavigationController。我将 VC1 设置为 rootViewController,并以编程方式从 VC1 加载 VC2,然后将自定义动画从 VC1 转到 VC2。标准。一切都很好。

现在,我想在两者之间有一个自定义动画,如下所示: 自定义动画

总之,VC1 滑出视图,而 VC2 位于其下方。就像一叠纸一样,您滑开第一张纸 (VC1),从而露出下面的纸 (VC2)。

所以我尝试的是从 VC1 调用以下代码以到达 VC2。但它也有问题:

MyVC2 *vctwo = [[[MyVC2 alloc] init] autorelease];

CATransition *transition = [CATransition animation];
transition.duration = 1;
transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
transition.type = kCATransitionReveal;
transition.subtype = kCATransitionFromRight;
transition.delegate = self;
[self.navigationController.view.layer addAnimation:transition forKey:nil];

[[self navigationController] pushViewController:vctwo animated:YES];

问题如下:

  1. VC2 没有在后台修复。它也会滑入(即使我指定了 kCATransitionReveal)。我想让VC2完全固定在后台。
  2. VC1 淡出。我真的不知道为什么。我没有使用 kCATransitionFade 之类的,所以我看不到淡入淡出来自哪里。

任何关于为什么我没有得到预期结果的建议将非常感激。抱歉,如果这是显而易见的,但我现在已经尝试了几个小时并且感到非常沮丧。

I have an UINavigationController. I made VC1 the rootViewController and programatically load VC2 from VC1 and then have the custom animation to go from VC1 to VC2. Standard. Everything is fine and good.

Now, I would like to have a custom animation between the two like so:
Custom animation

In sum, VC1 slides out of view while VC2 is beneath it. Just like a stack of paper where you slide away the first sheet (VC1) and thus reveal the sheet beneath (VC2).

So what I tried is the following code which is called from VC1 in order to get to VC2. But there are problems with it:

MyVC2 *vctwo = [[[MyVC2 alloc] init] autorelease];

CATransition *transition = [CATransition animation];
transition.duration = 1;
transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
transition.type = kCATransitionReveal;
transition.subtype = kCATransitionFromRight;
transition.delegate = self;
[self.navigationController.view.layer addAnimation:transition forKey:nil];

[[self navigationController] pushViewController:vctwo animated:YES];

The problems are the following:

  1. VC2 is not fixed in the background. It kind of slides in as well (even though I specified kCATransitionReveal). I want to be VC2 totally fixed in the background.
  2. VC1 fades out. I don't really know why. I did not use kCATransitionFade or the like, so I can't see where the fade comes from.

Any suggestions why I am not getting the expected results would be very much appreciated. Sorry if this is obvious, but I am trying this for hours now and got really frustrated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

提笔落墨 2024-12-15 11:33:03

我认为你应该使用 [[self navigationController] PushViewController:vctwoAnimated:NO]; 而不是 animated:YES

I think you should use [[self navigationController] pushViewController:vctwo animated:NO]; rather than animated:YES.

一紙繁鸢 2024-12-15 11:33:03

我想做到这一点的唯一方法是忘记 UINavigationController 并提出我自己的机制来处理所有事情:

此博客解释了如何...

因此,如果您想在 VC 之间自定义动画,请不要使用 UINavigationController。下面是如上所述在两个 VC 之间交换的示例代码:

    -(void)slideDoorOpenTo:(UIViewController *)aController duration:(float)aDuration {

    [aController viewWillAppear:YES];
    [activeController viewWillDisappear:YES];

    //aController.view.alpha = 0.0f;
    [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);
    activeController.view.transform = CGAffineTransformMakeTranslation(-320,0);

    [UIView commitAnimations];

    [self performSelector:@selector(animationDone:) withObject:aController afterDelay:aDuration];


}

I suppose the only way to do this is to forget UINavigationController and come up with my own mechanism to handle everything:

This blog explains how...

So if you wanted to custom animations between VCs, do not use UINavigationController. Here is sample code to swap between two VCs as described above:

    -(void)slideDoorOpenTo:(UIViewController *)aController duration:(float)aDuration {

    [aController viewWillAppear:YES];
    [activeController viewWillDisappear:YES];

    //aController.view.alpha = 0.0f;
    [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);
    activeController.view.transform = CGAffineTransformMakeTranslation(-320,0);

    [UIView commitAnimations];

    [self performSelector:@selector(animationDone:) withObject:aController afterDelay:aDuration];


}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文