View Programming Guide for iOS ---- iOS 視圖編程指南(三)---Windows

Windows

Every iOS application needs at least one window—an instance of the UIWindow class—and some may include more than one window. A window object has several responsibilities:html

每一個iOS應用程序須要至少一個窗口---UIWindow類的一個實例---有些可能有不止一個窗口。窗口對象的任務是:ios

  • It contains your application’s visible content.windows

    它包含了應用程序的可見內容。app

  • It plays a key role in the delivery of touch events to your views and other application objects.less

    它在向視圖和其它應用程序對象傳遞觸摸事件時扮演關鍵角色。ide

  • It works with your application’s view controllers to facilitate orientation changes.動畫

    它和應用程序視圖控制器一塊兒促進方向變化。ui

In iOS, windows do not have title bars, close boxes, or any other visual adornments. A window is always just a blank container for one or more views. Also, applications do not change their content by showing new windows. When you want to change the displayed content, you change the frontmost views of your window instead.this

在iOS,窗口沒有標題欄,關閉按鈕,或任何其它可見的裝飾。 窗口就只是一個或多個視圖的空白容器。 並且,應用程序不會經過顯示新窗口來改變它們的內容。 當你想改變顯示的內容時, 你能夠改變窗口上最前面的視圖。spa

Most iOS applications create and use only one window during their lifetime. This window spans the entire main screen of the device and is loaded from the application’s main nib file (or created programmatically) early in the life of the application. However, if an application supports the use of an external display for video out, it can create an additional window to display content on that external display. All other windows are typically created by the system, and are usually created in response to specific events, such as an incoming phone call.

大多數iOS應用程序在它們的生命期都只建立並使用一個窗口。 該窗口貫穿設備的整個主要屏幕, 它在應用程序生命週期早期從應用程序的主要nib文件載入(或程序自動建立)。然而,若是應用程序支持須要外接顯示器來顯示視頻,它能夠建立一個額外的窗口來顯示外接顯示器的內容。一般全部的其它窗口都由系統建立,它們經常是用來響應指定事件,好比一個來電。

Tasks That Involve Windows

1、窗口涉及的任務

For many applications, the only time the application interacts with its window is when it creates the window at startup. However, you can use your application’s window object to perform a few application-related tasks:

對於不少應用程序,應用程序跟窗口惟一交互,是它啓動時建立窗口的時候。然而,你可使用應用程序窗口對象來執行一些跟應用相關的任務:

  • Use the window object to convert points and rectangles to or from the window’s local coordinate system. For example, if you are provided with a value in window coordinates, you might want to convert it to the coordinate system of a specific view before trying to use it. For information on how to convert coordinates, see 「Converting Coordinates in the View Hierarchy.」

    使用窗口對象來轉換點和矩形,把它們從窗口的內部座標系統轉換過來,或從別處轉換到內部座標系統去。好比,若是你有一個座標窗口值,你可能想在使用它以前把它轉換爲一個指定視圖的座標系統。關於如何轉換座標的信息,請看 「Converting Coordinates in the View Hierarchy.」

  • Use window notifications to track window-related changes. Windows generate notifications when they are shown or hidden or when they accept or resign the key status. You can use these notifications to perform actions in other parts of your application. For more information, see 「Monitoring Window Changes.」

    使用窗口通知來追蹤窗口相關的各類改變。窗口在顯示或隱藏,或當它們接受或放棄(resign)關鍵狀態時,它們會生成各類通知。 你能夠用這些通知在應用程序別的部分執行各類操做。 更多信息請看 「Monitoring Window Changes.」

Creating and Configuring a Window

2、 建立並配置一個窗口

You can create and configure your application’s main window programmatically or using Interface Builder. In either case, you create the window at launch time and should retain it and store a reference to it in your application delegate object. If your application creates additional windows, have the application create them lazily when they are needed. For example, if your application supports displaying content on an external display, it should wait until a display is connected before creating the corresponding window.

