天天看点

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应用编程指南之四——实现全屏体验