iOS开发——自定义转场动画

王朝学院·作者佚名  2016-08-27
窄屏简体版  字體: 小  |  中  |  大  |  超大  

首先是UipresentationController,这个控制器给modal新的viewController提供了下一步的view和转场的管理,从一个viewController被modal出来到被dismiss,都是用UIPResentationController来管理视图被展现的过程的各个方面。可以添加自定义动画,对大小改变做出响应,并且可以管理其他viewController是如何显示在屏幕的。

在初始化时,重写

initWithPresentedViewController:presentingViewController:

这个方法,初始化并返回一个presentationController给两个视图之间的转换

containerView

这个只读属性是被呈现的视图所在的view,我们可以在这个view上添加其他视图,通过

insertSubview(dummyView, atIndex: 0)这个方法。

接下来就用到两个代理 UIViewControllerTransitioningDelegate和 UIViewControllerAnimatedTransitioning

实现代理方法,isPresented是一个是否被呈现的标记

//返回负责转场的控制器对象func presentationControllerForPresentedViewController(presented: UIViewController, presentingViewController presenting: UIViewController, sourceViewController source: UIViewController) -> UIPresentationController?{returnPopoverPresentationController(presentedViewController: presented, presentingViewController: presenting)

}//返回提供modal动画的对象func animationControllerForPresentedController(presented: UIViewController, presentingController presenting: UIViewController, sourceController source: UIViewController) -> UIViewControllerAnimatedTransitioning?{

isPresented=truereturnself

}//返回提供dismiss动画的对象func animationControllerForDismissedController(dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning?{

isPresented=falsereturnself

}//动画时长func transitionDuration(transitionContext: UIViewControllerContextTransitioning?) ->NSTimeInterval {return1.2}//转场动画实现函数func animateTransition(transitionContext: UIViewControllerContextTransitioning) {

}

在最后一个方法中实现转场动画的过程。

此外,还要设置

//设置转场动画代理vc.transitioningDelegate =self//将转场动画设置为自定义vc.modalPresentationStyle = UIModalPresentationStyle.Custom

否则,转场动画是无法自定义的。

 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
© 2005- 王朝網路 版權所有 導航