你能夠用程序建立並配置應用程序的主要窗口,或使用界面建立器(Interface Builder)。任何建立方式,窗口都是在啓動時建立,並一直保留它,還須要在應用程序委託對象裏存儲一個指向該窗口的引用鏈接。若是應用程序須要建立額外的窗口,讓應用程序在須要時惰性的建立它們。 好比,若是應用程序支持顯示外接顯示器的內容,它應用等待直到顯示在建立相關窗口以前被鏈接完成。

You should always create your application’s main window at launch time regardless of whether your application is being launched into the foreground or background. Creating and configuring a window is not an expensive operation by itself. However, if your application is launched straight into the background, you should avoid making the window visible until your application enters the foreground.

你應用老是在啓動時建立應用程序主窗口,無論應用程序是被啓動到前臺或是後臺。 建立並配置一個窗口自己並非高損耗操做。然而,若是應用程序被直接啓動到後臺,你應該讓窗口一直隱藏直到應用程序進入前臺。

Creating Windows in Interface Builder

一、用界面生成器建立窗口

Creating your application’s main window using Interface Builder is simple because the Xcode project templates do it for you. Every new Xcode application project includes a main nib file (usually with the name MainWindow.xib or some variant thereof) that includes the application’s main window. In addition, these templates also define an outlet for that window in the application delegate object. You use this outlet to access the window object in your code.

用界面生成器建立應用程序的窗口很簡單,由於Xcode項目模板已經爲你完成。 每一個新Xcode應用程序工程都包含一個主nib文件(一般的名稱是MainWindow.xib 或一些變種),它包含了應用程序的主窗口。 此外,這些模板還給窗口在應用程序代理對象內定義了一個接口(outlet)。 你能夠在代碼裏使用該接口來訪問窗口對象。

Important: When creating your window in Interface Builder, it is recommended that you enable the Full Screen at Launch option in the attributes inspector. If this option is not enabled and your window is smaller than the screen of the target device, touch events will not be received by some of your views. This is because windows (like all views) do not receive touch events outside of their bounds rectangle. Because views are not clipped to the window’s bounds by default, the views still appear visible but events do not reach them. Enabling the Full Screen at Launch option ensures that the window is sized appropriately for the current screen.

 重要:當用界面生成器建立窗口時, 推薦你啓用」全屏啓動「選項,該選項在屬性選擇器(attributes inspector)裏。 若是該選項不被啓用,你的窗口將比設備屏幕小,一些視圖將接收不到不少觸摸事件。那是由於窗口(跟全部視圖同樣)不接收在它們的邊界矩形外發生的觸摸事件。由於默認狀況下,視圖沒有被裁減到窗口的邊界裏,視圖任然是可見的,可是事件沒有到達那裏。啓用」全屏啓動」選項確保窗口尺寸對於當前屏幕正合適。

If you are retrofitting a project to use Interface Builder, creating a window using Interface Builder is a simple matter of dragging a window object to your nib file. Of course, you should also do the following:

若是你正使用界面生成器更新(retrofitting)一個項目, 用界面生成器建立一個窗口很簡單,你只須要拖動一個窗口對象到nib文件便可。 固然,你還應該作如下事情:

  • To access the window at runtime, you should connect the window to an outlet, typically one defined in your application delegate or the File’s Owner of the nib file.

    要想在運行時訪問窗口,你應該把窗口鏈接到一個接口,一般是一個在應用程序委託或nib文件的File's Owner裏定義的接口。

  • If your retrofit plans include making your new nib file the main nib file of your application, you must also set theNSMainNibFile key in your application’s Info.plist file to the name of your nib file. Changing the value of this key ensures that the nib file is loaded and available for use by the time theapplication:didFinishLaunchingWithOptions: method of your application delegate is called.

    若是你的更新計劃包含製做新的nib文件來代替應用程序的主nib文件, 你必要同時在應用程序的Info.plist文件的NSMainNibFile關鍵字那裏,把關鍵字設置爲你的新nib文件名。 改變該關鍵字確保該nib文件被加載,並讓應用程序調用application:didFinishLaunchingWithOptions: 方法時使用它。

