IOS 3D Touch功能
IOS 3D touch功能
- IOS9之后苹果增加了3d touch支持,也就是用力按压主屏幕按钮来调出快速操作的菜单
添加方式
-
静态添加
- 配置info.plist文件添加
UIApplicationShortcutItems
数组
<key>UIApplicationShortcutItems</key> <array> <dict> <key>UIApplicationShortcutItemIconFile</key> <string>open-favorites</string> <key>UIApplicationShortcutItemIconType</key> <string>open-favorites</string> <key>UIApplicationShortcutItemTitle</key> <string>Title</string> <key>UIApplicationShortcutItemType</key> <string>com.mycompany.myapp.openfavorites</string> <key>UIApplicationShortcutItemSubtitle</key> <string>Subtitle</string> <key>UIApplicationShortcutItemUserInfo</key> <dict> <key>key1</key> <string>value1</string> </dict> </dict> </array>
其中
UIApplicationShortcutItemTitle
和UIApplicationShortcutItemType
是必填的,对应的是此快捷方式的标题和唯一标识符其他几个参数可选
UIApplicationShortcutItemSubtitle
是副标题UIApplicationShortcutItemIconFile
是配置自己工程里面的图标UIApplicationShortcutItemIconType
是配置系统自带的图标UIApplicationShortcutItemUserInfo
是配置额外的可能需要的字典参数 - 配置info.plist文件添加
-
动态添加
在
didFinishLaunchingWithOptions
代码中添加func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool { let dynamicIcon1 = UIApplicationShortcutIcon(systemImageName: "play") let dynamicItem1 = UIApplicationShortcutItem(type: "play", localizedTitle: "播放", localizedSubtitle: "播放喜欢的内容", icon: dynamicIcon1, userInfo: nil) let dynamicIcon2 = UIApplicationShortcutIcon(systemImageName: "message") let dynamicItem2 = UIApplicationShortcutItem(type: "message", localizedTitle: "消息", localizedSubtitle: "发送消息", icon: dynamicIcon2, userInfo: nil) UIApplication.shared.shortcutItems = [dynamicItem1, dynamicItem2] }
具体展现为
添加的顺序是先静态后动态,且最多添加4个,多出的不会显示
逻辑处理
- 在AppDelegate中的
performActionFor
方法中处理逻辑
func application(_ application: UIApplication, performActionFor shortcutItem: UIApplicationShortcutItem, completionHandler: @escaping (Bool) -> Void) {}
shortcutItem: UIApplicationShortcutItem
就带有所有的信息