代码改变世界

【iOS】3D Touch

2016-04-10 18:36  l4y  阅读(233)  评论(0编辑  收藏  举报

文章内容来源于Apple的开发者文档:https://developer.apple.com/library/content/documentation/UserExperience/Conceptual/Adopting3DTouchOniPhone/,这个文档中还提供了几个示例,建议直接阅读该文档。

------------------------------------------------------------------------------

iOS 9中以后,引入了3D Touch功能。主要包括两个方面,一是Home Screen Quick Actions, 二是Peek and Pop。

Home Screen Quick Actions

实现这个主要有两种方式,

  1. 在Info.plist中的UIApplicationShortcutIconTypeAdd下定义静态的quick actions;
  2. 动态的定义,使用UIApplicationShortItem及相关的API定义quick action,添加到UIApplication对象的shortcutItems属性。

这里主要说一下第一种方式中各property的含义,先来看一个示例:

 1 <key>UIApplicationShortcutItems</key>
 2     <array>
 3         <dict>
 4             <key>UIApplicationShortcutItemIconFile</key>
 5             <string>open-favorites</string>
 6             <key>UIApplicationShortcutItemTitle</key>
 7             <string>Favorites</string>
 8             <key>UIApplicationShortcutItemType</key>
 9             <string>com.mycompany.myapp.openfavorites</string>
10             <key>UIApplicationShortcutItemUserInfo</key>
11             <dict>
12                 <key>key1</key>
13                 <string>value1</string>
14             </dict>
15         </dict>
16         <dict>
17             <key>UIApplicationShortcutItemIconType</key>
18             <string>UIApplicationShortcutIconTypeCompose</string>
19             <key>UIApplicationShortcutItemTitle</key>
20             <string>New Message</string>
21             <key>UIApplicationShortcutItemType</key>
22             <string>com.mycompany.myapp.newmessage</string>
23             <key>UIApplicationShortcutItemUserInfo</key>
24             <dict>
25                 <key>key2</key>
26                 <string>value2</string>
27             </dict>
28         </dict>
29     </array>

各个key的解释:

 

Peek and Pop

可以在view controller中响应用户不同力度的按压。交互处理过程(还是按Apple的原文吧,翻译的表达不太好,原文也很简单):

  1. Indication that content preview is available.
  2. Display of the preview ---- known as a peek ---- with options to act on it directly ---- known as peek quick actions
  3. Optional navigation to the view shown in the preview ---- known as a pop