3D-touch目前有兩種主要的使用方式
- Home Screen Quick Actions 應用圖標的快捷按鈕
- Peek and Pop 預覽,窺探
Home Screen Quick Actions
應用快速操作分為靜態和動態兩種
- 靜態
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.
靜態方式通過info.plist的方式來實現
- 動態
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.
動態方式通過創建UIApplicationShortcutItem, UIMutableApplicationShortcutItem和UIApplicationShortcutIcon對象,并在應用第一次啟動的時候添加到UIApplication的shortcutItems數組里面來實現
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 默認的先顯示靜態的快速操作,如果靜態的快捷方式沒有超過4個,再去顯示動態的快速操作,一共可以顯示4個快捷方式.
UIApplicationShortcutItem簡介
An application shortcut item, also called a Home screen dynamic quick action, specifies a user-initiated action for your app.
這個類就是一個快速操作類,創建一個這個類就相當也創建了一個快速操作
主要屬性如下:
- localizedTitle
The required, user-visible title for the Home screen dynamic quick action.
必須的 快速操作的標題
- localizedSubtitle
The optional, user-visible subtitle for the Home screen dynamic quick action.
可選的 快速操作的副標題
- type
A required, app-specific string that you employ to identify the type of quick action to perform.
必須的 快速操作的類型 用來標識執行的快速操作類型
- icon
The optional icon for the Home screen dynamic quick action.
可選的 快速操作的圖標 會被渲染成同一種顏色
- userinfo
Optional, app-specific information that you can provide for use when your app performs the Home screen quick action.
可選的 用戶信息 可以在用戶執行快速操作的時候傳遞給用戶
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,區別在于這個類的屬性是可以進行修改了,不是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.
圖標:每一個快捷操作都配有一個圖標
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)
圖標分為三種:
- 系統提供的圖標
- 用戶自定義的圖標
- 聯系人圖標
code
- 靜態的快速操作實現方式
在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>
效果如圖:
- 動態的快速操作實現方式
在AppDelegate的如下方法中
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions;
導入以下代碼
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;
}
效果如下:
- 點擊之后我們怎么去執行相應的操作
點擊一個快速操作后就會調用如下方法,在這里就可以拿到UIApplicationShortcutItem對象,根據不同的對象可以做出不同的操作
- (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.