【问题标题】:Swift Custom Transition Not Working (UIViewControllerTransitioningDelegate not called)Swift 自定义转换不起作用(未调用 UIViewControllerTransitioningDelegate)
【发布时间】:2018-08-02 21:24:23
【问题描述】:

我只是想切换到第二个视图控制器。我的 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 类中设置断点,则无法到达。

【问题讨论】:

    标签: swift animation transition transitions uiviewanimationtransition


    【解决方案1】:
            self.present(viewController, animated: false, completion: nil)
    

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-08-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-09-10
      • 2021-04-29
      • 2021-08-28
      相关资源
      最近更新 更多