天天看點

Mac應用程式設計指南之四——實作全屏體驗

Implementing the Full-Screen Experience

實作全屏體驗

Mac應用程式設計指南之四——實作全屏體驗

Enabling a window of your app to assume full-screen mode, taking over the entire screen, provides users with a more immersive, cinematic experience. Full-screen appearance can be striking and can make your app stand out. From a practical standpoint, full-screen mode presents a better view of users’ data, enabling them to concentrate fully on their content without the distractions of other apps or the desktop.

使應用的一個視窗采用全屏模式,占據整個螢幕,可為使用者提供更加沉浸式、影院式的體驗。全屏外觀引人注目,使應用更加突出。從實用的角度來看,全屏模式呈現了更好的使用者資料視圖,使使用者能夠完全集中于他們的内容,而不受其它應用和桌面的打擾。

In full-screen mode, by default, the menu bar and Dock are autohidden; that is, they are normally hidden but reappear when the user moves the pointer to the top or bottom of the screen. A full-screen window does not draw its titlebar and may have special handling for its toolbar.

The full-screen experience makes sense for many apps but not for all. For example, the Finder, Address Book, and Calculator would not provide any benefit to users by assuming full-screen mode. The same is probably true for most utility apps. Media-rich apps, on the other hand, can often benefit from full-screen presentation.

在全屏模式中,菜單欄和Dock預設自動隐藏;也就是說,它們通常隐藏,而當使用者将指針移動到螢幕的頂部或底部時,它們會再次出現。全屏視窗不繪制其标題欄,還可能對其工具欄做特殊處理。全屏體驗對許多應用有意義,但并非全部。例如,Finder、通訊錄、電腦采用全屏模式,不會為使用者提供任何便利。對大多數實用程式來說,情況也大概相似。從另一方面來說,富媒體應用通常會從全屏模式中受益。

Beginning with OS X v10.7, Cocoa includes support for full-screen mode through APIs in NSApplication, NSWindow, and NSWindowDelegate protocol. When the user chooses to enter full-screen mode, Cocoa dynamically creates a space and puts the window into that space. This behavior enables the user to have one window of an app running in full-screen mode in one space, while using other windows of that app, as well as other apps, on the desktop in other spaces. While in full-screen mode, the user can switch between windows in the current app or switch apps.

從OS X v10.7開始,Cocoa通過NSApplication、NSWindow和NSWindowDelegate協定中的API包含了對全屏模式的支援。當使用者選擇進入全屏模式時,Cocoa動态建立一個空間,并把視窗放到那個空間中去。這種特性使使用者可在一個空間中擁有一個全屏模式運作的應用視窗,同時還可在其它空間中的桌面上使用該應用的其它視窗或者其它應用。在全屏模式期間,使用者可在目前應用間切換視窗,也可切換應用。

Apps that have implemented full-screen user interfaces in previous versions of OS X should consider standardizing on the full-screen APIs in OS X v10.7.

在之前版本的OS X中實作了全屏使用者界面的應用,應當考慮采用OS X v10.7中的全屏API進行标準化。

Full-Screen API in NSApplication

NSApplication中的全屏API

Full-screen support in NSApplication is provided by the presentation option NSApplicationPresentationFullScreen. You can find the current presentation mode via the NSApplication method currentSystemPresentationOptions, which is also key-value observable. You can set the presentation options using the NSApplication method setPresentationOptions:. (Be sure to observe the restrictions on presentation option combinations documented with NSApplicationPresentationOptions, and set the presentation options in a try-catch block to ensure that your program does not crash from an invalid combination of options.)

NSApplication中的全屏支援是由NSApplicationPresentationFullScreen呈現選項提供的。你可以通過NSApplication的currentSystemPresentationOptions方法來查找目前呈現方式,這個方法也是鍵值觀察的。可使用NSApplication的setPresentationOptions:方法來設定呈現選項。(務必仔細觀察NSApplicationPresentationOptions文檔中注明的呈現選項組合,并在一個塊中設定呈現選項,以確定應用不會因使用無效的選項組合而崩潰。)

A window delegate may also specify that the window toolbar be removed from the window in full-screen mode and be shown automatically with the menu bar by including NSApplicationPresentationAutoHideToolbar in the presentation options returned from the window:willUseFullScreenPresentationOptions: method of NSWindowDelegate.

視窗代理還需要明确是,在全屏模式下,視窗工具欄應從視窗移除,并與菜單欄一塊自動顯示。實作方式是在NSWindowDelegate的window:willUseFullScreenPresentationOptions:方法傳回的呈現選項中包含NSApplicationPresentationAutoHideToolbar。

Full-Screen API in NSWindow

NSWindow中的全屏API

The app must specify whether a given window can enter full-screen mode. Apps can set collection behavior using the setCollectionBehavior: method by passing in various constants, and the current options may be accessed via the collectionBehavior method. You can choose between two constants to override the window collection behavior, as shown in the following table:

應用必須指定一個給定視窗是否可進入全屏模式。應用可使用setCollectionBehavior:方法、并傳入不同的常量來設定集合特性,目前選項可通過collectionBehavior方法通路。可在兩個常量之間選擇其一來重寫視窗集合特性,如下表所示:

Constant Behavior
NSWindowCollectionBehaviorFullScreenPrimary

The frontmost window with this collection behavior becomes the full-screen window. A window with this collection behavior has a full-screen button in the upper right of its titlebar.

擁有這個集合特性的最前端視窗會成為全屏視窗。一個擁有這個集合特性的視窗在其标題欄的右上角會有一個全屏按鈕。

NSWindowCollectionBehaviorFullScreenAuxiliary

Windows with this collection behavior can be shown in the same space with the full-screen window.

擁有這個集合特性的視窗可與全屏視窗在同一空間顯示。

When a window goes into full-screen mode, the styleMask changes to NSFullScreenWindowMask to reflect the state of the window.The setting of the styleMask goes through the setStyleMask: method. As a result, a window can override this method if it has customization to do when entering or exiting full-screen.

當一個視窗進入全屏模式時,styleMask屬性會變為NSFullScreenWindowMask,以此來反映視窗的狀态。styleMask屬性通過setStyleMask:方法設定。是以,如果視窗在進入或退出全屏時有自定義事件要完成,可重寫這個方法。

A window can be taken into or out of full-screen mode using the toggleFullScreen: method. If an app supports full-screen mode, it should add a menu item to the View menu with toggleFullScreen: as the action, and nil as the target.

使用toggleFullScreen:方法可将視窗帶入或帶出全屏模式。如果應用支援全屏模式,應當在視圖菜單中增加一個菜單項,這個菜單項應以toggleFullScreen:方法為操作,以nil為目标。

Full-Screen API in NSWindowDelegate Protocol

NSWindowDelegate協定中的全屏API

The following notifications are sent before and after the window enters and exits full-screen mode:

以下通知會在視窗進入和退出全屏模式之前和之後被發送:

  • NSWindowWillEnterFullScreenNotification
  • NSWindowDidEnterFullScreenNotification
  • NSWindowWillExitFullScreenNotification
  • NSWindowDidExitFullScreenNotification

The window delegate has the following corresponding window delegate notification methods:

視窗代理擁有下列對應的視窗代理通知方法:

  • windowWillEnterFullScreen:
  • windowDidEnterFullScreen:
  • windowWillExitFullScreen:
  • windowDidExitFullScreen:

The NSWindowDelegate protocol methods supporting full-screen mode are listed in Table 3-1.

支援全屏模式的NSWindowDelegate協定方法在表3-1中列出。

Table 3-1  Window delegate methods supporting full-screen mode

表3-1 支援全屏模式的視窗代理方法

Method Description
window:willUseFullScreenContentSize:

Invoked to allow the delegate to modify the full-screen content size.

調用這個方法來允許代理修改全屏内容的大小。

window:willUseFullScreenPresentationOptions:

Returns the presentation options the window will use when transitioning to full-screen mode.

傳回視窗過渡到全屏模式時使用的呈現選項。

customWindowsToEnterFullScreenForWindow:

Invoked when the window is about to enter full-screen mode. The window delegate can implement this method to customize the animation by returning a custom window or array of windows containing layers or other effects.

即将進入全屏模式前調用。視窗代理可實作這個方法,通過傳回一個或一組包含層或其它效果的視窗,來自定義動畫。

window:startCustomAnimationToEnterFullScreenWithDuration:

The system has started its animation into full-screen mode, including transitioning into a new space. You can implement this method to perform custom animation with the given duration to be in sync with the system animation.

系統已經開始進入全屏模式和過渡到新空間的動畫。可實作這個方法,使用給定的持續時間來執行自定義動畫,與系統動畫同步。

windowDidFailToEnterFullScreen:

Invoked if the window failed to enter full-screen mode.

視窗進入全屏模式失敗時調用。

customWindowsToExitFullScreenForWindow:

Invoked when the window is about to exit full-screen mode. The window delegate can implement this method to customize the animation when the window is about to exit full-screen by returning a custom window or array of windows containing layers or other effects.

即将退出全屏模式前調用。視窗代理可實作這個方法,通過傳回一個或一組包含層或其它效果的視窗,來自定義動畫。

window:startCustomAnimationToExitFullScreenWithDuration:

The system has started its animation out of full-screen, including transitioning back to the desktop space. You can implement this method to perform custom animation with the given duration to be in sync with the system animation.

系統已經開始退出全屏模式和傳回到桌面空間的動畫。可實作這個方法,使用給定的持續時間來執行自定義動畫,與系統動畫同步。

windowDidFailToExitFullScreen:

Invoked if the window failed to exit full-screen mode.

視窗退出全屏模式失敗時調用。

For more information about full-screen mode, see NSWindowDelegate Protocol Reference and the macOS Human Interface Guidelines.

關于全屏模式的更多資訊,參考NSWindowDelegate Protocol Reference和macOS Human Interface Guidelines。

原文摘自蘋果官網,譯文為原創,請勿轉載。 水準有限,譯文有錯誤、疏漏之處,敬請指正。 知識有價,感謝您支援原創,微信掃碼贊助:

Mac應用程式設計指南之四——實作全屏體驗

郵箱:[email protected] 微信公衆号CocoaLover,掃碼關注:

Mac應用程式設計指南之四——實作全屏體驗

繼續閱讀