For more information about creating and configuring nib files, see Interface Builder User Guide. For information about how to load nib files into your application at runtime, see 「Nib Files」 in Resource Programming Guide.

更多有關建立和配置nib文件的各類信息,請看 Interface Builder User Guide. 更多有關在運行時應用程序如何加載nib文件的詳細信息,請看Resource Programming Guide  「Nib Files」 .

Creating a Window Programmatically

二、用程序建立一個窗口

If you prefer to create your application’s main window programmatically, you should include code similar to the following in the application:didFinishLaunchingWithOptions: method of your application delegate:

若是你更喜歡用程序建立你的應用程序主窗口,你應該在應用程序委託的application:didFinishLaunchingWithOptions: 方法裏包含如下類似代碼:

self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];

In the preceding example, self.window is assumed to be a declared property of your application delegate that is configured to retain the window object. If you were creating a window for an external display instead, you would assign it to a different variable and you would need to specify the bounds of the non main UIScreen object representing that display.

例子中,假設 self.window是應用程序委託中一個已被聲明的屬性(property),應用程序委託是用來保留(retain)窗口對象的。若是你想要爲外部顯示器建立一個窗口,你應該把它分配(assign)給一個不一樣的變量, 而且你應該須要指定非主要UIScreen 對象的邊界用來表明那個外部顯示器。

When creating windows, you should always set the size of the window to the full bounds of the screen. You should not reduce the size of the window to accommodate the status bar or any other items. The status bar always floats on top of the window anyway, so the only thing you should shrink to accommodate the status bar is the view you put into your window. And if you are using view controllers, the view controller should handle the sizing of your views automatically.

當建立窗口時,你應該老是設置窗口尺寸爲滿屏。 你不該該爲容納狀態欄或任何其它項而縮減窗口尺寸。 無論怎樣狀態欄老是漂浮在窗口的上邊, 因此你只須要把容納狀態欄的視圖放入窗口。 並且若是你有使用視圖控制器,視圖控制器應該能自動處理視圖的尺寸。

Adding Content to Your Window

三、向窗口添加內容

Each window typically has a single root view object (managed by a corresponding view controller) that contains all of the other views representing your content. Using a single root view simplifies the process of changing your interface; to display new content, all you have to do is replace the root view. To install a view in your window, use theaddSubview: method. For example, to install a view that is managed by a view controller, you would use code similar to the following:

每一個窗口一般有一個單一的根視圖對象(由相關的視圖控制器管理),根視圖控制器包含表現內容的全部其它視圖。使用一個根視圖簡化了改變界面的過程;要想顯示新內容,你只須要替換根視圖便可。要想在窗口裏加入一個視圖,使用addSubview: 方法。好比, 加入一個由一個視圖控制器管理的視圖,你使用如下類似代碼:

[window addSubview:viewController.view];

In place of the preceding code, you can alternatively configure the rootViewController property of the window in your nib file. This property offers a convenient way to configure the root view of the window using a nib file instead of programmatically. If this property is set when the window is loaded from its nib file, UIKit automatically installs the view from the associated view controller as the root view of the window. This property is used only to install the root view and is not used by the window to communicate with the view controller.

以上代碼,你也能夠在你的nib文件裏設置窗口的 rootViewController 屬性。該屬性提供了一個配置窗口根視圖的便捷方法--用nib文件代替程序。 若是當窗口從它的nib文件被載入時,該屬性已經被設置,UIKit自動從相關的視圖控制器(做爲窗口根視圖)加載視圖。該屬性只在加載根視圖時使用,它不用來讓窗口跟視圖控制器通訊。

