如何将UIViewController呈现给自定义时更改默认过渡

问题描述 投票:0回答:2

我正在尝试从当前视图控制器中呈现另一个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
2个回答
4
投票

处理动画的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 !!!


-1
投票

我认为以下内容可以工作

let editCategoryVC = self.storyboard?.instantiateViewControllerWithIdentifier("editCategoryVC") as editCategoryVC
self.presentViewController(editCategoryVC, animated:true, completion: nil
© www.soinside.com 2019 - 2024. All rights reserved.