快速自定义过渡不起作用(未调用UIViewControllerTransitioningDelegate)

时间:2018-08-02 21:24:23

标签: swift animation transition transitions uiviewanimationtransition

我只是想切换到另一个视图控制器。我的HomeViewController具有以下扩展名:

extension HomeViewController: UIViewControllerTransitioningDelegate {
    func animationController(forPresented presented: UIViewController,
                             presenting: UIViewController,
                             source: UIViewController)
        -> UIViewControllerAnimatedTransitioning? {
        return transition
    }

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

和类中定义的过渡

let transition = PopAnimator()

我有一个按钮,点击该按钮时应切换视图控制器并使用自定义过渡(PopAnimator):

@objc func handleTap() {
        let viewController = ViewController()
        viewController.transitioningDelegate = self
        self.present(viewController, animated: false, completion: nil)
    }

PopAnimator类(目前还只是一个fadeIn):

class PopAnimator: NSObject, UIViewControllerAnimatedTransitioning {
    let duration = 1.0
    var presenting = true
    var originFrame = CGRect.zero

    func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval {
        return duration
    }

    func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {
        let containerView = transitionContext.containerView

        let toView = transitionContext.view(forKey: .to)!

        containerView.addSubview(toView)
        toView.alpha = 0.0
        UIView.animate(withDuration: duration,
        animations: {
            toView.alpha = 1.0
        },
        completion: { _ in
            transitionContext.completeTransition(true)
        }
        )
    }
}

当我单击按钮时,它将切换viewControllers,但不使用我的自定义过渡。如果在我的animationController(forPresented:presenting:source :)函数或我的PopAnimator类中添加了一个断点,则无法实现。

1 个答案:

答案 0 :(得分:0)

        self.present(viewController, animated: false, completion: nil)

如果要动画过渡,请尝试设置animated: true