You can use any view you want for a window’s root view. Depending on your interface design, the root view can be a generic UIView object that acts as a container for one or more subviews, the root view can be a standard system view, or the root view can be a custom view that you define. Some standard system views that are commonly used as root views include scroll views, table views, and image views.

 你可使用任何你想要的視圖做爲窗口的根視圖。 根據你的界面設計,根視圖能夠是一個普通的UIView 對象,它是一個或多個子視圖的的容器, 也能夠是一個標準系統視圖,還能夠是一個自定義視圖。 有一些標準系統視圖經常被用來做爲根視圖,包括滾動視圖,表格視圖,和圖像視圖等。

When configuring the root view of the window, you are responsible for setting its initial size and position within the window. For applications that do not include a status bar, or that display a translucent status bar, set the view size to match the size of the window. For applications that show an opaque status bar, position your view below the status bar and reduce its size accordingly. Subtracting the status bar height from the height of your view prevents the top portion of your view from being obscured.

當配置窗口的根視圖時,你負責在窗口裏給它設置初始化尺寸和位置。 那些不包含狀態欄或顯示一個半透明狀態欄的應用程序,視圖尺寸跟窗口尺寸相匹配。 對於那些顯示一個不透明狀態欄,把視圖放在狀態欄的下面,相應的所見它的尺寸。從視圖高度減去狀態欄的高度,避免視圖頂部被狀態欄遮擋。 

Note: If the root view of your window is provided by a container view controller (such as a tab bar controller, navigation controller, or split-view controller), you do not need to set the initial size of the view yourself. The container view controller automatically sizes its view appropriately based on whether the status bar is visible.

 注意:若是窗口的根視圖是由一個容器視圖控制器提供(好比,標籤欄控制器,導航控制器,或拆分視圖控制器), 你不須要設置視圖的初始化尺寸。 容器視圖控制器會根據狀態欄是否可見自動調整合適的尺寸。

Changing the Window Level

四、 改變窗口層次

Each UIWindow object has a configurable windowLevel property that determines how that window is positioned relative to other windows. For the most part, you should not need to change the level of your application’s windows. New windows are automatically assigned to the normal window level at creation time. The normal window level indicates that the window presents application-related content. Higher window levels are reserved for information that needs to float above the application content, such as the system status bar or alert messages. And although you can assign windows to these levels yourself, the system usually does this for you when you use specific interfaces. For example, when you show or hide the status bar or display an alert view, the system automatically creates the needed windows to display those items.

每一個UIWindow 對象有一個可設置的windowLevel 屬性,該屬性決定了窗口相對於別的窗口如何定位。 大部分狀況,你應該不須要改變應用程序窗口的層次。 新窗口在建立時自動分配到一個正常的窗口層。 正常窗口層代表窗口表現應用程序相關的內容。 高窗口層次保留給須要漂浮在應用程序內容上面的信息,好比系統狀態欄或各類警報信息。儘管你能夠指派這些層次給窗口,可是當你使用指定接口的時候,這些是由系統爲你完成。

Monitoring Window Changes

3、監控窗口變化

If you want to track the appearance or disappearance of windows inside your application, you can do so using these window-related notifications:

若是你想要監控應用程序中窗口的出現和隱藏, 你可使用這些窗口相關的通知:

These notifications are delivered in response to programmatic changes in your application’s windows. Thus, when your application shows or hides a window, the UIWindowDidBecomeVisibleNotification andUIWindowDidBecomeHiddenNotification notifications are delivered accordingly. These notifications are not delivered when your application moves into the background execution state. Even though your window is not displayed on the screen while your application is in the background, it is still considered visible within the context of your application.

當應用程序的窗口發生程序性改變時,這些通知將被傳遞。所以,當你的應用程序顯示或隱藏一個窗口,UIWindowDidBecomeVisibleNotification 和 UIWindowDidBecomeHiddenNotification 通知也被相應的傳遞。這些通知在應用程序轉入後臺執行狀態時將不被傳遞。 儘管當應用程序在後臺時,窗口沒有在屏幕上顯示,可是它在應用程序的上下文中任然被認爲是可見的。

