【问题标题】:How to change the default transition while presenting UIViewController to custom如何在将 UIViewController 呈现给自定义时更改默认转换
【发布时间】: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

)

【问题讨论】:

标签: ios swift uiviewcontroller transition


【解决方案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
    

    【讨论】:

    • 这就是问题所在......这段代码的动画在呈现视图控制器时从下到上移动。我想要从上到下的动画
    猜你喜欢
    • 1970-01-01
    • 2014-10-03
    • 1970-01-01
    • 1970-01-01
    • 2018-07-17
    • 1970-01-01
    • 2016-03-17
    • 2015-01-21
    • 2017-12-30
    相关资源
    最近更新 更多