1. 程式人生 > >macOS 開發 - NSVisualEffectView

macOS 開發 - NSVisualEffectView

文章目錄


一、簡介

NSVisualEffectView 是 macOS 10.10 後開放的一個類,繼承自 UIView
提供一個模糊效果的檢視

二、相關屬性


blendingMode

NSVisualEffectBlendingMode 列舉

typedef NS_ENUM(NSInteger, NSVisualEffectBlendingMode) {
    /// Blend with the area behind the window (such as the Desktop or other windows).
    NSVisualEffectBlendingModeBehindWindow, 
    
    /// Blend with the area behind the view in the window.
    NSVisualEffectBlendingModeWithinWindow,
} NS_AVAILABLE_MAC(10_10);

Behind-window blending uses the content behind the window as the background for your visual effect view. Behind-window blending makes your entire window stand out above other windows and apps on the desktop. Sheets and popovers use behind-window blending.
In-window blending uses the window’s content as the background for your visual effect view. Typically, you use in-window blending with scrolling content, so that the scrolled content remains partially visible under other parts of your window chrome. Toolbars always use in-window blending.

Behind-window 會將視窗背後的內容,作為模糊效果的背景檢視;彈窗和表單一般使用 Behind-Window 模式;
In-Window 會將窗體內容作為模糊的背景檢視。當滾動視窗時,滾動的內容會在 visualView 下面模糊展示。工具欄 Toolbars 一般使用 In-Window 模式。

在這裡插入圖片描述


在這裡插入圖片描述


設定blendingMode 為 behindwindow,在當前視窗不顯示半透明效果;

在這裡插入圖片描述


state

typedef NS_ENUM(NSInteger, NSVisualEffectState) {
    /// Use the active look only when the containing window is active.
    NSVisualEffectStateFollowsWindowActiveState,
    
    /// Use the active look always.
    NSVisualEffectStateActive,
    
    /// Use the inactive look always.
    NSVisualEffectStateInactive,
} NS_AVAILABLE_MAC(10_10);

NSVisualEffectStateActive
啟用狀態,可以顯示半透明模糊;

NSVisualEffectStateInactive
非啟用狀態,顯示不透明

NSVisualEffectStateFollowsWindowActiveState
視窗是啟用狀態,就是啟用。

在這裡插入圖片描述


設定顏色

既然繼承自 NSView,那麼設定layer color 看看;
設定後,就沒有模糊效果了。

在這裡插入圖片描述


allowsVibrancy

當前這個類不會自動新增 vibrancy 到內容;
如果需要 vibrancy,需要建立類繼承自 NSVisualEffectView,重寫 allowsVibrancy 方法:

-(BOOL)allowsVibrancy{
    return YES;
}

Appkit 會給適當的檢視新增 vibrancy。比如 NSTextField, 會增加文字和背景的對比效果。
一般不要去修改標準檢視和控制元件的 vibrancy 屬性。


material

模糊的材料,不同的場景使用不同的材料;
簡單測試看不出區別,用的時候可以多留意細節。

NSVisualEffectMaterial 列舉