The UIWindowDidBecomeKeyNotification and UIWindowDidResignKeyNotification notifications help your application keep track of which window is the key window—that is, which window is currently receiving keyboard events and other non touch-related events. Whereas touch events are delivered to the window in which the touch occurred, events that do not have an associated coordinate value are delivered to the key window of your application. Only one window at a time may be key.

UIWindowDidBecomeKeyNotification 和 UIWindowDidResignKeyNotification 通知幫助應用程序持續監控哪一個窗口是關鍵窗口---就是,當前哪一個窗口能接收鍵盤事件以及其它非觸摸相關事件。 不管觸摸事件被傳遞到事件發生窗口的哪裏,那些沒有一個相應座標值的事件都被傳遞到應用程序的關鍵窗口。同一時間只能有一個關鍵窗口。

Displaying Content on an External Display

4、在外部顯示器上顯示內容

To display content on an external display, you must create an additional window for your application and associate it with the screen object representing the external display. New windows are normally associated with the main screen by default. Changing the window’s associated screen object causes the contents of that window to be rerouted to the corresponding display. Once the window is associated with the correct screen, you can add views to it and show it just like you do for your application’s main screen.

要想在一個外部顯示器上顯示內容,你必須給應用程序建立一個額外的窗口,並跟表明外部顯示器的屏幕對象想關聯。新窗口正常狀況下都默認跟主屏幕相關聯。 改變窗口的相關聯屏幕對象會致使該窗口的內容被從新路由到相應的顯示器上。 一旦窗口跟正確的屏幕相關聯,你就能夠給它添加視圖,並像應用程序的主屏幕那樣顯示它。

The UIScreen class maintains a list of screen objects representing the available hardware displays. Normally, there is only one screen object representing the main display for any iOS-based device, but devices that support connecting to an external display can have an additional screen object available. Devices that support an external display include iPhone and iPod touch devices that have Retina displays and the iPad. Older devices, such as iPhone 3GS, do not support external displays.

UIScreen 類維護了一個屏幕對象列表,它們表明各類可用的硬件顯示器。正常狀況下,任何基於iOS的設備只有一個屏幕對象表明主顯示器,可是那些支持鏈接到一個外部顯示器的設備能有一個額外的屏幕對象。 支持一個外部顯示器的設備包括iPhone 和 有Retina顯示屏的iPod touch設備 以及iPad. 老設備,好比iPhone 3GS ,不支持外部顯示器。

Note: Because external displays are essentially a video-out connection, you should not expect touch events for views and controls in a window that is associated with an external display. In addition, it is your application’s responsibility to update the contents of the window as needed. Thus, to mirror the contents of your main window, your application would need to create a duplicate set of views for the external display’s window and update them in tandem with the views in your main window.

 注意: 由於外部顯示器基本上是一個視頻鏈接,你不該該期待窗口內的視圖和控制器的觸摸事件和能跟一個外部顯示器相關聯。另外,根據須要更新窗口的內容是應用程序的責任。 所以,要想鏡像主窗口裏的內容,應用程序應該須要給外部顯示器窗口建立一套視圖的複製並跟主窗口裏的視圖一塊兒更新它們。

The process for displaying content on an external display is described in the following sections. However, the following steps summarize the basic process:

