Swift 自定义过渡不起作用(未调用 UIViewControllerTransitioningDelegate)
Swift Custom Transition Not Working (UIViewControllerTransitioningDelegate not called)
我只是想切换到第二个视图控制器。我的 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
}
}
和class
中定义的转换
let transition = PopAnimator()
我有一个按钮,当它被点击时应该切换视图控制器并使用自定义过渡 (PopAnimator):
@objc func handleTap() {
let viewController = ViewController()
viewController.transitioningDelegate = self
self.present(viewController, animated: false, completion: nil)
}
PopAnimator class(现在真的只是淡入):
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 class 中放置一个断点,它就不会到达。
self.present(viewController, animated: false, completion: nil)
如果您想要动画过渡,请尝试设置 animated: true
我只是想切换到第二个视图控制器。我的 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
}
}
和class
中定义的转换let transition = PopAnimator()
我有一个按钮,当它被点击时应该切换视图控制器并使用自定义过渡 (PopAnimator):
@objc func handleTap() {
let viewController = ViewController()
viewController.transitioningDelegate = self
self.present(viewController, animated: false, completion: nil)
}
PopAnimator class(现在真的只是淡入):
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 class 中放置一个断点,它就不会到达。
self.present(viewController, animated: false, completion: nil)
如果您想要动画过渡,请尝试设置 animated: true