3D-touch目前有兩種主要的使用方式
- Home Screen Quick Actions 應(yīng)用圖標(biāo)的快捷按鈕
- Peek and Pop 預(yù)覽,窺探
Home Screen Quick Actions
應(yīng)用快速操作分為靜態(tài)和動態(tài)兩種
- 靜態(tài)
Static quick actions are available to the user immediately upon app installation. Define Home screen static quick actions in your app’s Info.plist file in the UIApplicationShortcutItems array.
靜態(tài)方式通過info.plist的方式來實現(xiàn)
- 動態(tài)
Dynamic quick actions are available to the user after first launch. Define Home screen dynamic quick actions with the UIApplicationShortcutItem, UIMutableApplicationShortcutItem, and UIApplicationShortcutIcon classes. Add dynamic quick actions to your app’s shared UIApplication object using the shortcutItems property.
動態(tài)方式通過創(chuàng)建UIApplicationShortcutItem, UIMutableApplicationShortcutItem和UIApplicationShortcutIcon對象,并在應(yīng)用第一次啟動的時候添加到UIApplication的shortcutItems數(shù)組里面來實現(xiàn)
iOS 9 displays up to four Home screen quick actions for your app. Within this limit, the system shows your static quick actions first, starting at the topmost position in the menu. If your static items do not exhaust the limit and you have also defined dynamic quick actions, then one or more of your dynamic quick actions is displayed.
iOS 9 默認(rèn)的先顯示靜態(tài)的快速操作,如果靜態(tài)的快捷方式?jīng)]有超過4個,再去顯示動態(tài)的快速操作,一共可以顯示4個快捷方式.
UIApplicationShortcutItem簡介
An application shortcut item, also called a Home screen dynamic quick action, specifies a user-initiated action for your app.
這個類就是一個快速操作類,創(chuàng)建一個這個類就相當(dāng)也創(chuàng)建了一個快速操作
主要屬性如下:
- localizedTitle
The required, user-visible title for the Home screen dynamic quick action.
必須的 快速操作的標(biāo)題
- localizedSubtitle
The optional, user-visible subtitle for the Home screen dynamic quick action.
可選的 快速操作的副標(biāo)題
- type
A required, app-specific string that you employ to identify the type of quick action to perform.
必須的 快速操作的類型 用來標(biāo)識執(zhí)行的快速操作類型
- icon
The optional icon for the Home screen dynamic quick action.
可選的 快速操作的圖標(biāo) 會被渲染成同一種顏色
- userinfo
Optional, app-specific information that you can provide for use when your app performs the Home screen quick action.
可選的 用戶信息 可以在用戶執(zhí)行快速操作的時候傳遞給用戶
UIMutableApplicationShortcutItem簡介
A mutable application shortcut item, also called, verbosely, a mutable Home screen dynamic quick action, specifies a configurable user-initiated action for your app. This class is a convenience subclass of UIApplicationShortcutItem, helping you work with registered, and therefore immutable, quick actions.
這個類繼承自UIApplicationShortcutItem,區(qū)別在于這個類的屬性是可以進行修改了,不是readonly
UIApplicationShortcutIcon簡介
An application shortcut, or quick action, icon is an image you can optionally associate with a Home screen quick action to improve its appearance and usability.
圖標(biāo):每一個快捷操作都配有一個圖標(biāo)
There are three types of quick action icon:
An icon from a system-provided library of common types, as described in the UIApplicationShortcutIconType enumeration
An icon derived from a custom template image in your app’s bundle and preferably in an asset catalog (see Template Images in UIKit User Interface Catalog and Asset Catalog Help)
An icon representing a contact in the user's address book, which you access through the ContactsUI framework (see ContactsUI)
圖標(biāo)分為三種:
- 系統(tǒng)提供的圖標(biāo)
- 用戶自定義的圖標(biāo)
- 聯(lián)系人圖標(biāo)
code
- 靜態(tài)的快速操作實現(xiàn)方式
在info.plist里面添加如下代碼:
<key>UIApplicationShortcutItems</key>
<array>
<dict>
<key>UIApplicationShortcutItemIconFile</key>
<string>open-favorites</string>
<key>UIApplicationShortcutItemTitle</key>
<string>Favorites</string>
<key>UIApplicationShortcutItemType</key>
<string>com.mycompany.myapp.openfavorites</string>
<key>UIApplicationShortcutItemUserInfo</key>
<dict>
<key>key1</key>
<string>value1</string>
</dict>
</dict>
<dict>
<key>UIApplicationShortcutItemIconType</key>
<string>UIApplicationShortcutIconTypeCompose</string>
<key>UIApplicationShortcutItemTitle</key>
<string>New Message</string>
<key>UIApplicationShortcutItemType</key>
<string>com.mycompany.myapp.newmessage</string>
<key>UIApplicationShortcutItemUserInfo</key>
<dict>
<key>key2</key>
<string>value2</string>
</dict>
</dict>
</array>
效果如圖:
- 動態(tài)的快速操作實現(xiàn)方式
在AppDelegate的如下方法中
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions;
導(dǎo)入以下代碼
UIApplicationShortcutIcon *icon1 = [UIApplicationShortcutIcon iconWithType:UIApplicationShortcutIconTypeCompose];
UIApplicationShortcutIcon *icon2 = [UIApplicationShortcutIcon iconWithType:UIApplicationShortcutIconTypeShare];
UIApplicationShortcutIcon *icon3 = [UIApplicationShortcutIcon iconWithType:UIApplicationShortcutIconTypeAdd];
UIMutableApplicationShortcutItem *item1 = [[UIMutableApplicationShortcutItem alloc] initWithType:@"com.test.dynamic" localizedTitle:@"item1" localizedSubtitle:@"item1sub" icon:icon1 userInfo:nil];
UIMutableApplicationShortcutItem *item2 = [[UIMutableApplicationShortcutItem alloc] initWithType:@"com.test.dynamic" localizedTitle:@"item2" localizedSubtitle:@"item2sub" icon:icon2 userInfo:nil];
UIMutableApplicationShortcutItem *item3 = [[UIMutableApplicationShortcutItem alloc] initWithType:@"com.test.dynamic" localizedTitle:@"item3" localizedSubtitle:@"item3sub" icon:icon3 userInfo:nil];
NSArray *items = @[item1, item2, item3];
//只需添加一次就好了
if ([UIApplication sharedApplication].shortcutItems.count == 0) {
[UIApplication sharedApplication].shortcutItems = items;
}
效果如下:
- 點擊之后我們怎么去執(zhí)行相應(yīng)的操作
點擊一個快速操作后就會調(diào)用如下方法,在這里就可以拿到UIApplicationShortcutItem對象,根據(jù)不同的對象可以做出不同的操作
- (void)application:(UIApplication *)application performActionForShortcutItem:(UIApplicationShortcutItem *)shortcutItem completionHandler:(void (^)(BOOL))completionHandler {
NSLog(@"%@",shortcutItem.localizedTitle);
}
Called when the user selects a Home screen quick action for your app, except when you’ve intercepted the interaction in a launch method.