在外部顯示器上顯示內容的過程在如下段裏描述。然而,如下步驟只概述了基本過程:

  1. At application startup, register for the screen connection and disconnection notifications.

     在應用程序啓動時,註冊屏幕鏈接和斷開鏈接通知。

  2. When it is time to display content on the external display, create and configure a window.

    當要在外部顯示器裏顯示內容時,建立並配置一個窗口。 

    • Use the screens property of UIScreen to obtain the screen object for the external display.

       用UIScreen類的screens 屬性來獲取外部顯示器的屏幕對象。

    • Create a UIWindow object and size it appropriately for the screen (or for your content).

       建立一個UIWindow 對象,併爲屏幕(或爲你的內容)定製合適的尺寸。

    • Assign the UIScreen object for the external display to the screen property of the window.

       把外部顯示器的UIScreen對象分配(assign)給窗口的screen 屬性。

    • Adjust the resolution of the screen object as needed to support your content.

       根據須要調整屏幕對象的分辨率來支持你的內容。

    • Add any appropriate views to the window.

       給窗口添加任何合適的視圖。

  3. Show the window and update it normally.

     正常的顯示和更新窗口。

Handling Screen Connection and Disconnection Notifications

一、處理屏幕的鏈接和斷開鏈接通知

Screen connection and disconnection notifications are crucial for handling changes to external displays gracefully. When the user connects or disconnects a display, the system sends appropriate notifications to your application. You should use these notifications to update your application state and create or release the window associated with the external display.

屏幕的鏈接和斷開通知對於優雅的處理外部顯示器的變化時相當重要的。 當用戶鏈接或斷開一個顯示器,系統給應用程序發送適當的通知。你應用使用這些通知來更新應用程序狀態以及建立或釋放跟外部顯示器相關的窗口。

The important thing to remember about the connection and disconnection notifications is that they can come at any time, even when your application is suspended in the background. Therefore, it is best to observe the notifications from an object that is going to exist for the duration of your application’s runtime, such as your application delegate. If your application is suspended, the notifications are queued until your application exits the suspended state and starts running in either the foreground or background.

請記住:鏈接和斷開通知能夠在任什麼時候候發送,即便是應用程序暫停在後臺。所以,最好在應用程序整個運行期間都存在的對象裏觀察通知,好比應用程序的委託。 若是應用程序被暫停, 通知排隊等待直到應用程序退出暫停狀態並開始在前臺或後臺運行。

Listing 2-1 shows the code used to register for connection and disconnection notifications. This method is called by the application delegate at initialization time but you could register for these notifications from other places in your application, too. The implementation of the handler methods is shown in Listing 2-2.

列表 2-1 顯示了註冊鏈接和斷開通知的代碼。該方法被應用程序在初始化時調用,可是你也能夠在應用程序的別的地方註冊這些通知。 處理方法的實現顯示在列表2-2中。

Listing 2-1  Registering for screen connect and disconnect notifications

列表 2-1 爲屏幕註冊鏈接和斷開通知

- (void)setupScreenConnectionNotificationHandlers
{
    NSNotificationCenter* center = [NSNotificationCenter defaultCenter];
 
    [center addObserver:self selector:@selector(handleScreenConnectNotification:)
            name:UIScreenDidConnectNotification object:nil];
    [center addObserver:self selector:@selector(handleScreenDisconnectNotification:)
            name:UIScreenDidDisconnectNotification object:nil];
}
 

If your application is active when an external display is attached to the device, it should create a second window for that display and fill it with some content. The content does not need to be the final content you want to present. For example, if your application is not ready to use the extra screen, it can use the second window to display some placeholder content. If you do not create a window for the screen, or if you create a window but do not show it, a black field is displayed on the external display.

若是當一個外部顯示器被鏈接到設備時,應用程序是活動的, 那麼它應該建立第二個窗口來顯示並裝入一些內容。這些內容並不須要是你想要呈現的最終內容。好比,若是應用程序沒準備好使用額外的屏幕,它可使用該第二個窗口來顯示一些佔位內容。若是你沒有爲屏幕建立一個窗口,或若是你建立了一個窗口但沒顯示,在外部顯示器上就會顯示一個空白區域。

