用于推送 UIViewController 的自定义动画

发布于 2024-10-15 03:40:23 字数 836 浏览 2 评论 0原文

我想在推动视图控制器时显示自定义动画:我想实现类似“展开”动画的效果,这意味着新视图从给定的矩形展开,例如在动画到全屏期间的[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 时,我无法调整视图框架的大小......是吗?

I want to show a custom animation when pushing a view controller: I would like to achieve something like an "expand" animation, that means the new view expands from a given rectangle, lets say [100,100 220,380] during the animation to full screen.

Any suggestions where to start, respectively any documents, tutorials, links? :)


Alright. I could make the expand animation with the following code:

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];

My View is properly displayed, but unfortunately the navigation bar is not updated. Is there a way to do that manually?


In the sample code, a function is called all 0.03 seconds that updates the transformation of the view.
Unfortunately, when pushing a UIViewController, I am not able to resize the frame of the view ... am I ?

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

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

发布评论

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

评论(6

雄赳赳气昂昂 2024-10-22 03:40:24

我使用以下函数(添加到 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];
}

我想您可以调整此代码来执行您想要的任何动画。

I use the following function (added to UINavigationController) to customize the push animation:

- (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];
}

I guess you could adapt this code to do whatever animation you want.

℉絮湮 2024-10-22 03:40:24

您正在寻找的代码:

    [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];

The code which you are looking for:

    [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];
葬﹪忆之殇 2024-10-22 03:40:24

您可以做的是推送下一个视图控制器,但不要对其进行动画处理,如下所示:

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

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

查看 核心动画指南,了解如何实际制作动画。特别注意隐式动画。

编辑: 已更新链接

What you could do is push the next view controller but don't animate it, like so:

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

...and then, in the view controller that is getting pushed in, you could do a custom animation of it's view using CoreAnimation. This might be best done in the viewDidAppear:(BOOL)animated method.

Check out the Core Animation Guide on how to actually do the animation. Look particularly at the implicit animation.

EDIT: updated link

诺曦 2024-10-22 03:40:24

@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];

然后我只是调整了推送功能并制作了我在 -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 调用已向下移动到末尾。不知道这是否符合犹太洁食,但它再次起作用了。

@zoul: That worked great! I just changed "self" to "self.navigationController" and "self.view" to "self.navigationController.view" Don't know if that was necessary, but it worked. And @crafterm, as for popping back, just make your own leftBarButtonItem by adding this code in viewDidLoad or ViewWillAppear:

//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];

Then I just tweaked the push function and made this popWithTransition function that I called in my -backButtonTapped method.

- (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];
}

Note that the popViewController call got shifted down to the end, after the animation. Don't know if that's kosher, but again, it worked.

好久不见√ 2024-10-22 03:40:24

您需要下载 iPhone 开发人员指南的第 2 章。具体查看 affineRotate 示例,尽管任何核心 animatin 示例都会对您有所帮助。

What you want is the downloads for chapter 2 of iphone developers cookbook. Look at the affineRotate sample specifically, although any of the core animatin samples will help you.

寻找我们的幸福 2024-10-22 03:40:24

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

您可以为pushpop操作使用不同的预定义动画,例如滑动淡入淡出立方体卡鲁塞尔等等。就您而言,您请求的动画是名为“Zoom”的动画。

Have a look at ADTransitionController, a drop in replacement for UINavigationController with custom transition animations (its API matches the API of UINavigationController) that we created at Applidium.

You can use different pre-defined animations for push and pop actions such as Swipe, Fade, Cube, Carrousel and so on. In your case, the animation you are requesting is the one called Zoom.

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