Пользовательский переход Swift не работает (UIViewControllerTransitioningDelegate не вызывается)

Я просто пытаюсь переключиться на второй контроллер представления. У меня есть 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 (на данный момент это просто затухание):

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, она не будет достигнута.


person Connor V    schedule 02.08.2018    source источник


Ответы (1)


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

попробуйте установить animated: true, если вы хотите анимированный переход

person mas'an    schedule 02.08.2018