OC学习26:APP图标3D Touch菜单探索

张建 lol

前言

3D Touch是指给 APP 图标添加菜单,一共有两种方式:静态添加动态添加

静态添加

  1. 直接在 info.plist 文件中设置如下字段:
  1. 菜单字段 UIApplicationShortcutItems 数组,可以设置多个 item,每个item可以包含以下字段:
  • UIApplicationShortcutItemType :可以理解为标识符
  • UIApplicationShortcutItemTitle :标题
  • UIApplicationShortcutItemSubtitle :副标题
  • UIApplicationShortcutItemIconType :如果使用系统图标的话使用这个
  • UIApplicationShortcutItemIconFile :自定义图标图片名称
  • UIApplicationShortcutItemUserInfo :One use for this dictionary is to provide app version information
  1. 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>

注:plist文件中不能有注释,需要去掉

动态添加

  1. 动态设置也比较简单,如下:
  1. 通过 UIApplicationShortcutIcon 创建图标。
  2. 通过 UIApplicationShortcutItem 来创建菜单 item 对象。
  3. 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];
}
  1. 然后在 application: didFinishLaunchingWithOptions: 中调用 addShortcutItems 方法:
1
2
3
4
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[self addShortcutItems];
return YES;
}
  1. 系统提供的图标种类
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;

种类对应的图标:

事件响应

  1. 判断 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"]) {
...
}
}
  1. 自定义 icon
1
2
// 自定义 icon
[UIApplicationShortcutIcon iconWithTemplateImageName:@"custom"];

在文档中指出: Icons should be square, single color, and 35x35 points

补充

  • 静态添加动态添加 可以同时使用,但是系统会先加载 静态items,然后再加载 动态items
  • 开发自定义的最多只能添加 4item,加上系统会自带一个 分享应用 一共 5 个。
  • Post title:OC学习26:APP图标3D Touch菜单探索
  • Post author:张建
  • Create time:2020-09-04 11:36:06
  • Post link:https://redefine.ohevan.com/2020/09/04/OC/OC学习26:APP图标3D-Touch菜单探索/
  • Copyright Notice:All articles in this blog are licensed under BY-NC-SA unless stating additionally.
On this page
OC学习26:APP图标3D Touch菜单探索