gpt4 book ai didi

swift - 自定义 UIViewControllerAnimatedTransitioning 完成后显示黑屏

转载 作者:行者123 更新时间:2023-11-28 12:14:46 34 4
gpt4 key购买 nike

我有一个 segue,它是通过一个按钮触发的,该按钮在当前上下文中模态显示另一个 View Controller 。我正在使用自定义类来自定义转换,它是这样的:

class ShowAnimator: NSObject {
}


extension ShowAnimator: UIViewControllerAnimatedTransitioning {
func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval {
return 0.5
}
func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {
guard
let fromVC = transitionContext.viewController(forKey: .from),
let toVC = transitionContext.viewController(forKey: .to) else {
return
}
let containerView = transitionContext.containerView
containerView.insertSubview(fromVC.view, belowSubview: toVC.view)
let toFrame = transitionContext.finalFrame(for: toVC)
let screenBounds = UIScreen.main.bounds
let bottomLeftCorner = CGPoint(x: screenBounds.width, y: 0)
let finalFrame = CGRect(origin: bottomLeftCorner, size: screenBounds.size)

UIView.animate(withDuration: transitionDuration(using: transitionContext),
animations: {
fromVC.view.frame = finalFrame
},
completion: { _ in
transitionContext.completeTransition(!transitionContext.transitionWasCancelled)
}
)
}
}

在我的主视图 Controller 中,我有这些相关的功能:

let showAnimator = ShowAnimator()
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
let destination = segue.destination
destination.transitioningDelegate = self
}


func animationController(forDismissed dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
return nil
}
func animationController(forPresented presented: UIViewController, presenting: UIViewController, source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
return showAnimator
}

请注意,此类是一个 ViewController 以及一个 UIViewControllerTransitioningDelegate然而,每当我按下按钮执行我的 segue 时,它​​都会正确地执行动画,但是一旦完成它就只显示黑屏,而不是下一个 ViewController。

最佳答案

一个问题是这一行:

containerView.insertSubview(fromVC.view, belowSubview: toVC.view)

您正在插入 fromVC View 。那是错的。它已经存在(尽管不一定在容器中)。

并且您没有插入toVC View 。那也是错误的。您需要将其放入容器中。

关于swift - 自定义 UIViewControllerAnimatedTransitioning 完成后显示黑屏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47127751/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com