2011-05-13 112 views
5

我一直在环顾四周,但无法找到答案。我试图制作一个视图来正常显示它的内容,但是它下面的任何东西(在z轴上)都会模糊不清。NSView下的模糊背景

我似乎无法设法做到这一点。我试过的任何东西(最好)模糊了视图的内容,而不是内容。

回答

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. 

或者,它可能更容易使用CGContext上。但是对于CALayer,你有你提到的zPosition属性。

P.S. “内容”和“超级观点”和“子层”的使用使结构混乱。这是一个描述性的层次结构。排名首位的项目是在底部,最后命名之上,因为这将是在IB:

  • 上海华与根层
  • 上海华盈的模糊子层(子层的内容属性被放大的图像)
  • 子视图
2

退房这篇文章Cocoanetics /秒(无论你脑子里的“内容”文本框或),它给你一些指导如何正确设置的NSView与模糊的背景http://www.cocoanetics.com/2013/10/blurring-views-on-mac/

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

其背后的想法是使用一个层支持的NSView与CIGaussianBlur CIFilter应用在backgroundFilters财产。所有重要的标志都在所提到的类中正确设置。