Listing 2-2 shows how to create a secondary window and fill it with some content. In this example, the application creates the window in the handler methods it uses to receive screen connection notifications. (For information about registering for connection and disconnection notifications, see Listing 2-1.) The handler method for the connection notification creates a secondary window, associates it with the newly connected screen and calls a method of the application’s main view controller to add some content to the window and show it. The handler method for the disconnection notification releases the window and notifies the main view controller so that it can adjust its presentation accordingly.

列表2-2 顯示瞭如何建立第二個窗口並裝入一個內容。 該例子中, 應用程序在用來接收屏幕鏈接通知的處理方法內建立該窗口。(關於註冊鏈接和斷開通知的信息,請看 Listing 2-1) 鏈接通知的處理方法建立了一個附加窗口, 讓它跟新鏈接相關聯,並調用應用程序主視圖控制器的方法來添加一些內容到窗口,而後顯示它。 斷開通知的處理方法釋放了窗口並通知主視圖控制器,這樣他就能相應的調整它的外觀。

Listing 2-2  Handling connect and disconnect notifications

列表 2-2 處理鏈接和斷開通知

- (void)handleScreenConnectNotification:(NSNotification*)aNotification
{
    UIScreen*    newScreen = [aNotification object];
    CGRect        screenBounds = newScreen.bounds;
 
    if (!_secondWindow)
    {
        _secondWindow = [[UIWindow alloc] initWithFrame:screenBounds];
        _secondWindow.screen = newScreen;
 
        // Set the initial UI for the window.
        [viewController displaySelectionInSecondaryWindow:_secondWindow];
    }
}
 
- (void)handleScreenDisconnectNotification:(NSNotification*)aNotification
{
    if (_secondWindow)
    {
        // Hide and then delete the window.
        _secondWindow.hidden = YES;
        [_secondWindow release];
        _secondWindow = nil;
 
        // Update the main screen based on what is showing here.
        [viewController displaySelectionOnMainScreen];
    }
 
}

Configuring a Window for an External Display

二、爲外部顯示器配置一個窗口

To display a window on an external screen, you must associate it with the correct screen object. This process involves locating the proper UIScreen object and assigning it to the window’s screen property. You can get the list of screen objects from the screens class method of UIScreen. The array returned by this method always contains at least one object representing the main screen. If a second object is present, that object represents a connected external display.

要想在外接顯示器上顯示一個窗口, 你必須把它關聯到正確的屏幕對象。 該進程涉及到定位正確的UIScreen 對象,並把它分配給窗口的screen 屬性。你能夠用UIScreen類的screens類方法獲取屏幕對象的列表。該方法返回的數字老是至少包含一個對象,該對象表明主屏幕。 若是存在第二個對像,該對象表明一個外接顯示器的鏈接。

Listing 2-3 shows a method that is called at application startup to see if an external display is already attached. If it is, the method creates a window, associates it with the external display, and adds some placeholder content before showing the window. In this case, the placeholder content is a white background and a label indicating that there is no content to display. To show the window, this method changes the value of its hidden property rather than callingmakeKeyAndVisible. It does this because the window contains only static content and is not used to handle events.

列表2-3 顯示了一個在應用程序啓動時被調用的方法,該方法用來查看一個外接顯示器是否已被鏈接。若是是,該方法建立一個跟外接顯示器相關聯的窗口,並在窗口顯示以前添加一些佔位內容。 在這種狀況下,佔位內容是一個白背景,以及一個代表沒有任何可顯示內容的標籤。 要想顯示該窗口,該方法改變它的hidden 屬性值,而不是調用 makeKeyAndVisible 方法。 它決定這麼作的緣由是窗口只包含靜態內容並不用它來處理任何事件。

Listing 2-3  Configuring a window for an external display

列表 2-3 爲一個外接顯示器配置一個窗口

