
隨著 iOS 系統對 3D Touch 的使用越來越頻繁,瞭解這項從 iOS 9 引入的能力就顯得頗有必要。日常開發中真正會用到的其實就兩塊:
在主屏幕上對 App 圖標使用 3D Touch(ShortcutItem);
在應用內對某個控件使用 3D Touch(Peek & Pop)。
ShortcutItem 讓用戶在主屏幕重壓 App 圖標時彈出若干快捷入口,直接跳到指定功能。

在 Info.plist 中配置 UIApplicationShortcutItems 鍵即可,無需寫代碼。

各鍵含義:
UIApplicationShortcutItemType:快捷項的唯一標識字符串(必填)
UIApplicationShortcutItemTitle:標題(必填)
UIApplicationShortcutItemSubtitle:副標題(可選)
UIApplicationShortcutItemIconType:系統圖標(可選)
UIApplicationShortcutItemIconFile:自定義圖標(可選)
UIApplicationShortcutItemUserInfo:附加信息(可選)
每一個快捷項是一個 UIApplicationShortcutItem 對象,其指定初始化器如下:
- (instancetype)initWithType:(NSString *)type
localizedTitle:(NSString *)localizedTitle
localizedSubtitle:(nullable NSString *)localizedSubtitle
icon:(nullable UIApplicationShortcutIcon *)icon
userInfo:(nullable NSDictionary *)userInfo;
參數含義與 plist 中的鍵一一對應:type 為唯一標識(必填),localizedTitle 為標題(必填),localizedSubtitle 為副標題,icon 為圖標,userInfo 為附加信息,後三者均可選。
圖標由 UIApplicationShortcutIcon 描述,既可用系統提供的樣式,也可用自定義模板圖:
// 使用系統提供的圖標 + (instancetype)iconWithType:(UIApplicationShortcutIconType)type; // 自定義圖標 + (instancetype)iconWithTemplateImageName:(NSString *)templateImageName;
系統預置圖標樣式如下:

注:自定義圖標需要使用鏤空圖標,建議 1 倍圖尺寸為 35×35。
示例:動態添加四個快捷項。
// 以「動態添加四個快捷可選項」為例
UIApplicationShortcutIcon *searchShortcutIcon = [UIApplicationShortcutIcon iconWithType:UIApplicationShortcutIconTypeSearch];
UIApplicationShortcutItem *searchShortcutItem = [[UIApplicationShortcutItem alloc] initWithType:@"com.liupeng.search"
localizedTitle:@"Search"
localizedSubtitle:@"Search Subtitle"
icon:searchShortcutIcon
userInfo:nil];
UIApplicationShortcutIcon *playShortcutIcon = [UIApplicationShortcutIcon iconWithType:UIApplicationShortcutIconTypePlay];
UIApplicationShortcutItem *playShortcutItem = [[UIApplicationShortcutItem alloc] initWithType:@"com.liupeng.play"
localizedTitle:@"Play"
localizedSubtitle:@"Play Subtitle"
icon:playShortcutIcon
userInfo:nil];
UIApplicationShortcutIcon *qrShortcutIcon = [UIApplicationShortcutIcon iconWithTemplateImageName:@"shortcut_QR"];
UIApplicationShortcutItem *qrShortcutItem = [[UIApplicationShortcutItem alloc] initWithType:@"com.liupeng.qr"
localizedTitle:@"QR"
localizedSubtitle:@"QR Subtitle"
icon:qrShortcutIcon
userInfo:nil];
UIApplicationShortcutIcon *payShortcutIcon = [UIApplicationShortcutIcon iconWithTemplateImageName:@"shortcut_pay"];
UIApplicationShortcutItem *payShortcutItem = [[UIApplicationShortcutItem alloc] initWithType:@"com.liupeng.pay"
localizedTitle:@"Pay"
localizedSubtitle:@"Pay Subtitle"
icon:payShortcutIcon
userInfo:nil];
[UIApplication sharedApplication].shortcutItems = @[searchShortcutItem, playShortcutItem, qrShortcutItem, payShortcutItem];
用戶點擊快捷項進入 App 後,系統會回調下面的方法,可依 type 區分並執行不同邏輯:
- (void)application:(UIApplication *)application
performActionForShortcutItem:(UIApplicationShortcutItem *)shortcutItem
completionHandler:(void (^)(BOOL))completionHandler {
if ([shortcutItem.type isEqualToString:@"com.liupeng.search"]) {
// do something ...
} else if ([shortcutItem.type isEqualToString:@"com.liupeng.play"]) {
// do something ...
}
// ...
}
動態添加快捷項之前,必須先判斷設備是否支持 3D Touch,否則在不支持的機型上會崩潰:
if ([self respondsToSelector:@selector(traitCollection)]) {
if ([self.traitCollection respondsToSelector:@selector(forceTouchCapability)]) {
if (self.traitCollection.forceTouchCapability == UIForceTouchCapabilityAvailable) {
// 支持 3D Touch
} else {
// 不支持 3D Touch
}
}
}
注:即使用戶設備支持 3D Touch,也可能在運行期通過「設置 → 通用 → 輔助功能 → 3D Touch」將其關閉,因此有必要重寫
-traitCollectionDidChange:隨時響應變化。
Peek 與 Pop 是應用內的全新交互模式。隨著按壓力度加大,用戶會依次經歷四個階段:
1. 輕按控件,除觸發控件外的其他區域被虛化。

