如何在将 UIViewController 呈现给自定义时更改默认转换
Posted
技术标签:
【中文标题】如何在将 UIViewController 呈现给自定义时更改默认转换【英文标题】:How to change the default transition while presenting UIViewController to custom 【发布时间】:2015-03-25 09:25:04 【问题描述】:我正在尝试从我当前的视图控制器中呈现另一个 UIViewController。过渡动画设置为默认值,呈现的 UIViewController 似乎从底部到顶部出现。但是我正在尝试将动画从上到下设置。我怎样才能做到这一点? 我尝试过使用过渡效果作为垂直覆盖、水平翻转、交叉溶解,但似乎没有一个符合我的选择。有什么建议我该如何实现这一目标?
let storyboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let editCategoryVC: EditCategoryVC = storyboard.instantiateViewControllerWithIdentifier("editCategoryVC")as EditCategoryVC
self.presentViewController(editCategoryVC, animated:true, completion: nil
)
【问题讨论】:
查看这个答案:***.com/a/26859432/2082569 【参考方案1】:处理动画的TransitionHandler辅助类
import UIKit
class TransitionHandler : NSObject, UIViewControllerAnimatedTransitioning, UIViewControllerTransitioningDelegate
func animationControllerForPresentedController(presented: UIViewController, presentingController presenting: UIViewController, sourceController source: UIViewController) -> UIViewControllerAnimatedTransitioning?
return self
func animationControllerForDismissedController(dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning?
return self
func transitionDuration(transitionContext: UIViewControllerContextTransitioning) -> NSTimeInterval
return 0.9
func animateTransition(transitionContext: UIViewControllerContextTransitioning)
let container = transitionContext.containerView
let fromView = transitionContext.view(forKey: .from)!
let toView = transitionContext.view(forKey: .to)!
// e.g to show the animation from x axis change this frame as required
// let offScreenUp = CGAffineTransformMakeTranslation(-container.frame.size.width, 0)
// let offScreenDown = CGAffineTransformMakeTranslation(container.frame.size.width,0)
let offScreenUp = CGAffineTransform(translationX: 0, y: 0) // (-container.frame.size.width,0)
let offScreenDown = CGAffineTransform(translationX: 0, y: container.frame.size.height)
toView.transform = offScreenUp
container.addSubview(toView)
container.addSubview(fromView)
let duration = self.transitionDuration(using: transitionContext)
UIView.animate(withDuration: duration, delay: 0.0, usingSpringWithDamping: 0.9, initialSpringVelocity: 0.8, options: [], animations:
fromView.transform = offScreenDown
toView.transform = CGAffineTransform.identity
, completion: finished in
transitionContext.completeTransition(true)
)
在按钮单击时显示下一个控制器的类
class ViewController: UIViewController
let transitionHandler = TransitionHandler()
@IBAction func showVCAnimation(sender: AnyObject)
var storyboard : UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let nextVC = storyboard.instantiateViewControllerWithIdentifier("editCategoryVC") as EditCategoryVC
nextVC.transitioningDelegate = self.transitionHandler
self.presentViewController(nextVC, animated: true, completion: nil)
感谢 AppCoda!!!
【讨论】:
【参考方案2】:我认为以下可以工作
let editCategoryVC = self.storyboard?.instantiateViewControllerWithIdentifier("editCategoryVC") as editCategoryVC
self.presentViewController(editCategoryVC, animated:true, completion: nil
【讨论】:
这就是问题所在......这段代码的动画在呈现视图控制器时从下到上移动。我想要从上到下的动画以上是关于如何在将 UIViewController 呈现给自定义时更改默认转换的主要内容,如果未能解决你的问题,请参考以下文章
如何在 SKScene 中呈现 UIViewController?
如何访问呈现模态视图的 UIViewController 类/类名?
如何使用社交框架呈现来自 SKscene 的 UIViewController?
如何从 viewController 的视图中呈现 UIViewController