3D Touch 有兩種方式,一種是在按壓icon圖標出現快捷標簽跳轉到相應界面,這里的快捷標簽最多只能有四個;另一種是在app內實現按壓預覽,預覽圖片或文字等。
下面先說第一種3D Touch 方式!這種方式很簡單,只需在info.plist文件里添加相應參數就可以了,如下圖:
在info.plist文件的UIApplicationShortcutItems下添加參數:
//這兩個為必有項
- UIApplicationShortcutItemTitle:標簽的名字,自己隨便取咯!
- UIApplicationShortcutItemType:標簽標識,在application中用到,也是自己隨便取的哦!
//以下為可選項 - UIApplicationShortcutItemIconType:標簽前圖標的樣式,此為系統圖片,也可用下面的自定義圖片!
- UIApplicationShortcutItemSubtitle:標簽副標題!
- UIApplicationShortcutItemIconFile:自定義圖片的路徑!
- UIApplicationShortcutItemUserInfo:設置用戶信息,是一個字典類型,可以用來傳值!
info.plist文件里添加參數
添加完成后就可以運行程序啦!效果如下:
(http://upload-images.jianshu.io/upload_images/2455662-bba6ed5447ddaa30.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)運行效果圖
當然也可以用代碼實現,在application:didFinishLaunchingWithOptions:中添加代碼
// 創建標簽的ICON圖標。 UIApplicationShortcutIcon *icon = [UIApplicationShortcutIcon iconWithType:UIApplicationShortcutIconTypeSearch]; // 創建一個標簽,并配置相關屬性。 UIApplicationShortcutItem *item = [[UIApplicationShortcutItem alloc] initWithType:@"com.playmoster.app" localizedTitle:@"play master" localizedSubtitle:nil icon:icon userInfo:nil]; // 將標簽添加進Application的shortcutItems中,最多可添加四個哦! [UIApplication sharedApplication].shortcutItems = @[item];
那么點擊標簽如何進入指定的界面呢,iOS9提供了一個新方法,在ApplicationDelegate中的
- (void)application:(UIApplication *)application performActionForShortcutItem:(nonnull UIApplicationShortcutItem *)shortcutItem completionHandler:(nonnull void (^)(BOOL))completionHandler
方法
if ([shortcutItem.type isEqualToString:@"com.playmoster.app"]) { //跳轉到你想要跳轉的頁面,先跳轉到首頁,再在首頁中處理你的跳轉邏輯 ViewController *ctrl = [[ViewController alloc] init]; ctrl.shortcutItemType = 0; UINavigationController *navCtrl = [[UINavigationController alloc]initWithRootViewController:ctrl]; self.window.rootViewController = navCtrl;}
這樣就ok了,你的程序就可以實現3D Touch在icon上面的功能了!
下面我們說一下3D Touch的預覽功能!
在開始前我們需要在ViewController中添加一個UITableView的首頁,如圖,這個大家自己隨便寫一個咯!
用來實現3D Touch的UITableView
接下來我們要實現3D Touch的預覽功能啦!
第一步、我們要注冊3D Touch:這里是在- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
中注冊的。
//代理設為當前界面,sourceView為按壓的視圖,只要是UIView類型的都可以注冊!
[self registerForPreviewingWithDelegate:self sourceView:cell];
} else {
NSLog(@"no support 3D Touch!");
}```
第二步、實現兩個代理方法,當壓力超過一定值后,會直接跳轉到預覽界面
```- (UIViewController *)previewingContext:(id)previewingContext viewControllerForLocation:(CGPoint)location {
// 初始化預覽界面
SelectViewController *ctrl = [[SelectViewController alloc] init];
//設定預覽界面的大小
ctrl.preferredContentSize = CGSizeMake(0, kScreenHeight);
//設置未出現預覽界面是周邊模糊范圍,rect為3D touch范圍,范圍以外為模糊區域
CGRect rect = CGRectMake(0, 0, kScreenWidth, 44);
previewingContext.sourceRect = rect;
return ctrl;
}```
```- (void)previewingContext:(id)previewingContext commitViewController:(UIViewController *)viewControllerToCommit {
//跳轉到預覽界面
[self showViewController:viewControllerToCommit sender:self];
}```
預覽效果如圖

想要實現預覽圖片是上滑出現sheet的功能,只需實現下面幾步,以下代碼在預覽界面實現
```- (NSArray<id<>UIPreviewActionItem>> *)previewActionItems {
// setup a list of preview actions
UIPreviewAction *action1 = [UIPreviewAction actionWithTitle:@"one" style:UIPreviewActionStyleDefault handler:^(UIPreviewAction * _Nonnull action, UIViewController * _Nonnull previewViewController) {
//添加點擊方法
NSLog(@"click one");
}];
UIPreviewAction *action2 = [UIPreviewAction actionWithTitle:@"two" style:UIPreviewActionStyleDefault handler:^(UIPreviewAction * _Nonnull action, UIViewController * _Nonnull previewViewController) {
//添加點擊方法
NSLog(@"click two");
}];
NSArray *actions = @[action1, action2];
return actions;
}```
若要在點擊方法里面實現頁面跳轉,則需要添加如下方法,注意:appDelegate是在ApplicationDelegate.h 文件中添加類方法
```+ (AppDelegate *)appDelegate;```
在.m文件中實現:
```+ (AppDelegate *)appDelegate {
return (AppDelegate *)[[UIApplication sharedApplication] delegate];
}```
然后才能在預覽界面中實現下面的方法
將跳轉是用到的self都替換成[self topViewController]即可。
```- (UIViewController*)topViewController {
return [self topViewControllerWithRootViewController:[AppDelegate appDelegate].window.rootViewController];
}
-(UIViewController*)topViewControllerWithRootViewController:(UIViewController*)previewViewController {
if ([previewViewController isKindOfClass:[UITabBarController class]]) {
UITabBarController *tabBarController = (UITabBarController *)previewViewController;
return [self topViewControllerWithRootViewController:tabBarController.selectedViewController];
} else if ([previewViewController isKindOfClass:[UINavigationController class]]) {
UINavigationController* navigationController = (UINavigationController*)previewViewController;
return [self topViewControllerWithRootViewController:navigationController.visibleViewController];
} else if (previewViewController.presentedViewController) {
UIViewController* presentedViewController = previewViewController.presentedViewController;
return [self topViewControllerWithRootViewController:presentedViewController];
} else {
return previewViewController;
}
}```
效果如下圖:

以上就是我所學習的一點3D Touch了,大家看看提點意見哈!!!