- (void)checkForExistingScreenAndInitializeIfPresent
{
    if ([[UIScreen screens] count] > 1)
    {
        // Associate the window with the second screen.
        // The main screen is always at index 0.
        UIScreen*    secondScreen = [[UIScreen screens] objectAtIndex:1];
        CGRect        screenBounds = secondScreen.bounds;
 
        _secondWindow = [[UIWindow alloc] initWithFrame:screenBounds];
        _secondWindow.screen = secondScreen;
 
        // Add a white background to the window
        UIView*            whiteField = [[UIView alloc] initWithFrame:screenBounds];
        whiteField.backgroundColor = [UIColor whiteColor];
 
        [_secondWindow addSubview:whiteField];
        [whiteField release];
 
        // Center a label in the view.
        NSString*    noContentString = [NSString stringWithFormat:@"<no content>"];
        CGSize        stringSize = [noContentString sizeWithFont:[UIFont systemFontOfSize:18]];
 
        CGRect        labelSize = CGRectMake((screenBounds.size.width - stringSize.width) / 2.0,
                                    (screenBounds.size.height - stringSize.height) / 2.0,
                                    stringSize.width, stringSize.height);
 
        UILabel*    noContentLabel = [[UILabel alloc] initWithFrame:labelSize];
        noContentLabel.text = noContentString;
        noContentLabel.font = [UIFont systemFontOfSize:18];
        [whiteField addSubview:noContentLabel];
 
        // Go ahead and show the window.
        _secondWindow.hidden = NO;
    }
}

Important: You should always associate a screen with a window before showing the window. While it is possible to change screens for a window that is currently visible, doing so is an expensive operation and should be avoided.

 重要提示:你應該老是在顯示窗口以前把一個屏幕關聯到窗口。 可是當目前可見的一個窗口可能改變屏幕時,這樣作是一個高損耗的操做,應該避免。

As soon as the window for an external screen is displayed, your application can begin updating it like any other window. You can add and remove subviews as needed, change the contents of subviews, animate changes to the views, and invalidate their contents as needed.

一旦一個外界屏幕的窗口被顯示,應用程序就能夠像更新任何別的窗口同樣更新該窗口。 你能夠根據須要添加和刪除子視圖, 改變子視圖的內容,動畫視圖的變化,以及根據須要讓它們的內容無效。

Configuring the Screen Mode of an External Display

三、配置外接顯示器的屏幕模式

Depending on your content, you might want to change the screen mode before associating your window with it. Many screens support multiple resolutions, some of which use different pixel aspect ratios. Screen objects use the most common screen mode by default, but you can change that mode to one that is more suitable for your content. For example, if you are implementing a game using OpenGL ES and your textures are designed for a 640 x 480 pixel screen, you might change the screen mode for screens with higher default resolutions.

根據你的內容,你可能想在關聯屏幕模式和窗口以前改變屏幕模式。 不少屏幕支持多種分辨率,其中一些使用不一樣的像素高寬比( pixel aspect ratios). 默認時,屏幕對象使用最經常使用的屏幕模式,可是你能夠把它改爲更適合你的內容的模式。 好比,若是你正使用OpenGL ES實現一個遊戲,你的結構是爲640x480像素屏幕而設計的,你可能將屏幕的模式改成更高的默認分辨率。

If you plan to use a screen mode other than the default one, you should apply that mode to the UIScreen object before associating the screen with a window. The UIScreenMode class defines the attributes of a single screen mode. You can get a list of the modes supported by a screen from its availableModes property and iterate through the list for one that matches your needs.

若是你計劃使用一個屏幕模式,不是默認那個, 你應該在關聯屏幕和窗口前給UIScreen對象應用那個模式。UIScreenMode 類定義了一個單一屏幕模式的各類屬性。 你能夠從屏幕的availableModes屬性裏獲取屏幕支持的模式列表, 而後遍歷該列表來選擇符合你須要的模式。

For more information about screen modes, see UIScreenMode Class Reference.

關於屏幕模式的更多信息,請看UIScreenMode Class Reference.

相關文章
相關標籤/搜索