【问题标题】:UIBlurEffect in a custom modal presentation自定义模态演示中的 UIBlurEffect
【发布时间】:2016-08-22 19:57:11
【问题描述】:

我目前正在使用自定义UIPresentationController 在屏幕的三分之一上展示一个视图控制器。在我的UIPresentationController 中,我将presentedViewController 包裹在几个视图中以获得圆角和阴影(就像在Apple 的自定义过渡示例应用程序中一样)。我最近在presentedViewController 层次结构中添加了带有UIBlurEffectUIVisualEffectView,但它显示得很奇怪。视图现在是半透明的,但不是模糊的。

我认为这是因为正确应用了模糊效果,但因为它是模态演示,它看不到后面的视图,因此无法模糊它。有什么想法吗?这是相关的代码 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)

【问题讨论】:

  • 我看到了完全相同的行为。你有没有想过如何解决这个问题?
  • 尝试将 UIPresentationController 的 presentationStyle 更改为 overCurrentContext

标签: ios swift uivisualeffectview uimodalpresentationcustom


【解决方案1】:

正如我在上面的评论中所说,根据我的经验,使用UIPresentationControllerpresentationStyle 属性可以定义一旦发生这种情况,如何处理呈现的视图背后的视图。有关它的更多信息,请参阅here。该属性本身的类型为UIModalPresentationStyle,您可以看到它的可用值here

在我看来,overCurrentContext 似乎是您想要的值。来自 Apple 的文档:

演示结束时,演示内容下方的视图不会从视图层次结构中删除。因此,如果呈现的视图控制器没有用不透明的内容填充屏幕,则底层内容会显示出来。

在我看来,这意味着您的模糊视图将会有一些需要模糊的东西。

更新


此外,您也可以尝试this,即使 Apple 的文档似乎已关闭。我认为该属性的默认值是true 而不是那里提到的false

【讨论】:

  • 我发现重写 UIPresentationController 的presentationStyle 属性很危险。我相信这只是一个代理,所以我们可能会打破内部假设。反正我试过了,没有成功。后面的视图正确显示,但未应用模糊效果。相反,视图是半透明的。
【解决方案2】:

我假设您正在模拟器上进行测试,对吧?如果是这样,请确保检查模拟器的 Graphic's Quality Override 设置并将其设置为 High Quality。这应该对你有用。

【讨论】:

    猜你喜欢
    • 2015-03-03
    • 2019-11-26
    • 1970-01-01
    • 1970-01-01
    • 2021-07-14
    • 1970-01-01
    • 2013-05-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多