typedef NS_ENUM(NSInteger, NSVisualEffectMaterial) { 

  /// The material used by window titlebars. 
  NSVisualEffectMaterialTitlebar = 3, 

  /// The material used in some table views, menus, etc., to indicate selection. 
  NSVisualEffectMaterialSelection = 4, 

  /// The material used by menus. 
  NSVisualEffectMaterialMenu NS_ENUM_AVAILABLE_MAC(10_11) = 5, 

  /// The material used in the background of NSPopover windows. 
  NSVisualEffectMaterialPopover NS_ENUM_AVAILABLE_MAC(10_11) = 6, 

  /// The material used in the background of window sidebars. 
  NSVisualEffectMaterialSidebar NS_ENUM_AVAILABLE_MAC(10_11) = 7, 

  /// The material used in various in-line header or footer views (e.g., by NSTableView). 

  NSVisualEffectMaterialHeaderView NS_ENUM_AVAILABLE_MAC(10_14) = 10, 

  /// The material used as the background of sheet windows. 
  NSVisualEffectMaterialSheet NS_ENUM_AVAILABLE_MAC(10_14) = 11, 

  /// The material used by opaque window backgrounds. 
  NSVisualEffectMaterialWindowBackground NS_ENUM_AVAILABLE_MAC(10_14) = 12, 

  /// The material used as the background of heads-up display (HUD) windows. 
  NSVisualEffectMaterialHUDWindow NS_ENUM_AVAILABLE_MAC(10_14) = 13, 

  /// The material used as the background of full-screen modal UI. 
  NSVisualEffectMaterialFullScreenUI NS_ENUM_AVAILABLE_MAC(10_14) = 15, 

  /// The material used as the background of tool tips. 
  NSVisualEffectMaterialToolTip NS_ENUM_AVAILABLE_MAC(10_14) = 17, 

  /// The material used as the opaque background of content (e.g., by NSScrollView, NSTableView, NSCollectionView, etc.). 
  NSVisualEffectMaterialContentBackground NS_ENUM_AVAILABLE_MAC(10_14) = 18, 

  /// The material used under window backgrounds. 
  NSVisualEffectMaterialUnderWindowBackground NS_ENUM_AVAILABLE_MAC(10_14) = 21, 

  /// The material used as the background behind document pages. 
  NSVisualEffectMaterialUnderPageBackground NS_ENUM_AVAILABLE_MAC(10_14) = 22, 

  /// A default material appropriate for the view's effectiveAppearance.  You should instead choose an appropriate semantic material. 
  NSVisualEffectMaterialAppearanceBased NS_ENUM_DEPRECATED_MAC(10_10, API_TO_BE_DEPRECATED, "Use a specific semantic material instead.") = 0, 

  // Materials with specific looks.  You should instead choose an appropriate semantic material. 

  NSVisualEffectMaterialLight NS_ENUM_DEPRECATED_MAC(10_10, API_TO_BE_DEPRECATED, "Use a semantic material instead.  To force the appearance of a view hierarchy, set the `appearance` property to an appropriate NSAppearance value.") = 1, 

  NSVisualEffectMaterialDark NS_ENUM_DEPRECATED_MAC(10_10, API_TO_BE_DEPRECATED, "Use a semantic material instead.  To force the appearance of a view hierarchy, set the `appearance` property to an appropriate NSAppearance value.") = 2, 

  NSVisualEffectMaterialMediumLight NS_ENUM_DEPRECATED_MAC(10_11, API_TO_BE_DEPRECATED, "Use a semantic material instead.  To force the appearance of a view hierarchy, set the `appearance` property to an appropriate NSAppearance value.") = 8, 

  NSVisualEffectMaterialUltraDark NS_ENUM_DEPRECATED_MAC(10_11, API_TO_BE_DEPRECATED, "Use a semantic material instead.  To force the appearance of a view hierarchy, set the `appearance` property to an appropriate NSAppearance value.") = 9, 

} NS_AVAILABLE_MAC(10_10); 


interiorBackgroundStyle(只讀)

NSBackgroundStyle 列舉

typedef NS_ENUM(NSInteger, NSBackgroundStyle) { 

  /* The background reflects the predominant color scheme of the view's appearance. */ 

  NSBackgroundStyleNormal = 0, 

  /* The background is indicating emphasis (e.g. selection state) using an alternate color or visual effect. Content may alter its appearance to reflect this emphasis. */ 

  NSBackgroundStyleEmphasized,

  /* The background is intended to appear higher than the content drawn on it. Content might need to be inset. */ 

  NSBackgroundStyleRaised,

  /* The background is intended to appear lower than the content drawn on it. Content might need to be embossed. */ 

  NSBackgroundStyleLowered,
}