南山忆的技术博客

回忆是抓不住的月光,握紧就变黑暗

0%

3DTouch应用详解

3D touch也出了很长时间了,这次花时间好好研究了一下,把经验与大家分享一下

主界面重按APP图标,弹出Touch菜单

 静态快速选项
   (iOS数组)给APP指定静态主屏幕的快速选项,这个键包含了一个字典数组,每个字典包含关于一个快速选项的详细信息。你可以指定静态快速选项给你的APP用一个字典数组。

 UIApplicationShortcutItems (iOS数组)给APP指定静态主屏幕的快速选项,这个键包含了一个字典数组,每个字典包含关于一个快速选项的详细信息。你可以指定静态快速选项给你的APP用一个字典数组。

静态定义快速在运行时常用的key:
UIApplicationShortcutItemType (必须使用) 用来区分与其他快速选项的分类
UIApplicationShortcutItemTitle (必须使用) 快速选项显示的标题
UIApplicationShortcutItemSubtitle 快速选项显示的子标题
UIApplicationShortcutItemIconType 图片类型由系统提供( iOS9.1之后新添加了许多图片类型)
UIApplicationShortcutItemIconFile 自定义的图标
UIApplicationShortcutItemUserInfo 附加信息

动态快速选项

1
2
3
4
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
UIApplicationShortcutItem * item = [[UIApplicationShortcutItem alloc]initWithType:@"two" localizedTitle:@"搜索" localizedSubtitle:@"一步到达指定地点" icon:[UIApplicationShortcutIcon iconWithType:UIApplicationShortcutIconTypeSearch] userInfo:nil];
UIApplicationShortcutItem * item1 = [[UIApplicationShortcutItem alloc]initWithType:@"three" localizedTitle:@"附近" localizedSubtitle:@"好吃的" icon:[UIApplicationShortcutIcon iconWithType:UIApplicationShortcutIconTypeMarkLocation] userInfo:nil];
[UIApplication sharedApplication].shortcutItems = @[item,item1];

选择item后触发的方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
 - (void)application:(UIApplication *)application performActionForShortcutItem:(nonnull     UIApplicationShortcutItem *)shortcutItem completionHandler:(nonnull void   (^)(BOOL))completionHandler{
  //通过shortcutItem.type来判断点击的是哪一个item,来进行不同的操作
if ([shortcutItem.type isEqualToString:@"one"]) {
UITabBarController *mytab = (UITabBarController*)self.window.rootViewController;
mytab.selectedIndex = 0;
}else if ([shortcutItem.type isEqualToString:@"two"]){
SearchVC *searchVC = [[SearchVC alloc]init];
UITabBarController *mytab = (UITabBarController*)self.window.rootViewController;
UINavigationController *myNAV = [mytab.viewControllers firstObject];
[myNAV pushViewController:searchVC animated:YES];
// [self.window.rootViewController presentViewController:searchVC animated:YES completion:nil];
}else{
FPHNearbyVC *vc = [[FPHNearbyVC alloc] init];
UITabBarController *mytab = (UITabBarController*)self.window.rootViewController;
UINavigationController *myNAV = [mytab.viewControllers firstObject];
vc.hidesBottomBarWhenPushed = YES;
[myNAV pushViewController:vc animated:YES];
}
completionHandler(YES);
}

APP内部peek和pop的使用(以tableView中的使用为例)

  首先遵守协议UIViewControllerPreviewingDelegate
  检测是否有3Dtouch;

1
2
3
4
5
6
7
8
9
10
- (void)check3DTouch{
if (self.traitCollection.forceTouchCapability == UIForceTouchCapabilityAvailable)
{
NSLog(@"3D Touch 开启");

}
else{

}
}

  下面来实现相应的代理方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
//peek的代理方法,轻按即可触发弹出vc
- (UIViewController *)previewingContext:(id <UIViewControllerPreviewing>)previewingContext viewControllerForLocation:(CGPoint)location{
NSIndexPath *indexPath = [_tableView indexPathForCell:(UITableViewCell* )[previewingContext sourceView]];
//通过[previewingContext sourceView]拿到对应的cell;  
NewVC *vc = [[FPHNewHouseDetailVC alloc] init];
newModel *model= [_tableView objectAtIndex:indexPath.row];
vc.pid = house.id;

NSLog(@"%@",location);
return vc;
}
//pop的代理方法,在此处可对将要进入的vc进行处理,比如隐藏tabBar;
- (void)previewingContext:(id <UIViewControllerPreviewing>)previewingContext commitViewController:(UIViewController *)viewControllerToCommit
{
viewControllerToCommit.hidesBottomBarWhenPushed = YES;
[self showViewController:viewControllerToCommit sender:self];
}

注意:tableView在
    - (UITableViewCell)tableView:(UITableView )tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  方法中一定要对每个cell进行注册代理方法如下
  [self registerForPreviewingWithDelegate:self sourceView:cell];

预览时底部菜单的添加

在要预览的VC中添加以下代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
-(NSArray<id<UIPreviewActionItem>> *)previewActionItems
{
UIPreviewAction * action1 = [UIPreviewAction actionWithTitle:@"标题1" style:1 handler:^(UIPreviewAction * _Nonnull action, UIViewController * _Nonnull previewViewController) {
NSLog(@"标题1");
}];

UIPreviewAction * action2 = [UIPreviewAction actionWithTitle:@"标题2" style:0 handler:^(UIPreviewAction * _Nonnull action, UIViewController * _Nonnull previewViewController) {
NSLog(@"标题2");

}];
UIPreviewAction * action3 = [UIPreviewAction actionWithTitle:@"标题3" style:2 handler:^(UIPreviewAction * _Nonnull action, UIViewController * _Nonnull previewViewController) {
NSLog(@"标题3");
}];

NSArray * actions = @[action1,action2,action3];

return actions;
}

    block里面直接写点击后要实现的操作
最终效果:

  暂时就写这么多,有什么不对的地方请大家指教,大家互相学习。