【问题标题】:Blurred background underneath an NSViewNSView 下的背景模糊
【发布时间】:2011-05-13 19:40:17
【问题描述】:

我一直在环顾四周,但找不到答案。我正在尝试制作一个可以正常显示其内容的视图,但是它下面的任何东西(在 z 轴上)都会变得模糊。

我似乎无法立即做到这一点。我尝试的任何东西(充其量)都会模糊视图的内容,而不是下面的内容。

【问题讨论】:

    标签: objective-c cocoa


    【解决方案1】:

    查看这篇关于 Cocoanetics 的文章,它为您提供了一些说明如何正确设置具有模糊背景的 NSViewhttp://www.cocoanetics.com/2013/10/blurring-views-on-mac/

    Github 上还有一个 RMBlurredView 类:https://github.com/raffael/RMBlurredView

    其背后的想法是使用层支持的 NSView,并在 backgroundFilters 属性上应用 CIGaussianBlur CIFilter。在提到的类中所有重要的标志都正确设置。

    【讨论】:

      【解决方案2】:

      如果您使用明显小于视图框架的图像并将图像放大,则会变得模糊。

      您可以使用 CALayers。声明一个根层并将背景图像添加为子层。将“内容”放在具有透明背景的子视图中。 (否则“内容”可能会被模糊的子层遮挡。)

      这是设置子层的部分(未经测试)代码:

       // Set up the root layer.
      [[self.aViewController view] setLayer:[CALayer layer]];
      [[self.aViewController view] setWantsLayer:YES];
      // Set up a sublayer.
      CALayer *blurredSublayer = [CALayer layer];
       NSRect rectOfView = [self.aViewController view] frame];
       blurredSublayer.bounds = rectOfView;
      // Views are usually positioned from their lower left corner, but CALayers are positioned from their center point.
      blurredSublayer.position = CGPointMake(rectOfView.size.width/2, rectOfView.size.height/2);
      
       // Set the sublayer's contents property to the image you've chosen. You might need to do the scaling when you set up the CGImageRef used by the contents property. (I haven't done this; I leave it to you.)
      
       // Add the sublayer to the view's root layer.
      [self.aViewController.view.layer addSublayer:blurredSublayer];
      
       // You'll probably want to save expense by calling setWantsLayer:NO for the contents-bearing subview, since it will have been turned on when you set up the superview's root layer.
      

      或者使用 CGContext 可能更容易。但是有了 CALayer,你就有了你提到的 zPosition 属性。

      附: “内容”和“超级视图”和“子层”的使用使结构混乱。这是一个描述性的层次结构。第一个命名的项目在底部,最后一个命名的项目在顶部,就像在 IB 中一样:

      • 带有根层的超级视图
      • superview 的模糊子层(子层的内容属性是放大的图像)
      • subview/s(文本字段或您想到的任何“内容”)

      【讨论】:

        猜你喜欢
        • 2023-04-05
        • 2018-04-24
        • 2013-10-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-01-15
        • 2017-07-17
        • 1970-01-01
        相关资源
        最近更新 更多