3D-touch API - Home Screen Quick Actions
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.