ios - 如何在不关闭父/第一个 View Controller 的情况下以模态方式呈现 View Controller

标签 ios swift animation

我正在尝试在原始 UIViewController 上为 UICollectionViewController 设置动画,并将原始 UIViewController 模糊为背景,但是,每次动画结束后,我可以通过模糊的 View 清楚地看到原始 View Controller 已被关闭,我该怎么做才能避免第一个 UIViewController 被关闭?

在第一个 View Controller 中显示第二个 View Controller 的代码:

let VC = storyboard?.instantiateViewController(withIdentifier: "PopoverCollectionVC") as! PopoverCollectionVC
VC.setDataSource(with: .calcDPSItems)
VC.collectionView?.backgroundColor = UIColor.clear
VC.transitioningDelegate = self
self.present(VC, animated: true, completion: nil)

animator 对象中用于自定义动画的代码:

func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {
    let containerView = transitionContext.containerView
    let fromView = transitionContext.view(forKey: .from)!
    let toView = transitionContext.view(forKey: .to)!

    if presenting {
        // configure blur
        effectView.frame = fromView.bounds
        containerView.addSubview(effectView)
        // configure collection view
        toView.frame = CGRect(x: 0, y: fromView.frame.height, width: fromView.frame.width, height: fromView.frame.height / 2)
        containerView.addSubview(toView)

        UIView.animateKeyframes(withDuration: duration, delay: 0, options: .calculationModeCubic, animations: {

            UIView.addKeyframe(withRelativeStartTime: 0, relativeDuration: 0.6) {
                toView.center.y = fromView.center.y
            }

            UIView.addKeyframe(withRelativeStartTime: 0.1, relativeDuration: 1) {
                self.effectView.effect = UIBlurEffect(style: .dark)
            }

        }) { _ in
            transitionContext.completeTransition(true)
        }
    } else {

        ...
    }

最佳答案

您需要指定模态呈现样式将覆盖当前上下文:

VC.modalPresentationStyle = .overCurrentContext

然后得到你需要的呈现 View

// Get the from view from The ViewController because there is a bug in iOS when 
// using some modalPresentationStyle values 
// that causes the viewForKey to returm nil for UITransitionContextFromViewKey 
// www.splinter.com.au/2015/04/17/ios8-view-controller-transiti‌​oning-bug/ 

let fromVC = transitionContext.viewController(forKey: .from) 
let fromView = fromVC?.view

关于ios - 如何在不关闭父/第一个 View Controller 的情况下以模态方式呈现 View Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41279769/

相关文章:

ios - 如何将带有应用程序组的应用程序上传到苹果商店?

ios - 在 Facebook 应用程序中通过 FBDialog 共享图像失败

iphone - NSString的前两行,给出了宽度和字体

ios - 计算 AttributeText 的宽度和高度不正确

ios - 防止在 UIImagePickerController 中选择同一张照片两次

objective-c - 使用 block 的多级动画

android - 如何知道下一页/上一页何时完全加载到 harism 的 curl 效果中?

java - 如何在 Ludo 棋盘中使用数组索引

ios - 如何在关闭应用程序后维护一个值(int 或 string)以供使用

ios - 在 Swift、iOS 中将表格 View 转换为多页 PDF 的最佳方法之一