gpt4 book ai didi

swift - animateTransition 不再适用于 iOS 13 中以模态呈现的 viewController

转载 作者:行者123 更新时间:2023-12-05 07:18:36 25 4
gpt4 key购买 nike

当用户在 tableViewController 的单元格中选择 imageView 到显示放大图像的焦点 View 时,我一直在我的自定义 Animator 类中使用以下方法进行转换。 imageView 很好地从它在 tableView 中的框架动画到全屏,然后在我关闭它时返回。

这在 iOS 13 之前工作正常。但是现在当我在模态呈现的 tableViewController 上使用这个动画转换时,焦点 View 的动画效果很好但是当我关闭焦点 View 以动画回到 tableViewController 时,tableViewController 是不再存在。它只显示了在模态呈现的 tableViewController 下方的灰色 viewController。我知道新的 modalPresentation 默认样式是 pageSheet 但不确定为什么动画之前的顶 View Controller 消失了?

动画对于未模态呈现的 viewController 仍然可以正常工作。

想知道这是一个错误还是有人找到了解决方案?

这是我的 animateTransition 函数:

func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {
// define variable to keep track of whether transitioning from or to
var presentingImage = false

// define containerView
let containerView = transitionContext.containerView

// get view controllers
let fromVC = transitionContext.viewController(forKey: UITransitionContextViewControllerKey.from)!
let toVC = transitionContext.viewController(forKey: UITransitionContextViewControllerKey.to)!

// set the destination view controllers frame
toVC.view.frame = fromVC.view.frame

// create transition imageView
let imageView = UIImageView(image: image)
imageView.contentMode = .scaleAspectFill
imageView.frame = (fromDelegate == nil) ? CGRect() : fromDelegate!.imageWindowFrame()
imageView.clipsToBounds = true

// add imageView to containerView
containerView.addSubview(imageView)

// create from screen snapshot
fromDelegate!.transitionSetup(presentingImage: presentingImage)
toDelegate!.transitionSetup(presentingImage: presentingImage)
let fromSnapshot = fromVC.view.snapshotView(afterScreenUpdates: true)!
fromSnapshot.frame = fromVC.view.frame
containerView.addSubview(fromSnapshot)

// create to screen snapshot
let toSnapshot = toVC.view.snapshotView(afterScreenUpdates: true)!
toSnapshot.frame = fromVC.view.frame
containerView.addSubview(toSnapshot)
toSnapshot.alpha = 0

// bring the image view to the front and get the final frame
containerView.bringSubviewToFront(imageView)
let toFrame = (self.toDelegate == nil) ? CGRect() : self.toDelegate!.imageWindowFrame()

// animate change
UIView.animate(withDuration: transitionDuration(using: transitionContext), delay: 0, usingSpringWithDamping: 0.7, initialSpringVelocity: 0.8, options: .curveEaseOut, animations: {

// set toSnapshot alpha to 1
toSnapshot.alpha = 1

// set imageView frame to toFrame
imageView.frame = toFrame

}, completion:{ (finished) in

// call transition cleanup for to and from delegate
self.toDelegate!.transitionCleanup(presentingImage: presentingImage)
self.fromDelegate!.transitionCleanup(presentingImage: presentingImage)

// remove transition views
imageView.removeFromSuperview()
fromSnapshot.removeFromSuperview()
toSnapshot.removeFromSuperview()

// complete transition
if !transitionContext.transitionWasCancelled {
containerView.addSubview(toVC.view)
}
transitionContext.completeTransition(!transitionContext.transitionWasCancelled)
})
}

最佳答案

上面的答案是将 modalPresentationStyle 设置为 .fullScreen 是正确的,但是还值得一提的是,如果您的 View Controller 嵌入在 UINavigationController 中,则需要在 View Controller 上进行设置:

viewController.modalPresentationStyle = .fullScreen

关于swift - animateTransition 不再适用于 iOS 13 中以模态呈现的 viewController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58248687/

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