在推动 UINavigationController 时对 UIView 进行动画处理

4
以下是需要翻译的内容:

以下是情况说明。 我有一个使用UINavigationController进行导航的应用程序。

对于特殊的导航控制器推送,我想要自定义动画,即缩小动画。 到目前为止,我的效果看起来不错,唯一的问题是,“旧”的Viewcontroller在动画开始之前就消失了,因此新的viewcontroller从“无处”缩小,而不是在背景中查看旧的viewcontroller。

为了更好地查看,我创建了一个简单的示例应用程序:下载

enter image description hereenter image description hereenter image description here

是否有人知道如何使新的viewcontroller(image3)在动画时旧的viewcontroller(image1)保持在后台(image2)?

/* THIS CANNOT BE CHANGED */
    AnimatedViewController *animatedViewController = [[AnimatedViewController alloc] initWithNibName:@"AnimatedViewController" bundle:nil];

    /* THIS CAN BE CHANGED */
    animatedViewController.view.transform = CGAffineTransformMakeScale(0.01, 0.01);
    [UIView beginAnimations:@"animationExpand" context:NULL];
    [UIView setAnimationDuration:0.6f];
    animatedViewController.view.transform=CGAffineTransformMakeScale(1, 1);
    [UIView setAnimationDelegate:self];
    [UIView commitAnimations];

    /* THIS CANNOT BE CHANGED */
    [self.navigationController pushViewController:animatedViewController animated:NO];

附加信息:我的应用程序并不简单。我在我的应用程序中使用了Three20框架,但推送和创建视图控制器只是Three20的常规操作。我只能钩取代码中(THIS CAN BE CHANGED)这部分的内容。我不能在此之前或之后修改代码(除非进行大量研究)。


如何在返回原始视图时设置相同的动画(反向方向)? - iOS dev
2个回答

9
我很快地完成了这个。思路是在缩放动画完成后调用pushViewController。
-(IBAction)animateButtonClicked:(id)sender {
    animatedViewController = [[AnimatedViewController alloc] initWithNibName:@"AnimatedViewController" bundle:nil];
    animatedViewController.view.transform = CGAffineTransformMakeScale(0.01, 0.01);

    [UIView animateWithDuration:0.6
                     animations:^{
                         [self.view addSubview:animatedViewController.view];
                         animatedViewController.view.transform=CGAffineTransformMakeScale(1, 1);                     
                     }
                     completion:^(BOOL finished){ 
                         [animatedViewController.view removeFromSuperview];
                         [self.navigationController pushViewController:animatedViewController animated:NO];
                     }];

}

是的,这在我的示例应用程序中运行良好。但是我在我的应用程序中使用它时遇到了问题。我只能在委托回调中工作:https://gist.github.com/f13b1da4114b7d8691d6,在此委托之后,pushviewcontroller被调用。 - choise
问题在于,视图被渲染了两次。 - choise
@choise,所以你的意思是在比例动画完成之前(即等待0.6秒),无法从函数返回,因为紧接着会直接调用pushViewController吗? - Martin Wickman
没错,我可以钩入一个在之前立即被调用的委托。请参见 https://github.com/facebook/three20/blob/master/src/Three20UINavigator/Sources/TTBaseNavigator.m#L449 - choise
如何在返回到原始视图时设置相同的动画(反向方向)? - iOS dev

0

好的,一种方法(有点丑陋)是在完成动画后进行pushViewController。当您执行动画时,需要动画化即将呈现的视图控制器屏幕看起来像什么。由于动画以新视图结束,因此当新视图出现在屏幕上时,不应发生任何变化,因为它与刚刚动画化的视图相同。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接