前言
3D Touch 已经是几年前的东西了,但是项目一直没有涉及过相关的功能.所以今年查了一些相关的资料写个 demo 学习一下并做个总结方便以后使用.
设置图标
给应用图标添加 3D Touch 的菜单一共有2种方式: 静态添加 和 动态添加.
静态添加
直接在 info.plist 文件中设置如下字段:
菜单字段 UIApplicationShortcutItems 数组, 可以设置多个 item.
每个 item 可以包含以下字段:
| Key | Required | Description | 
|---|---|---|
| UIApplicationShortcutItemType | YES | 可以理解为标识符 | 
| UIApplicationShortcutItemTitle | YES | 标题 | 
| UIApplicationShortcutItemSubtitle | NO | 副标题 | 
| UIApplicationShortcutItemIconType | NO | 如果使用系统图标的话使用这个 | 
| UIApplicationShortcutItemIconFile | NO | 自定义图标图片名称 | 
| UIApplicationShortcutItemUserInfo | NO | One use for this dictionary is to provide app version information | 
info.plist 中的配置:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
  <key>UIApplicationShortcutItems</key>
	<array>
		<dict>
            // 系统图标类型
			<key>UIApplicationShortcutItemIconType</key>          
			<string>UIApplicationShortcutIconTypeShare</string>    
            // 标题 key
			<key>UIApplicationShortcutItemTitle</key>             
			<string>分享</string>                                  
            // 标识符 (用于判断哪个 item 被点击)
			<key>UIApplicationShortcutItemType</key>
			<string>share</string>                                 
		</dict>
		<dict>
			<key>UIApplicationShortcutItemIconType</key>
			<string>UIApplicationShortcutIconTypeAdd</string>
			<key>UIApplicationShortcutItemTitle</key>
			<string>添加</string>
			<key>UIApplicationShortcutItemType</key>
			<string>add</string>
		</dict>
	</array>
动态添加
动态设置也比较简单:
- 通过 
UIApplicationShortcutIcon创建图标. - 通过 
UIApplicationShortcutItem来创建菜单item对象. - 把 
item数组赋值给[UIApplication sharedApplication].shortcutItems 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
/**
 添加 items
 */
