简体   繁体   English

自定义模式演示文稿中的UIBlurEffect

[英]UIBlurEffect in a custom modal presentation

I am currently presenting a view controller over a third of the screen with a custom UIPresentationController . 我目前正在使用自定义UIPresentationController在屏幕的三分之一处呈现视图控制器。 In my UIPresentationController , I wrap the presentedViewController in a few views to get rounded corners and a shadow (like in Apple's Custom Transitions sample app). 在我UIPresentationController ,我包裹presentedViewController的一些看法得到圆角(在苹果的自定义转换的示例应用程序等)的影子。 I recently added a UIVisualEffectView with a UIBlurEffect to the presentedViewController hierarchy, but it is displaying weird. 我最近增加了一个UIVisualEffectViewUIBlurEffectpresentedViewController等级,但它显示的是怪异。 The view is now semi-transparent, but not blurred. 视图现在是半透明的,但不模糊。

I think this is because the blur effect is being applied properly, but because it's a modal presentation it is not seeing the view behind and therefor unable to blur it. 我认为这是因为模糊效果正确应用,但因为它是一个模态演示,它不会看到背后的视图,因此无法模糊它。 Any thoughts? 有什么想法吗? Here is the related code in override func presentationTransitionWillBegin() 这是override func presentationTransitionWillBegin()的相关代码

    // Wrap the presented view controller's view in an intermediate hierarchy
    // that applies a shadow and rounded corners to the top-left and top-right
    // edges.  The final effect is built using three intermediate views.
    //
    // presentationWrapperView              <- shadow
    //   |- presentationRoundedCornerView   <- rounded corners (masksToBounds)
    //        |- presentedViewControllerWrapperView
    //             |- presentedViewControllerView (presentedViewController.view)
    //
    // SEE ALSO: The note in AAPLCustomPresentationSecondViewController.m.


    let presentationWrapperView = UIView(frame: frameOfPresentedViewInContainerView)
    presentationWrapperView.layer.shadowOpacity = 0.44
    presentationWrapperView.layer.shadowRadius = 13
    presentationWrapperView.layer.shadowOffset = CGSize(width: 0, height: -6)
    self.presentationWrappingView = presentationWrapperView

    // presentationRoundedCornerView is CORNER_RADIUS points taller than the
    // height of the presented view controller's view.  This is because
    // the cornerRadius is applied to all corners of the view.  Since the
    // effect calls for only the top two corners to be rounded we size
    // the view such that the bottom CORNER_RADIUS points lie below
    // the bottom edge of the screen.
    let presentationRoundedCornerView = UIView(frame: UIEdgeInsetsInsetRect(presentationWrapperView.bounds, UIEdgeInsets(top: 0, left: 0, bottom: -CORNER_RADIUS, right: 0)))
    presentationRoundedCornerView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
    presentationRoundedCornerView.layer.cornerRadius = CORNER_RADIUS
    presentationRoundedCornerView.layer.masksToBounds = true

    // To undo the extra height added to presentationRoundedCornerView,
    // presentedViewControllerWrapperView is inset by CORNER_RADIUS points.
    // This also matches the size of presentedViewControllerWrapperView's
    // bounds to the size of -frameOfPresentedViewInContainerView.
    let presentedViewControllerWrapperView = UIView(frame: UIEdgeInsetsInsetRect(presentationRoundedCornerView.bounds, UIEdgeInsets(top: 0, left: 0, bottom: CORNER_RADIUS, right: 0)))
    presentedViewControllerWrapperView.autoresizingMask = [.flexibleWidth, .flexibleHeight]

    let blurWrapperView = UIVisualEffectView(effect: UIBlurEffect(style: .light))
    blurWrapperView.autoresizingMask =  [.flexibleWidth, .flexibleHeight]
    blurWrapperView.frame = presentedViewControllerWrapperView.bounds

    // Add presentedViewControllerView -> presentedViewControllerWrapperView.
    presentedViewControllerView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
    presentedViewControllerView.frame = blurWrapperView.bounds
    blurWrapperView.contentView.addSubview(presentedViewControllerView)

    presentedViewControllerWrapperView.addSubview(blurWrapperView)
    // Add presentedViewControllerWrapperView -> presentationRoundedCornerView.
    presentationRoundedCornerView.addSubview(presentedViewControllerWrapperView)

    // Add presentationRoundedCornerView -> presentationWrapperView.
    presentationWrapperView.addSubview(presentationRoundedCornerView)

As I said in the comment above, from my experience, playing with the presentationStyle property of the UIPresentationController allows you to define how the views behind the presented one are handled once that happen. 正如我在上面的评论中所说的,根据我的经验,使用UIPresentationControllerpresentationStyle属性可以定义一旦发生后如何处理所呈现的视图。 See here more info about it. 在这里查看更多关于它的信息。 The property itself is of type UIModalPresentationStyle and you can see it's available values here . 属性本身是UIModalPresentationStyle类型,你可以在这里看到它的可用值。

In my opinion it seems that overCurrentContext it's the value you want. 在我看来,似乎overCurrentContext它是你想要的价值。 From Apple's documentation: 来自Apple的文档:

The views beneath the presented content are not removed from the view hierarchy when the presentation finishes. 演示文稿完成后,不会从视图层次结构中删除所显示内容下方的视图。 So if the presented view controller does not fill the screen with opaque content, the underlying content shows through. 因此,如果呈现的视图控制器没有用不透明的内容填充屏幕,则底层内容会显示出来。

In my view that means that your blur view will then have something to blur. 在我看来,这意味着您的模糊视图将有一些模糊的东西。

Update 更新


Also, you could try this , too even though Apple's documentation seems off. 此外,即使Apple的文档似乎没有,你也可以试试这个 I think the default value of the property is true not false as mentioned in there. 我认为属性的默认值是truefalse在那里提到。

I'm assuming you are testing this on the simulator, right? 我假设你在模拟器上测试它,对吗? If so make sure to check the simulator's Graphic's Quality Override setting and set it to High Quality. 如果是这样,请务必检查模拟器的图形质量覆盖设置并将其设置为高质量。 That should do the trick for you. 这应该为你做的伎俩。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM