在NSView下面的模糊的背景

Puc*_*cho 5 cocoa objective-c

我一直在四处寻找,但我找不到答案.我正在尝试制作一个能够正常显示其内容的视图,但是在它下面(在z轴上)的任何东西都会模糊不清.

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

Wie*_*nke 0

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

您可以使用 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.
Run Code Online (Sandbox Code Playgroud)

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

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

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