3D touch也出了很長時間了,這次花時間好好研究了一下,把經(jīng)驗與大家分享一下
1. 主界面重按APP圖標(biāo),彈出Touch菜單
1.1靜態(tài)快速選項
(iOS數(shù)組)給APP指定靜態(tài)主屏幕的快速選項,這個鍵包含了一個字典數(shù)組,每個字典包含關(guān)于一個快速選項的詳細(xì)信息。你可以指定靜態(tài)快速選項給你的APP用一個字典數(shù)組。
UIApplicationShortcutItems (iOS數(shù)組)給APP指定靜態(tài)主屏幕的快速選項,這個鍵包含了一個字典數(shù)組,每個字典包含關(guān)于一個快速選項的詳細(xì)信息。你可以指定靜態(tài)快速選項給你的APP用一個字典數(shù)組。
靜態(tài)定義快速在運行時常用的key:
UIApplicationShortcutItemType (必須使用) 用來區(qū)分與其他快速選項的分類
UIApplicationShortcutItemTitle (必須使用) 快速選項顯示的標(biāo)題
UIApplicationShortcutItemSubtitle 快速選項顯示的子標(biāo)題
UIApplicationShortcutItemIconType 圖片類型由系統(tǒng)提供( iOS9.1之后新添加了許多圖片類型)
UIApplicationShortcutItemIconFile 自定義的圖標(biāo)
UIApplicationShortcutItemUserInfo 附加信息
2.動態(tài)快速選項
1 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
2 UIApplicationShortcutItem * item = [[UIApplicationShortcutItem alloc]initWithType:@"two" localizedTitle:@"搜索" localizedSubtitle:@"一步到達(dá)指定地點" icon:[UIApplicationShortcutIcon iconWithType:UIApplicationShortcutIconTypeSearch] userInfo:nil];
3 UIApplicationShortcutItem * item1 = [[UIApplicationShortcutItem alloc]initWithType:@"three" localizedTitle:@"附近" localizedSubtitle:@"好吃的" icon:[UIApplicationShortcutIcon iconWithType:UIApplicationShortcutIconTypeMarkLocation] userInfo:nil];
4 [UIApplication sharedApplication].shortcutItems = @[item,item1];
3.選擇item后觸發(fā)的方法
1 - (void)application:(UIApplication *)application performActionForShortcutItem:(nonnull UIApplicationShortcutItem *)shortcutItem completionHandler:(nonnull void (^)(BOOL))completionHandler{
//通過shortcutItem.type來判斷點擊的是哪一個item,來進(jìn)行不同的操作
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內(nèi)部peek和pop的使用(以tableView中的使用為例)
首先遵守協(xié)議UIViewControllerPreviewingDelegate
檢測是否有3Dtouch;
1 - (void)check3DTouch{
2 if (self.traitCollection.forceTouchCapability == UIForceTouchCapabilityAvailable)
3 {
4 NSLog(@"3D Touch 開啟");
5
6 }
7 else{
8
9 }
10 }
下面來實現(xiàn)相應(yīng)的代理方法
//peek的代理方法,輕按即可觸發(fā)彈出vc
1 - (UIViewController *)previewingContext:(id <UIViewControllerPreviewing>)previewingContext viewControllerForLocation:(CGPoint)location{
2 NSIndexPath *indexPath = [_tableView indexPathForCell:(UITableViewCell* )[previewingContext sourceView]];
3 //通過[previewingContext sourceView]拿到對應(yīng)的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的代理方法,在此處可對將要進(jìn)入的vc進(jìn)行處理,比如隱藏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進(jìn)行注冊代理方法如下
[self registerForPreviewingWithDelegate:self sourceView:cell];
5.預(yù)覽時底部菜單的添加
在要預(yù)覽的VC中添加以下代碼:
1 -(NSArray<id<UIPreviewActionItem>> *)previewActionItems
2 {
3 UIPreviewAction * action1 = [UIPreviewAction actionWithTitle:@"標(biāo)題1" style:1 handler:^(UIPreviewAction * _Nonnull action, UIViewController * _Nonnull previewViewController) {
4 NSLog(@"標(biāo)題1");
5 }];
6
7 UIPreviewAction * action2 = [UIPreviewAction actionWithTitle:@"標(biāo)題2" style:0 handler:^(UIPreviewAction * _Nonnull action, UIViewController * _Nonnull previewViewController) {
8 NSLog(@"標(biāo)題2");
9
10 }];
11 UIPreviewAction * action3 = [UIPreviewAction actionWithTitle:@"標(biāo)題3" style:2 handler:^(UIPreviewAction * _Nonnull action, UIViewController * _Nonnull previewViewController) {
12 NSLog(@"標(biāo)題3");
13 }];
14
15 NSArray * actions = @[action1,action2,action3];
16
17 return actions;
18 }
block里面直接寫點擊后要實現(xiàn)的操作
最終效果:
暫時就寫這么多,有什么不對的地方請大家指教,大家互相學(xué)習(xí)。