我正在尝试在原始 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-transitioning-bug/
let fromVC = transitionContext.viewController(forKey: .from)
let fromView = fromVC?.view
关于ios - 如何在不关闭父/第一个 View Controller 的情况下以模态方式呈现 View Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41279769/