3DTouch开发

图片来自百度百科

3D Touch是一种立体触控技术,被苹果称为新一代多点触控技术,是在Apple Watch上采用的Force Touch,屏幕可感应不同的感压力度触控。3D Touch,苹果iPhone 6s的新功能,看起来类似 PC 上的右键。有Peek Pop 两种新手势。

2015年9月10日,苹果在新品发布会上宣布了3D-Touch功能。
2016年6月13日,苹果开发者大会WWDC在旧金山召开,会议宣布可以在待机画面用3D Touch操作通知。

产品介绍

用力按一个图标会弹出一层半透明菜单,里面包含了该应用下的一些快捷操作,在邮件列表上用力按也可以快速弹开窗口查看邮件,在邮件列表当中,用力按邮件列表里的一条邮件就会显示完整邮件内容,松手就会关掉,无需左右滑动的打开关闭操作。轻点电话就可以查看最近联系人,按压相机可以快速自拍,按压图片库可以快速浏览大图

轻按弹出窗口预览邮件内容,增加拇指压力时则可以全屏,在日程安排中轻轻用力按航班信息即可弹出窗口显示航班信息。按住地图图标即可弹出一些快速选项,Instagram中可以直接进入自己的时间流或者友邻动态,在相机中可以通过轻按相机预览拍的照片,而且在iOS9中可以快速通过边缘轻按调出后台。可以说,3D-Touch大幅度提升了手机操作的效率。
新iPhone增加了3D-Touch技术,相对于多点触摸在平面二维空间的操作,3D-Touch技术增加了对力度和手指面积的感知,可以通过长按快速预览/查看你想要的短信/图片/超链接等内容,Peek和Pop手势的响应时间可迅捷到10ms和15ms。
相机与3D-Touch技术结合,相应的相机中增加拍摄动态图片的功能选项,查看时只需要用一定力道向图片按下去,图片就会动起来。

可以在待机画面用3D Touch操作通知。

ShortcutItem

ShortcutItem 快捷方式
ShortcutItem 快捷方式

初始化ShortcutItem

- (void)createItemsWithIcons {
    // icons with my own images,icon size 35*35
    UIApplicationShortcutIcon *icon1 = [UIApplicationShortcutIcon iconWithType:UIApplicationShortcutIconTypeFavorite];
    UIApplicationShortcutIcon *icon2 = [UIApplicationShortcutIcon iconWithType:UIApplicationShortcutIconTypeSearch];
    UIApplicationShortcutIcon *icon3 = [UIApplicationShortcutIcon iconWithType:UIApplicationShortcutIconTypeShare];
    
    // create several (dynamic) shortcut items
    UIMutableApplicationShortcutItem *item1 = [[UIMutableApplicationShortcutItem alloc] initWithType:@"com.touch.deep1" localizedTitle:@"喜爱" localizedSubtitle:@"我的收藏" icon:icon1 userInfo:nil];
    UIMutableApplicationShortcutItem *item2 = [[UIMutableApplicationShortcutItem alloc] initWithType:@"com.touch.deep2" localizedTitle:@"搜索" localizedSubtitle:@"搜索活动" icon:icon2 userInfo:nil];
    UIMutableApplicationShortcutItem *item3 = [[UIMutableApplicationShortcutItem alloc] initWithType:@"com.touch.deep3" localizedTitle:@"分享" localizedSubtitle:@"分享到朋友圈" icon:icon3 userInfo:nil];
    
    // add all items to an array
    /**
     *  防止不支持设备引起崩溃
     */
    if (item1 && item2 && item3) {
        NSArray *items = @[item1, item2, item3];
        
        // add the array to our app
        [UIApplication sharedApplication].shortcutItems = items;
    }
}

ShortcutItem事件

