iPhone上控制器推动时的翻转动画

9

我查看了一下,没有找到我需要的内容。

在推送视图控制器时,有没有一种方法可以获得翻转动画?

我读到可以通过使用模态视图控制器来更改动画,但是据我所知,模态视图的动画是从底部到顶部,这不是我想要的。有没有办法以某种方式获取翻转动画?

4个回答

50

这样应该可以工作

[UIView beginAnimations:@"animation" context:nil];
[self.navigationController pushViewController: yourviewcontroller animated:NO]; 
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:self.navigationController.view cache:NO]; 
[UIView commitAnimations];

调用pushViewController时,别忘了将animated设置为NO。


1
我们能稍微减慢一下动画吗?它太快了。谢谢。 - Shishir.bobby
4
您可以使用"setAnimationDuration"函数来指定动画的持续时间,单位为秒。例如:[UIView setAnimationDuration:1.5]。 - John
3
如果你想与天气应用程序相匹配,0.7似乎是合适的。 - Tommy Herbert
是的,animated:NO 是很重要的。谢谢你提到它。 - abc123

15

这也适用于 iOS 4.0 及更高版本

[UIView  transitionWithView:self.navigationController.view duration:0.8  options:UIViewAnimationOptionTransitionFlipFromLeft
                             animations:^(void) {
                                 BOOL oldState = [UIView areAnimationsEnabled];
                                 [UIView setAnimationsEnabled:NO];
                                 [self.navigationController pushViewController:viewController animated:YES];
                                 [UIView setAnimationsEnabled:oldState];
                             }
                             completion:nil];

在iOS7中,它的工作效果不佳,因为视图动画被切成了一半,然后在动画完成时“跳”到位置。尽管被Apple不鼓励,但这种方式仍然完美地运行。 - margusholland

4
- (void)viewWillDisappear:(BOOL)animated {
[UIView beginAnimations:@"animation2" context:nil];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
[UIView setAnimationDuration: 0.7];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:self.navigationController.view cache:NO]; 
[UIView commitAnimations]; }

在新的视图控制器中,当工具栏中的“返回”按钮被按下时,它将以相同的方式翻转回去(而不是向左滑动)。请确保启用动画效果,例如,如果您想使用自定义按钮弹出堆栈,请使用以下代码:

- (void) backToPrevious: (id) sender 
{
    //[self.navigationController popViewControllerAnimated:YES];
    [self dismissModalViewControllerAnimated:YES];
}

1
UIView文档所述,beginAnimations:context:方法是苹果公司不建议开发者使用的一组方法(从iOS 4开始)。他们推荐使用UIView的基于块的动画方法(例如,animateWithDuration:animations:completion:)代替。 - user577537

1

对于以模式呈现的视图控制器,您可以使用modalTransitionStyle属性更改动画。据我所知,没有办法更改导航控制器的推送动画(除非从头开始重建UINavigationController)。


好的,现在应该足够了;)谢谢。 - chacha
2
经过长时间的搜索,我找到了这个 github 链接。对我来说它是有效的 https://github.com/devindoty/iOS-Transition-Pack - Naveen Thunga

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