深入学习iOS7自定义导航转场动画

2020-01-14 20:05:04刘景俊

 然后,在推入模态视图控制器时,我们设置modalPresentationStyle为UIModalPresentationFullScreen或UIModalPresentationCustom。我们还必须将一个符合UIViewControllerTransitioningDelegate协议的对象设置为它的transitioningDelegate,一般来说都是推入该模态视图控制器的UIViewController。 


OptionsViewController *modal = [[OptionsViewController alloc] 
initWithNibName:@"OptionsViewController" bundle:[NSBundle 
mainBundle]]; 
modal.transitioningDelegate = self; 
modal.modalPresentationStyle = UIModalPresentationCustom; 
[self presentViewController:modal animated:YES 
completion:nil]; 

如果需要将动画控制器应用到UINavigationController的转场动画中,我们需要使用UINavigationControllerDelegate协议中的一个新方法:animationControllerForOperation。对于任何自定义的导航转场动画,导航栏都会有一个淡入淡出的动画过程。同样,对于UITabBarController,使用UITabBarControllerDelegate协议的新方法——animationController-ForTransitionFromViewController。

 为转场动画定义交互方式
在iOS7中,苹果到处都在使用交互式弹出手势,同时,苹果也给开发者们提供了一系列工具,只需简单几步就能将交互手势应用在视图切换过程中。我们可以通过相应的委托方法返回一个交互控制器:

  •  UINavigationController
  • interactionControllerForAnimationController
  • UITabBarController
  • interactionControllerForAnimationController
  • UIViewController
  • interactionControllerForPresentation
  • interactionControllerForDismissal 

    这里唯一需要注意的是,如果没有自定义转场动画,这些方法就不会起作用。例如,你必须从animationControllerForOperation得到一个有效的动画控制器,UINavigationController才会调用interactionController-
    ForAnimationController——即使你在转场交互中没有使用动画控制器。

    其次,交互控制器非常灵活,有很强的可扩展性。虽然在示例应用程序中我使用手势检测来控制交互,但是你也可以用手势以外的其他方式来实现。你可以设计任意你想要的效果用以转场交互。