3Dtouch 的實際應用詳解(tableView中的應用)數組
3D touch也出了很長時間了,此次花時間好好研究了一下,把經驗與你們分享一下app
1. 主界面重按APP圖標,彈出Touch菜單ide
1.1靜態快速選項學習
(iOS數組)給APP指定靜態主屏幕的快速選項,這個鍵包含了一個字典數組,每一個字典包含關於一個快速選項的詳細信息。你能夠指定靜態快速選項給你的APP用一個字典數組。spa
UIApplicationShortcutItems (iOS數組)給APP指定靜態主屏幕的快速選項,這個鍵包含了一個字典數組,每一個字典包含關於一個快速選項的詳細信息。你能夠指定靜態快速選項給你的APP用一個字典數組。3d
靜態定義快速在運行時經常使用的key:代理
UIApplicationShortcutItemType (必須使用) 用來區分與其餘快速選項的分類code
UIApplicationShortcutItemTitle (必須使用) 快速選項顯示的標題orm
UIApplicationShortcutItemSubtitle 快速選項顯示的子標題blog
UIApplicationShortcutItemIconType 圖片類型由系統提供( iOS9.1以後新添加了許多圖片類型)
UIApplicationShortcutItemIconFile 自定義的圖標
UIApplicationShortcutItemUserInfo 附加信息
2.動態快速選項
在AppDelegate中實現如下代碼
1 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { 2 UIApplicationShortcutItem * item = [[UIApplicationShortcutItem alloc]initWithType:@"two" localizedTitle:@"搜索樓盤" localizedSubtitle:@"一步到達指定樓盤" icon:[UIApplicationShortcutIcon iconWithType:UIApplicationShortcutIconTypeSearch] userInfo:nil]; 3 UIApplicationShortcutItem * item1 = [[UIApplicationShortcutItem alloc]initWithType:@"three" localizedTitle:@"附近樓盤" localizedSubtitle:@"快來領路費吧" icon:[UIApplicationShortcutIcon iconWithType:UIApplicationShortcutIconTypeMarkLocation] userInfo:nil]; 4
5 [UIApplication sharedApplication].shortcutItems = @[item,item1];
3.選擇item後觸發的方法
1 - (void)application:(UIApplication *)application performActionForShortcutItem:(nonnull UIApplicationShortcutItem *)shortcutItem completionHandler:(nonnull void (^)(BOOL))completionHandler{ //經過shortcutItem.type來判斷點擊的是哪個item,來進行不一樣的操做
2 if ([shortcutItem.type isEqualToString:@"one"]) { 3 UITabBarController *mytab = (UITabBarController*)self.window.rootViewController; 4 mytab.selectedIndex = 0; 5 }else if ([shortcutItem.type isEqualToString:@"two"]){ 6 SearchVC *searchVC = [[SearchVC alloc]init]; 7 UITabBarController *mytab = (UITabBarController*)self.window.rootViewController; 8 UINavigationController *myNAV = [mytab.viewControllers firstObject]; 9 [myNAV pushViewController:searchVC animated:YES]; 10 // [self.window.rootViewController presentViewController:searchVC animated:YES completion:nil];
11 }else{ 12 FPHNearbyVC *vc = [[FPHNearbyVC alloc] init]; 13 UITabBarController *mytab = (UITabBarController*)self.window.rootViewController; 14 UINavigationController *myNAV = [mytab.viewControllers firstObject]; 15 vc.hidesBottomBarWhenPushed = YES; 16 [myNAV pushViewController:vc animated:YES]; 17 } 18 completionHandler(YES); 19 }
4.APP內部peek和pop的使用(以tableView中的使用爲例)
首先遵照協議UIViewControllerPreviewingDelegate
檢測是否有3Dtouch;
1 - (void)check3DTouch{ 2 if (self.traitCollection.forceTouchCapability == UIForceTouchCapabilityAvailable) 3 { 4 NSLog(@"3D Touch 開啓"); 5
6 } 7 else{ 8
9 } 10 }
下面來實現相應的代理方法
//peek的代理方法,輕按便可觸發彈出vc
1 - (UIViewController *)previewingContext:(id <UIViewControllerPreviewing>)previewingContext viewControllerForLocation:(CGPoint)location{ 2 NSIndexPath *indexPath = [_tableView indexPathForCell:(UITableViewCell* )[previewingContext sourceView]]; 3 //經過[previewingContext sourceView]拿到對應的cell;
4 NewVC *vc = [[FPHNewHouseDetailVC alloc] init]; 5 newModel *model= [_tableView objectAtIndex:indexPath.row]; 6 vc.pid = house.id; 7
8 NSLog(@"%@",location); 9 return vc; 10 }
//pop的代理方法,在此處可對將要進入的vc進行處理,好比隱藏tabBar; 11 - (void)previewingContext:(id <UIViewControllerPreviewing>)previewingContext commitViewController:(UIViewController *)viewControllerToCommit 12 { 13 viewControllerToCommit.hidesBottomBarWhenPushed = YES; 14 [self showViewController:viewControllerToCommit sender:self]; 15 }
注意:tableView在
- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
方法中必定要對每一個cell進行註冊代理方法以下
[self registerForPreviewingWithDelegate:self sourceView:cell];
在要預覽的VC中添加如下代碼:
1 -(NSArray<id<UIPreviewActionItem>> *)previewActionItems 2 { 3 UIPreviewAction * action1 = [UIPreviewAction actionWithTitle:@"標題1" style:1 handler:^(UIPreviewAction * _Nonnull action, UIViewController * _Nonnull previewViewController) { 4 NSLog(@"標題1"); 5 }]; 6 7 UIPreviewAction * action2 = [UIPreviewAction actionWithTitle:@"標題2" style:0 handler:^(UIPreviewAction * _Nonnull action, UIViewController * _Nonnull previewViewController) { 8 NSLog(@"標題2"); 9 10 }]; 11 UIPreviewAction * action3 = [UIPreviewAction actionWithTitle:@"標題3" style:2 handler:^(UIPreviewAction * _Nonnull action, UIViewController * _Nonnull previewViewController) { 12 NSLog(@"標題3"); 13 }]; 14 15 NSArray * actions = @[action1,action2,action3]; 16 17 return actions; 18 }
block裏面直接寫點擊後要實現的操做
最終效果:
暫時就寫這麼多,有什麼不對的地方請你們指教,你們互相學習。