3DTouch

3D Touch是一种立体触控技术,被苹果称为新一代多点触控技术,是在Apple Watch上采用的Force Touch,屏幕可感应不同的感压力度触控。3D Touch,苹果iPhone 6s的新功能。有PeekPop两种新手势。3D-Touch技术,相对于多点触摸在平面二维空间的操作,3D-Touch技术增加了对力度和手指面积的感知,可以通过长按快速预览/查看你想要的短信/图片/超链接等内容,Peek和Pop手势的响应时间可迅捷到10ms和15ms

app内使用3Dtouch

一. 在controller中判断3DTouch是否可用
1
2
3
4
5
if ([UIDevice currentDevice].systemVersion.floatValue > 9.0) {
    if (self.traitCollection.forceTouchCapability == UIForceTouchCapabilityAvailable) {
        self.supportForceTouch = YES;
    }
}

二. 为view(这里以cell为例)注册3Dtouch

1
2
3
if (_supportForceTouch) {
        [self registerForPreviewingWithDelegate:self sourceView:cell];
    }

三. 遵守UIViewControllerPreviewingDelegate协议, 并实现两个协议方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
/**
 根据条件返回不同的控制器, 来实现peek, 和push
 */
- (UIViewController *)previewingContext:(id<UIViewControllerPreviewing>)previewingContext viewControllerForLocation:(CGPoint)location{
    NSLog(@"%@",NSStringFromCGPoint(location));
   
   NSIndexPath *indexPath = [self.tableView indexPathForCell:(UITableViewCell *)previewingContext.sourceView];
    LabelViewController *labVC = [[LabelViewController alloc] init];
    DTableViewController *table = [[DTableViewController alloc] init];
    switch (indexPath.row) {
        case 0:
            //准备数据源, 数据源在peek和正式视图都有效
            labVC.name = @"wo shi name";
            //可以再此直接push, 不建议.最好在下面这个代理方法里面push
            //返回重压要前往的VC
            return labVC;
            break;
        case 1:
        //可以通过preferredContentSize属性设置peek窗口的大小
        //table.preferredContentSize = CGSizeMake(375, 300);
            return table;
            break;
        default:
             //return nil不会有3DTouch效果
            return nil;
            break;
    }

}
- (void)previewingContext:(id<UIViewControllerPreviewing>)previewingContext commitViewController:(UIViewController *)viewControllerToCommit{
    //在此可以在此设置数据源, 让peek和正式视图显示不同的内容
//    LabelViewController *labVC = (LabelViewController *)viewControllerToCommit;
//    labVC.name = @"no name";
    //跳转控制器
    [self.navigationController pushViewController:viewControllerToCommit animated:YES];
   
}
四. 实现peek时上拉,在底部出现菜单

承接上文, 我们peek and push 到目的控制器 LabelViewController 之后, 在LabelViewController 中实现以下方法, 即可在上级控制器重压时能上拉显示菜单

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
- (NSArray<id<UIPreviewActionItem>> *)previewActionItems{

	//通过控制UIPreviewActionStyle属性来控制action的状态(默认, 被选中, 删除)
    UIPreviewAction *action1 = [UIPreviewAction actionWithTitle:@"action1" style:UIPreviewActionStyleDefault handler:^(UIPreviewAction * _Nonnull action, UIViewController * _Nonnull previewViewController) {
        NSLog(@"action1 ==%@",previewViewController);
    }];
    
    UIPreviewAction *action2 = [UIPreviewAction actionWithTitle:@"action2" style:UIPreviewActionStyleSelected handler:^(UIPreviewAction * _Nonnull action, UIViewController * _Nonnull previewViewController) {
        NSLog(@"action1 ==%@",previewViewController);
    }];
    UIPreviewAction *action3 = [UIPreviewAction actionWithTitle:@"action3" style:UIPreviewActionStyleDestructive handler:^(UIPreviewAction * _Nonnull action, UIViewController * _Nonnull previewViewController) {
        NSLog(@"action1 ==%@",previewViewController);
    }];   
    return @[action1, action2, action3];
}

另外, action时可以成组group

1
2
3
4
    //将action组成组
    UIPreviewActionGroup *group = [UIPreviewActionGroup actionGroupWithTitle:@"group" style:UIPreviewActionStyleDefault actions:@[action2, action3]];
    
    return @[action1, group];

app外使用 - Icon 3Dtouch

创建方式有两种:
1
2
3
4
5
6
7
UIApplicationShortcutItems:数组中的元素就是我们的那些快捷选项标签。
UIApplicationShortcutItemTitle:标签标题(必填)
UIApplicationShortcutItemType:标签的唯一标识 (必填)
UIApplicationShortcutItemIconType:使用系统图标的类型,如搜索、定位、home等(可选)
UIApplicationShortcutItemIconFile:使用项目中的图片作为标签图标 (可选) <mark>图片必须是正方形、单色并且尺寸是35*35像素的图片</mark>
UIApplicationShortcutItemSubtitle:标签副标题 (可选)
UIApplicationShortcutItemUserInfo:字典信息,如传值使用 (可选)
  • ⚠️:UIApplicationShortcutItemIconFile:使用项目中的图片作为标签图标 (可选) 图片必须是正方形、单色并且尺寸是35*35像素的图片

  • ⚠️:如果使用了自定义图片,系统的UIApplicationShortcutItemIconType字段会失效
  • ⚠️:没有使用系统图片或者自定义图片,那么图片位置会出现黑色原点
  • ⚠️:自定义图片必须是单色图片
  • ⚠️:建议35*35 2x图
  • ⚠️:最多添加4个,第五个可以是系统默认带上的分享功能

  • 通过配置info.plist的方式创建

图片

  • 通过代码创建

    1
    2
    3
    4
    5
    - (void)creatShortcutItem {
    	    
        UIApplicationShortcutItem *item1 = [[UIApplicationShortcutItem alloc] initWithType:@"com.long.he2" localizedTitle:@"吃饭" localizedSubtitle:@"我是案例看时间多浪费卡洛斯 跨境收到了缴费案例看世界的发掘" icon:[UIApplicationShortcutIcon iconWithType:UIApplicationShortcutIconTypeAdd] userInfo:@{@"icon": @"iconinfo"}];
        [UIApplication sharedApplication].shortcutItems = @[item1];
    }
    
点击事件响应
1
2
3
4
5
6
7
- (void)application:(UIApplication *)application performActionForShortcutItem:(UIApplicationShortcutItem *)shortcutItem completionHandler:(void (^)(BOOL))completionHandler{
    if ([shortcutItem.type isEqualToString:@"com.long.he"]) {
        DTableViewController *tableV = [[DTableViewController alloc] init];
        [(UINavigationController *)application.keyWindow.rootViewController pushViewController:tableV animated:YES];
        completionHandler(YES);
    }
}

重压出现today样式