显示导航控制器时在动画中自定义幻灯片根本无法快速工作

Posted

技术标签:

【中文标题】显示导航控制器时在动画中自定义幻灯片根本无法快速工作【英文标题】:Custom Slide in animation when presenting navigation controller not working at all in swift 【发布时间】:2017-05-01 23:26:04 【问题描述】:

所以我一直在关注一些关于如何在呈现视图控制器时执行自定义动画的教程。我目前有以下,

    一个名为 TransitionManager 的类将在 ViewController A 中实例化,以呈现视图 ViewController A 将在导航控制器中显示 ViewController B

根据我在视图演示中设置委托后阅读的所有教程,我应该会看到我的自定义转换。但是,仍使用默认动画。我尝试在演示前后移动代理的设置,但无济于事

转换管理器类

class TransitionManager: NSObject, UIViewControllerAnimatedTransitioning, UIViewControllerTransitioningDelegate 
    func animateTransition(using transitionContext: UIViewControllerContextTransitioning) 
        // get reference to our fromView, toView and the container view that we should perform the transition in
    let container = transitionContext.containerView
    let fromView = transitionContext.view(forKey: .from)!
    let toView = transitionContext.view(forKey: .to)!

    // set up from 2D transforms that we'll use in the animation
    let offScreenRight = CGAffineTransform(translationX: container.frame.width, y: 0)
    let offScreenLeft = CGAffineTransform(translationX: -container.frame.width, y: 0)

    // start the toView to the right of the screen
    if self.presenting 
        toView.transform = offScreenRight
     else 
        toView.transform = offScreenLeft
    


    // add the both views to our view controller
    container.addSubview(toView)
    container.addSubview(fromView)

    // get the duration of the animation
    // DON'T just type '0.5s' -- the reason why won't make sense until the next post
    // but for now it's important to just follow this approach
    let duration = self.transitionDuration(using: transitionContext)

    // perform the animation!
    // for this example, just slid both fromView and toView to the left at the same time
    // meaning fromView is pushed off the screen and toView slides into view
    // we also use the block animation usingSpringWithDamping for a little bounce
    UIView.animate(withDuration: duration, delay: 0.0, usingSpringWithDamping: 0.5, initialSpringVelocity: 0.8, options: [], animations: 

        if self.presenting 
            toView.transform = offScreenLeft
         else 
            toView.transform = offScreenRight
        
        toView.transform = .identity

    , completion:  finished in

        // tell our transitionContext object that we've finished animating
        transitionContext.completeTransition(true)

    )
    

func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval 
    return 0.5


// MARK: UIViewControllerTransitioningDelegate protocol methods

// return the animataor when presenting a viewcontroller
func animationControllerForPresentedController(presented: UIViewController, presentingController presenting: UIViewController, sourceController source: UIViewController) -> UIViewControllerAnimatedTransitioning? 
    self.presenting = true
    return self


// return the animator used when dismissing from a viewcontroller
func animationControllerForDismissedController(dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? 
    self.presenting = false
    return self



类 ViewController A

class ViewControllerA: UIViewController 
let transitionManager = TransitionManager()
func addButtonSelected() 
        let vc = ViewControllerB()
        let nav = UINavigationController(rootViewController: vc)
        present(nav, animated: true, completion: nil)
        nav.transitioningDelegate = self.transitionManager

    

【问题讨论】:

我们无法从perform slide in animation 收集到太多信息。显示您的代码。 ok 在编辑中添加 你需要在之前分配transitioningDelegate 【参考方案1】:

将您的方法更新为 swift3-:

func animationController(forDismissed dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? 
    return Transition()


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

【讨论】:

【参考方案2】:

您需要将modalPresentationStyle 设置为.custom,在文档here 和here 中提到。我不确定这是否重要,但我也总是在调用 present 之前设置转换委托:

nav.modalPresentationStyle = .custom
nav.transitioningDelegate = self.transitionManager
present(nav, animated: true, completion: nil)

【讨论】:

嗯,我按照建议做了,但过渡没有区别。它仍在向我展示默认的演示动画,从下到上:(

以上是关于显示导航控制器时在动画中自定义幻灯片根本无法快速工作的主要内容,如果未能解决你的问题,请参考以下文章

路由器导航幻灯片动画不起作用

如何在 Swift iOS 中自定义导航控制器

快速视频中的照片上的自定义动画

在 iOS 导航控制器的代码中自定义图像大小

UITableView 中自定义单元格中的导航控制器和 UIImageView

在ios中自定义导航栏