用于推送UIViewController的自定义动画

时间:2009-09-10 15:36:10

标签: iphone animation uikit uiviewcontroller uinavigationcontroller

我想在推送视图控制器时显示自定义动画:我希望实现像“展开”动画这样的东西,这意味着新视图从给定的矩形扩展,在动画期间让我们说[100,100 220,380]全屏。

任何建议从哪里开始,分别是任何文档,教程,链接? :)


好的。我可以使用以下代码制作展开动画:

if ([coming.view superview] == nil)   
    [self.view addSubview:coming.view];
    coming.view.frame = CGRectMake(160,160,0,0);
    [UIView beginAnimations:@"frame" context:nil];
    [UIView setAnimationDuration:4];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
    [coming viewWillAppear:YES];
    [going viewWillAppear:YES];
    coming.view.frame = CGRectMake(0, 0, 320, 480);
    [going viewDidDisappear:YES];
    [coming viewDidAppear:YES];
    [UIView commitAnimations];

我的视图已正确显示,但遗憾的是导航栏未更新。有没有办法手动完成?


在示例代码中,一个函数被称为0.03秒,用于更新视图的转换。 不幸的是,在推送UIViewController时,我无法调整视图的框架...是吗?

7 个答案:

答案 0 :(得分:53)

我使用以下函数(添加到UINavigationController)来自定义推送动画:

- (void) pushController: (UIViewController*) controller
         withTransition: (UIViewAnimationTransition) transition
{
    [UIView beginAnimations:nil context:NULL];
    [self pushViewController:controller animated:NO];
    [UIView setAnimationDuration:.5];
    [UIView setAnimationBeginsFromCurrentState:YES];        
    [UIView setAnimationTransition:transition forView:self.view cache:YES];
    [UIView commitAnimations];
}

我猜你可以调整这段代码来做你想做的任何动画。

答案 1 :(得分:34)

您正在寻找的代码:

    [UIView beginAnimations:@"View Flip" context:nil];
[UIView setAnimationDuration:0.80];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];

[UIView setAnimationTransition:
 UIViewAnimationTransitionFlipFromRight
                       forView:self.navigationController.view cache:NO];


[self.navigationController pushViewController:menu animated:YES];
[UIView commitAnimations];

答案 2 :(得分:24)

你可以做的是推动下一个视图控制器,但不要为它设置动画,如下所示:

[self.navigationController pushViewController:nextController animated:NO];

...然后,在推入的视图控制器中,您可以使用CoreAnimation对其视图进行自定义动画。这可能最好在viewDidAppear:(BOOL)animated方法中完成。

查看Core Animation Guide如何实际制作动画。特别注意隐式动画。

编辑: updated link

答案 3 :(得分:10)

海因里希,

我已经制作了一个youtube教程,展示如何扩展和缩小视图,就像在facebook iPhone应用中一样。

希望它可以提供帮助:How to make expanding/shrinking views on iPhone SDK

亚当

答案 4 :(得分:7)

@zoul:这很棒!我只是将“self”更改为“self.navigationController”,将“self.view”更改为“self.navigationController.view”。不知道是否有必要,但它有效。和@crafterm一样,弹出后,只需在viewDidLoad或ViewWillAppear中添加此代码即可创建自己的leftBarButtonItem:

//add your own left bar button
UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithTitle:@"Back" style:UIBarButtonItemStylePlain target:self action:@selector(backButtonTapped)];
self.navigationItem.leftBarButtonItem = backButton;
[backButton release];

然后我只是调整了push函数并创建了我在-backButtonTapped方法中调用的popWithTransition函数。

- (void) popWithTransition: (UIViewAnimationTransition) transition
{
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:.75];
    [UIView setAnimationBeginsFromCurrentState:YES];        
    [UIView setAnimationTransition:transition forView:self.navigationController.view cache:YES];
    [UIView commitAnimations];
[self.navigationController popViewControllerAnimated:NO];
}

请注意,在动画之后,popViewController调用已下移到结尾。不知道那是不是犹太人,但又一次,它有效。

答案 5 :(得分:3)

您想要的是iphone developers cookbook第2章的下载。请特别注意affineRotate样品,尽管任何核心的animatin样品都会对您有所帮助。

答案 6 :(得分:3)

看看ADTransitionController,我们在Applidium创建的自定义转换动画(它的API与UINavigationController的API匹配)是UINavigationController的替代品。

您可以为推送流行操作使用不同的预定义动画,例如刷卡淡化 Cube Carrousel 等。在您的情况下,您请求的动画是名为缩放的动画。