swift中基本转场动画.

swift中基本转场动画.,第1张

概述//-----------------------------------------------------在一个控制器中实现代码--------------------------------------------------------------------             //默认的Modal,会将上一级的视图移走             let photoBrowserCon



//-----------------------------------------------------在一个控制器中实现代码--------------------------------------------------------------------

//默认的Modal,会将上一级的视图移走
let photobrowserController = PhotobrowserController(urls: urls,indexPaht: indexPath)

//指定动画的提供者,TransitioningDelegate 隐式动画。
photobrowserController.TransitioningDelegate = self?.photobrowserAnimator

//指定Model的样式是自定义的
photobrowserController.modalPresentationStyle = UIModalPresentationStyle.Custom
self?.presentVIEwController(photobrowserController,animated: false,completion: nil)


//-----------------------------------------------------定义转场动画类----------------------------------------------------------------------

import UIKit

/// 专门给控制器提供 ‘转场’ 的动画模型 class PhotobrowserAnimator: NSObject,UIVIEwControllerTransitioningDelegate { //是否展示动画 var isPresent = false /// 返回提供展现 Presented 转场动画的对象 func animationControllerForPresentedController(presented: UIVIEwController,presentingController presenting: UIVIEwController,sourceController source: UIVIEwController) -> UIVIEwControllerAnimatedTransitioning? { isPresent = true return self } /// 返回消失时 dismissed 转场动画的对象 func animationControllerFordismissedController(dismissed: UIVIEwController) -> UIVIEwControllerAnimatedTransitioning? { isPresent = false return self } } extension PhotobrowserAnimator: UIVIEwControllerAnimatedTransitioning{ /// '转场'时长 /// /// - parameter TransitionContext: Transition[转场]Context 上下文 /// /// - returns: 时长 func TransitionDuration(TransitionContext: UIVIEwControllerContextTransitioning?) -> NSTimeInterval { return 0.5 } /// 实现专场动画效果 - 一旦实现这个方法,必须完成动画效果 /// /// - parameter TransitionContext: Transition[转场]Context 提供一转场动画效果的一切细节 func animateTransition(TransitionContext: UIVIEwControllerContextTransitioning) { isPresent ? presentAnimation(TransitionContext) : dismissAnimation(TransitionContext) } /// 消失时动画 /// /// - parameter TransitionContext: TransitionContext private func dismissAnimation(TransitionContext: UIVIEwControllerContextTransitioning){ let fromVIEw = TransitionContext.vIEwControllerForKey(UITransitionContextFromVIEwControllerKey)?.vIEw UIVIEw.animateWithDuration(TransitionDuration(TransitionContext),animations: { () -> VoID in fromVIEw?.Alpha = 0 }) { (_) -> VoID in fromVIEw?.removeFromSupervIEw() TransitionContext.completeTransition(true) } } /// 出现时动画 /// /// - parameter TransitionContext: TransitionContext private func presentAnimation(TransitionContext: UIVIEwControllerContextTransitioning){ //展示动哈 let toVIEw = TransitionContext.vIEwControllerForKey(UITransitionContextToVIEwControllerKey)!.vIEw toVIEw.Alpha = 0 //获取容器视图 let containerVIEw = TransitionContext.containerVIEw() //将视图添加到容器视图上才能看到动画效果 containerVIEw?.addSubvIEw(toVIEw) UIVIEw.animateWithDuration(TransitionDuration(TransitionContext),animations: { () -> VoID in toVIEw.Alpha = 0 }) { (_ ) -> VoID in //动画完成后要声明方式 TransitionContext.completeTransition(true) } } } 总结

以上是内存溢出为你收集整理的swift中基本转场动画.全部内容,希望文章能够帮你解决swift中基本转场动画.所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1083243.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-27
下一篇 2022-05-27

发表评论

登录后才能评论

评论列表(0条)

保存