2011-05-13 123 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財產。所有重要的標誌都在所提到的類中正確設置。