- (void)addShortcutItems {
    // share
    UIApplicationShortcutIcon *shareIcon = [UIApplicationShortcutIcon iconWithType:UIApplicationShortcutIconTypeShare];
    UIApplicationShortcutItem *shareItem = [[UIApplicationShortcutItem alloc] initWithType:@"Share" localizedTitle:@"Share" localizedSubtitle:nil icon:shareIcon userInfo:nil];
    
    // add
    UIApplicationShortcutIcon *addIcon = [UIApplicationShortcutIcon iconWithType:UIApplicationShortcutIconTypeAdd];
    UIApplicationShortcutItem *addItem = [[UIApplicationShortcutItem alloc] initWithType:@"Add" localizedTitle:@"Add" localizedSubtitle:nil icon:addIcon userInfo:nil];
    
    [UIApplication sharedApplication].shortcutItems = @[shareItem, addItem];
}
然后在 application: didFinishLaunchingWithOptions: 中调用 addShortcutItems 方法.
1
2
3
4
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    [self addShortcutItems];
    return YES;
}
系统提供的图标种类:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
typedef NS_ENUM(NSInteger, UIApplicationShortcutIconType) {
    UIApplicationShortcutIconTypeCompose,
    UIApplicationShortcutIconTypePlay,
    UIApplicationShortcutIconTypePause,
    UIApplicationShortcutIconTypeAdd,
    UIApplicationShortcutIconTypeLocation,
    UIApplicationShortcutIconTypeSearch,
    UIApplicationShortcutIconTypeShare,
    UIApplicationShortcutIconTypeProhibit       NS_ENUM_AVAILABLE_IOS(9_1),
    UIApplicationShortcutIconTypeContact        NS_ENUM_AVAILABLE_IOS(9_1),
    UIApplicationShortcutIconTypeHome           NS_ENUM_AVAILABLE_IOS(9_1),
    UIApplicationShortcutIconTypeMarkLocation   NS_ENUM_AVAILABLE_IOS(9_1),
    UIApplicationShortcutIconTypeFavorite       NS_ENUM_AVAILABLE_IOS(9_1),
    UIApplicationShortcutIconTypeLove           NS_ENUM_AVAILABLE_IOS(9_1),
    UIApplicationShortcutIconTypeCloud          NS_ENUM_AVAILABLE_IOS(9_1),
    UIApplicationShortcutIconTypeInvitation     NS_ENUM_AVAILABLE_IOS(9_1),
    UIApplicationShortcutIconTypeConfirmation   NS_ENUM_AVAILABLE_IOS(9_1),
    UIApplicationShortcutIconTypeMail           NS_ENUM_AVAILABLE_IOS(9_1),
    UIApplicationShortcutIconTypeMessage        NS_ENUM_AVAILABLE_IOS(9_1),
    UIApplicationShortcutIconTypeDate           NS_ENUM_AVAILABLE_IOS(9_1),
    UIApplicationShortcutIconTypeTime           NS_ENUM_AVAILABLE_IOS(9_1),
    UIApplicationShortcutIconTypeCapturePhoto   NS_ENUM_AVAILABLE_IOS(9_1),
    UIApplicationShortcutIconTypeCaptureVideo   NS_ENUM_AVAILABLE_IOS(9_1),
    UIApplicationShortcutIconTypeTask           NS_ENUM_AVAILABLE_IOS(9_1),
    UIApplicationShortcutIconTypeTaskCompleted  NS_ENUM_AVAILABLE_IOS(9_1),
    UIApplicationShortcutIconTypeAlarm          NS_ENUM_AVAILABLE_IOS(9_1),
    UIApplicationShortcutIconTypeBookmark       NS_ENUM_AVAILABLE_IOS(9_1),
    UIApplicationShortcutIconTypeShuffle        NS_ENUM_AVAILABLE_IOS(9_1),
    UIApplicationShortcutIconTypeAudio          NS_ENUM_AVAILABLE_IOS(9_1),
    UIApplicationShortcutIconTypeUpdate         NS_ENUM_AVAILABLE_IOS(9_1)
} NS_ENUM_AVAILABLE_IOS(9_0) __TVOS_PROHIBITED;

事件响应
判断 item 被点击只需要判断给 item 设置的 item.type 并做相应操作即可.
1
2
3
4
5
6
- (void)application:(UIApplication *)application performActionForShortcutItem:(nonnull UIApplicationShortcutItem *)shortcutItem completionHandler:(nonnull void (^)(BOOL))completionHandler {
    // 根据 shortcutItem.type 判断点击的是哪个 item
    if ([shortcutItem.type isEqualToString:@"Add"]) {
        ...
    }
}
自定义 ICON
1
2
// 自定义 icon
[UIApplicationShortcutIcon iconWithTemplateImageName:@"custom"];
在文档中指出:
Icons should be square, single color, and 35x35 points
自定义的 icon 必须是 35 * 35 的 正方形 单色 的图片
虽然我尝试了一下用 200 * 200 的图依旧能够正常显示.但是最好还是按照文档讲的标准来吧~
补充
- 静态添加 和 动态添加 可以同时使用, 但是系统会先加载 静态 
items, 然后再加载 动态items. - 开发者自定义的貌似最多只能添加 
4个item, 加上系统会自带一个分享应用一共5个.(虽然没有看到文档里面有写个数限制) 

上面中文的 2 个为 静态添加 , 下面 2 个为 动态添加. Custom 为自定义图标.
手头没有修图工具, 因此图标略显大.内容边框留足透明像素区域应该会美观一点, 跟系统的保持一致.