我有一个模态 segue(带有表单表演示文稿(,每当选择tableViewCell
时,我都会调用它。我在故事板中设置了 segue,但如果需要,我可以以编程方式设置它。我希望模态视图的背景是Visual Effect View with Blur
而不是半透明的黑色。我该怎么做?感谢您的帮助!我正在使用 Swift 3。
实现两个协议来做到这一点 - 对于您的Animator
,<UIViewControllerAnimatedTransitioning>
和TransitioningDelegate
,<UIViewControllerTransitioningDelegate>
。
在Animator
中,您需要覆盖方法 animateTransition
并在那里创建UIVisualEffectView
,您将将其添加到transitionContext.containerView
class Animator: UIViewControllerAnimatedTransitioning
{
func blurEffectView(_ transitionContext: UIViewControllerContextTransitioning) -> UIVisualEffectView
{
let container = transitionContext.containerView;
var effectView = objc_getAssociatedObject(container, &BlurEffectViewKey) as? UIVisualEffectView;
if effectView == nil
{
let effect = UIBlurEffect(style: .dark)
effectView = UIVisualEffectView(effect: effect);
objc_setAssociatedObject(container, &BlurEffectViewKey, effectView!, objc_AssociationPolicy.OBJC_ASSOCIATION_ASSIGN);
container.addSubview(effectView!);
effectView!.frame = container.bounds;
}
return effectView!;
}
func animateTransition(using transitionContext: UIViewControllerContextTransitioning)
{
...
let blurView = self.blurEffectView(transitionContext);
if (presenting)
{
blurView.contentView.addSubview(toController.view);
}
...
}
...
}
然后,设置并展示模态控制器:
presentedViewController.transitioningDelegate = myTransitioningDelegate
presentedViewController.modalPresentationStyle = .overFullScreen
self.present(presentedViewController, animated:false, completion:nil)
有关更多详细信息,请参阅 WWDC'13 会议"使用视图控制器自定义过渡">