iOS 在6s/6s plus中加入了的3D Touch技術,該技術的功能主要有3個:
1、Home Screen Quick Actions :主屏幕上按icon圖標出現快捷選項,然後選擇對應選項則會快速進入設定好的頁面中
2、提供peek(預覽)和pop(詳閱、敲擊)功能 :啓動app之後,可以設置某些頁面支持預覽和快速回復功能,例如微信點擊和某個人的對話就能實現預覽
3、iOS9 之後給UITouch對象加入了 壓力大小監測的屬性force
先講第一個功能:
效果圖:
有兩種髮式一種是靜態方式、一種是動態方式
靜態方式就是在plist文件中配置好,當你的app安裝之後就可以使用這個功能
動態方式就是用代碼創建,所有必需啓動app一次之後才能使用這個功能
由於靜態方式方便就直講靜態方式:
在plist文件中配置一些快捷項屬性
<array>
<dict>
<key>UIApplicationShortcutItemTitle</key>
<string>智能開門</string>
<key>UIApplicationShortcutItemUserInfo</key>
<dict>
<key>key1</key>
<string>value1</string>
</dict>
<key>UIApplicationShortcutItemType</key>
<string>openDoor</string>
<key>UIApplicationShortcutItemIconFile</key>
<string>智能開門@2x.png</string>
</dict>
<dict>
<key>UIApplicationShortcutItemTitle</key>
<string>我要報修</string>
<key>UIApplicationShortcutItemUserInfo</key>
<dict>
<key>key2</key>
<string>value2</string>
</dict>
<key>UIApplicationShortcutItemType</key>
<string>repair</string>
<key>UIApplicationShortcutItemIconFile</key>
<string>在線報修@2x.png</string>
</dict>
<dict>
<key>UIApplicationShortcutItemTitle</key>
<string>訪客邀請</string>
<key>UIApplicationShortcutItemUserInfo</key>
<dict>
<key>key3</key>
<string>value3</string>
</dict>
<key>UIApplicationShortcutItemType</key>
<string>invite</string>
<key>UIApplicationShortcutItemIconFile</key>
<string>訪客邀請@2x.png</string>
</dict>
<dict>
<key>UIApplicationShortcutItemTitle</key>
<string>社區服務</string>
<key>UIApplicationShortcutItemUserInfo</key>
<dict>
<key>key4</key>
<string>value4</string>
</dict>
<key>UIApplicationShortcutItemType</key>
<string>communityService</string>
<key>UIApplicationShortcutItemIconFile</key>
<string>社區服務@2x.png</string>
</dict>
</array>
UIApplicationShortcutItemType :當點擊Icon圖標的時候,彈出的選項中每一行設置一個唯一的ID 然後在app裏面通過該id判斷來確定選擇了哪一行(必需有)。
UIApplicationShortcutItemTitle 點擊之後彈出的選項中的主標題(必需有)
UIApplicationShortcutItemSubtitle 選項中的副標題。
UIApplicationShortcutItemIconType 利用系統庫提供的圖標 (UIApplicationShortcutIcon 裏面的)。
UIApplicationShortcutItemIconFile 自定義圖標,該圖標是正方形的、單色的、像素點爲35x35,如果這裏設置了自定義的圖標,設置的UIApplicationShortcutItemIconType屬性顯示系統圖標將不會顯示。
UIApplicationShortcutItemUserInfo :是一個字典 用來保存app的版本信息
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
UIApplicationShortcutItem *shortItem = [launchOptions objectForKey:UIApplicationLaunchOptionsShortcutItemKey];
if (shortItem != nil) {
// _options是個字典主要是用來傳遞快捷項的type 然後在頁面啓動完成了之後,我是在
-(void)viewDidAppear:(BOOL)animated方法裏面進行頁面跳轉的。
_options = launchOptions;
return NO;
}
else
{
//非快捷項啓動
_options = nil;
return YES;
}
}
注意:didFinishLaunchingWithOptions 這個方法要返回NO 避免重複邏輯操作。
第二中情況:app處於後臺掛起狀態,在代理方法
-(void)application:(UIApplication *)application performActionForShortcutItem:(UIApplicationShortcutItem *)shortcutItem completionHandler:(void (^)(BOOL))completionHandler
在這個方法裏面發送通知就可以進行頁面跳轉。
注意:在iOS 9中,一個應用最多能展示四個主屏快速選項。在這個限制下,系統首先展示靜態快速選項,以plist菜單中的第一個爲首。如果你的靜態選項不夠四個,並且你也定義了動態快速選項,那麼會展示一個或多個動態快速選項。
兩種快速選項都可以顯示最多兩行文本和一個可選的圖標。系統會格式化文本,排列包裝它,並且適當的添加省略號。