如何从顶部以模态方式呈现视图控制器,就像解除样式一样?

时间:2018-01-21 10:23:57

标签: ios swift swift4

如何从顶部以模态方式呈现视图控制器?我想呈现我的视图控制器就像解散样式...我已经尝试过CATransition但它没有给我相同的动画持续时间。有什么帮助吗?

CATransition方法:

let vc = storyboard?.instantiateViewController(withIdentifier: "DownloadAudio") as! DownloadAudioViewController 

let transition = CATransition() 
transition.duration = 0.25 
transition.type = kCATransitionPush 
transition.subtype = kCATransitionFromBottom 
transition.timingFunction = CAMediaTimingFunction(name:kCAMediaTimingFunctionEaseInEaseO‌​ut) 

view.window!.layer.add(transition, forKey: kCATransition) 
present(vc, animated: false, completion: nil)

1 个答案:

答案 0 :(得分:1)

您需要创建一个自定义演示文稿,方法是创建一个像现在这样的对象,该对象可以呈现当前动画对象并将其解除:

class PresentationObjectVendor: NSObject, UIViewControllerTransitioningDelegate {

    func animationController(forPresented presented: UIViewController, presenting: UIViewController, source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        return CustomDropPresentAnimator()
    }

    func animationController(forDismissed dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        return CustomDropDismissAnimator()
    }

}

然后创建这些动画对象,如下所示:

// present animator
class CustomDropPresentAnimator: NSObject, UIViewControllerAnimatedTransitioning {

    func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval {
        // return a time interval
    }

    func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {
        // perform animations
    }

}

// dismiss animator
class CustomDropDismissAnimator: NSObject, UIViewControllerAnimatedTransitioning {

    func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval {
        // return a time interval
    }

    func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {
        // perform animations
    }

}