如何在不关闭父/第一个视图控制器的情况下将视图控制器呈现



我正在尝试通过原始UIViewController使用原始 UIViewController模糊为背景来使UICollectionViewController动画,但是,每当动画完成时,我都可以通过模糊的视图清楚地看到原始视图控制器被驳回,我该怎么做才能避免第一个UIViewController被驳回?

第一个视图控制器中的代码显示第二个:

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)

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

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

然后获得所需的视图

// 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

您应该拍摄先前视图的快照,然后模糊,然后将其用作新VC的背景。

 let oldView = view_to_copy.snapshotView(afterScreenUpdates: false)

然后模糊此,然后添加为CollectionView Controller的子视图。

编辑:

仅添加如果您在uitabbar或uinavigation controller之类的内部使用VC,则可能需要快照该视图以确保所有UI都在快照内。

最新更新