ios - containerView的自定义动画中的modalPresentationStyle .overCurrentContext返回黑屏
问题描述
我在另一个 viewController 的 containerView 中有一个 tableViewController。
当点击图像时,会发生自定义转换到另一个 viewController 以查看图像。过渡效果很好。
当自定义转换发生时,虽然在 containerView 的 tableViewController 上调用了 viewWillDisappear,然后当呈现的 viewController 被解除(再次使用转换)时再次调用 viewWillAppear。
这会导致 tableView 中偶尔出现一些异常滚动。
我想要的是向 imageViewer 呈现过渡但 overCurrentContext。
我已经尝试过人们的建议如下:
self.definesPresentationContext = true
vc.modalPresentationStyle = .overCurrentContext
vc.transitioningDelegate = self
self.present(vc, animated: true, completion: nil)
不幸的是,这不起作用,当 imageViewController 被解除时,最初的过渡是正确的,并且可以看到 tableViewController。但是一旦过渡完成,视图就会变黑。
我认为这可能与我使用的是 containerView 的事实有关。但不确定也不知道如何解决这个问题。
这是我正在使用的动画方法:
private var image: UIImage?
private var fromDelegate: ImageTransitionProtocol?
private var toDelegate: ImageTransitionProtocol?
// MARK: Setup Methods
func setupImageTransition(image: UIImage, fromDelegate: ImageTransitionProtocol, toDelegate: ImageTransitionProtocol) {
self.image = image
self.fromDelegate = fromDelegate
self.toDelegate = toDelegate
}
// MARK: UIViewControllerAnimatedTransitioning
// 1: Set animation speed
func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval {
return 0.4
}
func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {
let containerView = transitionContext.containerView
// 2: Get view controllers involved
let fromVC = transitionContext.viewController(forKey: UITransitionContextViewControllerKey.from)!
let toVC = transitionContext.viewController(forKey: UITransitionContextViewControllerKey.to)!
// 3: Set the destination view controllers frame
toVC.view.frame = fromVC.view.frame
// 4: Create transition image view
let imageView = UIImageView(image: image)
imageView.contentMode = .scaleAspectFill
imageView.frame = (fromDelegate == nil) ? CGRect() : fromDelegate!.imageWindowFrame()
imageView.clipsToBounds = true
containerView.addSubview(imageView)
// 5: Create from screen snapshot
fromDelegate!.transitionSetup()
toDelegate!.transitionSetup()
let fromSnapshot = fromVC.view.snapshotView(afterScreenUpdates: true)!
fromSnapshot.frame = fromVC.view.frame
containerView.addSubview(fromSnapshot)
// 6: Create to screen snapshot
let toSnapshot = toVC.view.snapshotView(afterScreenUpdates: true)!
toSnapshot.frame = fromVC.view.frame
containerView.addSubview(toSnapshot)
toSnapshot.alpha = 0
// 7: Bring the image view to the front and get the final frame
containerView.bringSubview(toFront: imageView)
let toFrame = (self.toDelegate == nil) ? CGRect() : self.toDelegate!.imageWindowFrame()
// 8: Animate change
UIView.animate(withDuration: transitionDuration(using: transitionContext), delay: 0, usingSpringWithDamping: 0.7, initialSpringVelocity: 0.8, options: .curveEaseOut, animations: {
toSnapshot.alpha = 1
imageView.frame = toFrame
}, completion:{ [weak self] (finished) in
self?.toDelegate!.transitionCleanup()
self?.fromDelegate!.transitionCleanup()
// 9: Remove transition views
imageView.removeFromSuperview()
fromSnapshot.removeFromSuperview()
toSnapshot.removeFromSuperview()
// 10: Complete transition
if !transitionContext.transitionWasCancelled {
containerView.addSubview(toVC.view)
}
transitionContext.completeTransition(!transitionContext.transitionWasCancelled)
})
}
这是在呈现的 viewController 中:
func animationController(forPresented presented: UIViewController, presenting: UIViewController, source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
let imagePresentationViewController = (presented as! UINavigationController).topViewController as! ImagePresentationViewController
self.transition.setupImageTransition( image: pickedImageForTransition!, imageType: nil,
fromDelegate: self,
toDelegate: imagePresentationViewController)
return transition
}
func animationController(forDismissed dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
let imagePresentationViewController = (dismissed as! UINavigationController).topViewController as! ImagePresentationViewController
transition.setupImageTransition( image: pickedImageForTransition!, imageType: .listsImage,
fromDelegate: imagePresentationViewController,
toDelegate: self)
return transition
}
解决方案
有同样的问题,你只需要从中删除容器视图func animateTransition(using transitionContext: UIViewControllerContextTransitioning)
推荐阅读
- java - 单击树外的任何位置时从 JTree 中取消选择节点
- firebase - 如何在反应原生 Firebase 动态链接中从收到的链接中提取参数?
- postgresql - POSTGRESQL:如何在数组列上连接表?
- docker - 构建docker文件时无法获取依赖包
- database - 这是在BCNF吗?
- xml - 如何为 xml 连接多个属性值?
- react-native - 在 react-native-debugger 中配置 redux 存储的初始状态?
- python - 如何将数据库中的值显示到 QlineEdit pyqt4 python
- ruby - 如何计算账单年度的 4 周期间
- javascript - TypeScript -> 浏览器的 JavaScript:exports/require 语句