如何在不关闭父/第一视图控制器的情况下以模态方式呈现视图控制器

时间:2016-12-22 09:28:20

标签: ios swift animation

我尝试将原始UICollectionViewController上的原始UIViewController设置为UIViewController,并将原始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 {

        ...
    }

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

spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
     Model model = (Model) parent.getItemAtPosition(position);
     // do something with the model
}
@Override
public void onNothingSelected(AdapterView<?> parent) {

}});

2 个答案:

答案 0 :(得分:0)

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

 let oldView = view_to_copy.snapshotView(afterScreenUpdates: false)

然后将其模糊,并将其作为子视图添加到collectionView控制器。

编辑:

如果您在UITabbar或UINavigationController之类的内容中使用VC,您可能需要对该视图进行快照以确保所有UI都在快照中。

答案 1 :(得分:0)

您需要指定模态演示文稿样式将在当前上下文中:

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