- (void)application:(UIApplication *)application performActionForShortcutItem:(UIApplicationShortcutItem *)shortcutItem completionHandler:(void (^)(BOOL))completionHandler {
    // react to shortcut item selections
    NSLog(@"A shortcut item was pressed. It was %@.", shortcutItem.localizedTitle);
    
    // have we launched Deep Link Level 1
    if ([shortcutItem.type isEqualToString:@"com.touch.deep1"]) {
        NSLog(@"clicked deep1");
        PreviewViewController *vc = [PreviewViewController new];
        self.window.rootViewController = vc;
        [self.window makeKeyWindow];
    }
    
    if ([shortcutItem.type isEqualToString:@"com.touch.deep2"]) {
        NSLog(@"clicked deep2");
        [[LKGlobalNavigationController sharedInstance] pushViewControllerWithUrLPattern:PREVIEW_VIEW_CONTROLLER];
    }
    
    if ([shortcutItem.type isEqualToString:@"com.touch.deep3"]) {
        NSLog(@"clicked deep3");
    }
}

UIPreviewActionItem 快速操作

UIPreviewActionItem 快速操作
UIPreviewActionItem 快速操作

UIPreviewActionItem事件

// setup a list of preview actions
- (NSArray<id<UIPreviewActionItem>> *)previewActionItems {
    UIPreviewAction *action1 = [UIPreviewAction actionWithTitle:@"保存" style:UIPreviewActionStyleDefault handler:^(UIPreviewAction * _Nonnull action, UIViewController * _Nonnull previewViewController) {
        NSLog(@"Default Action triggered");
    }];
    
    UIPreviewAction *action2 = [UIPreviewAction actionWithTitle:@"分享" style:UIPreviewActionStyleDefault handler:^(UIPreviewAction * _Nonnull action, UIViewController * _Nonnull previewViewController) {
        NSLog(@"Default Action triggered");
    }];
    
    UIPreviewAction *action3 = [UIPreviewAction actionWithTitle:@"删除" style:UIPreviewActionStyleDestructive handler:^(UIPreviewAction * _Nonnull action, UIViewController * _Nonnull previewViewController) {
        NSLog(@"Destructive Action triggered");
    }];
    
    // add them to an arrary
    NSArray *actions = @[action1, action2, action3];
    
    // add all actions to a group
//    UIPreviewActionGroup *group1 = [UIPreviewActionGroup actionGroupWithTitle:@"Action Group" style:UIPreviewActionStyleDefault actions:actions];
//    NSArray *group = @[group1];
    
    // and return them (return the array of actions instead to see all items ungrouped)
    return actions;
}

Peek And Pop 预览

Peek And Pop 预览
Peek And Pop 预览

注册UIViewControllerPreviewingDelegate

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    TestTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"TestTableViewCell"];
    // Enable to use "-sizeThatFits:"
    cell.fd_enforceFrameLayout = NO;

    cell.entity = self.titleArray[indexPath.row%10];

    if (self.traitCollection.forceTouchCapability == UIForceTouchCapabilityAvailable) {
        [self registerForPreviewingWithDelegate:self sourceView:cell];
    }
        
    return cell;
}

实现UIViewControllerPreviewingDelegate

# pragma mark - 3D Touch Delegate

// 轻压显示预览页面
// check if we're not already displaying a preview controller
- (UIViewController *)previewingContext:(id<UIViewControllerPreviewing>)previewingContext viewControllerForLocation:(CGPoint)location {
    if ([self.presentedViewController isKindOfClass:[DetailViewController class]]) {
        return nil;
    }

    DetailViewController *detailView = [[DetailViewController alloc] init];
    
    return detailView;
}

// 重压显示详情页面
// alternatively, use the view controller that's being provided here (viewControllerToCommit)
- (void)previewingContext:(id<UIViewControllerPreviewing>)previewingContext commitViewController:(UIViewController *)viewControllerToCommit {
    [self showViewController:viewControllerToCommit sender:self];
}

参考文档

百度百科

demo

3DTouchDemo

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 159,219评论 4 362
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,363评论 1 293
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 108,933评论 0 243
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,020评论 0 206
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,400评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,640评论 1 219
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,896评论 2 313
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,597评论 0 199
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,327评论 1 244
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,581评论 2 246
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,072评论 1 261
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,399评论 2 253
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,054评论 3 236
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,083评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,849评论 0 195
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,672评论 2 274
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,585评论 2 270

推荐阅读更多精彩内容