2. 繼續用力,Peek 觸發,展示內容快照。

3. 向上滑動,展開快捷操作項。

4. 繼續用力,跳轉進入 Pop 界面。

以「從 ViewController 中的 UILabel 通過 Peek & Pop 進入 DetailViewController」為例。
第一步,讓需要交互的控件所在控制器遵循 UIViewControllerPreviewingDelegate 協議:
@interface ViewController () <UIViewControllerPreviewingDelegate> @end
第二步,為具體控件注冊預覽能力:
[self registerForPreviewingWithDelegate:self sourceView:label];
第三步,進入 Peek 狀態時,系統回調:
- (nullable UIViewController *)previewingContext:(id <UIViewControllerPreviewing>)previewingContext
viewControllerForLocation:(CGPoint)location {
// previewingContext.sourceView:觸發操作的視圖
// previewingContext.sourceRect:設置不被虛化的區域
DetailViewController *detailVC = [[DetailViewController alloc] init];
detailVC.preferredContentSize = CGSizeMake(0, 300); // 預覽區域大小,可不設置
return detailVC;
}
第四步,確認進入(Pop)時,系統回調:
- (void)previewingContext:(id <UIViewControllerPreviewing>)previewingContext
commitViewController:(UIViewController *)viewControllerToCommit {
[self.navigationController pushViewController:viewControllerToCommit animated:YES];
}
第五步,若希望在 Peek 時提供快捷操作項,需在 DetailViewController 中重寫 previewActionItems 的 getter:
- (NSArray<id<UIPreviewActionItem>> *)previewActionItems {
UIPreviewAction *action1 = [UIPreviewAction actionWithTitle:@"選項一"
style:UIPreviewActionStyleDefault
handler:^(UIPreviewAction *_Nonnull action,
UIViewController *_Nonnull previewViewController) {
}];
UIPreviewAction *action2 = [UIPreviewAction actionWithTitle:@"選項二"
style:UIPreviewActionStyleSelected
handler:^(UIPreviewAction *_Nonnull action,
UIViewController *_Nonnull previewViewController) {
}];
UIPreviewAction *action3 = [UIPreviewAction actionWithTitle:@"選項三"
style:UIPreviewActionStyleDestructive
handler:^(UIPreviewAction *_Nonnull action,
UIViewController *_Nonnull previewViewController) {
}];
UIPreviewActionGroup *actionGroup = [UIPreviewActionGroup actionGroupWithTitle:@"選項組"
style:UIPreviewActionStyleDefault
actions:@[action1, action2]];
return @[action1, action2, action3, actionGroup];
}
其中 style 決定按鈕語義:Default 為普通操作,Selected 用於選中態,Destructive 用於刪除等破壞性操作,並會以紅色呈現。