10分钟教你学会3D Touch

10分钟教你学会3D Touch


支持的设备

目前支持的设备有: iPhone 6s , iPhone 6s Plus, iPhone 7s , iPhone 7s Plus


Home Screen Quick Actions

当你用力按压图标时,会弹出一个快捷操作栏,如图。是不是非常炫酷,其实实现很简单,接下来我们看看如何实现这么炫酷的功能,苹果官方提供了两种实现方式给我们,静态方式和动态方式
官方配图


静态方式

静态方式只需要我们按照格式设置info.plist就ok了,如下:
这里写图片描述

  1. UIApplicationShortcutItemIconType:设置图标类型,官方提供了几十种类型的图标样式给我们使用,具体样式见下表:
    https://developer.apple.com/library/prerelease/content/documentation/General/Reference/InfoPlistKeyReference/Articles/iPhoneOSKeys.html#//apple_ref/doc/uid/TP40009252-SW36
  2. UIApplicationShortcutItemTitle:快速操作的标题
  3. UIApplicationShortcutItemSubtitle:快速操作的子标题
  4. UIApplicationShortcutItemType:快速操作的类型
  5. UIApplicationShortcutItemUserInfo:用户可以在这个属性中添加一些自定义的东西,比如系统的版本

动态方式

动态方式是需要我们写代码去添加,代码如下:

    NSMutableArray *items = [NSMutableArray array];
    UIMutableApplicationShortcutItem *item = [[UIMutableApplicationShortcutItem alloc] initWithType:@"type" localizedTitle:@"动态添加QuickAction"];
    item.icon = [UIApplicationShortcutIcon iconWithType:UIApplicationShortcutIconTypePlay];
    item.localizedSubtitle = @"动态添加的内容";
    [items addObject:item];
    [[UIApplication sharedApplication] setShortcutItems:items]; 

点击事件的系统回调方法在app delegate中,在这里你可以做你想做的操作

- (void)application:(UIApplication *)application performActionForShortcutItem:(UIApplicationShortcutItem *)shortcutItem completionHandler:(void (^)(BOOL))completionHandler {
    NSLog(@"%@", shortcutItem);
}

总结

静态方式VS静态方式:
对于静态方式而言,在编译时就开始部署了,所以当你的app安装之后就已经生效了。但是对于动态方式而言,它是运行时开始执行的,所以你的app必须运行一次才能生效。
除此之外,系统本身对条目也是有限制的,当你的数目过多时,系统会选择忽略一些,这个时候静态方式配置的优先被显示。


Peek and Pop

介绍

peek功能是当时我们用力按压view的时候会先有一个预览,当你继续用力的时候就会push你预览的视图控制器,这个过程一共分三个阶段:
1.系统提示你将要peek,周围的环境会产生模糊效果,如图
这里写图片描述

2.当你继续用力时会产生预览效果
这里写图片描述

3.如果此时上下滑动,视图下面还会产生操作选项,这个也是我们自己配置的,这时候如果松手,就会还原
这里写图片描述


实现方式

首先你的控制器要遵守UIViewControllerPreviewingDelegate协议,并实现协议方法

- (UIViewController *)previewingContext:(id<UIViewControllerPreviewing>)previewingContext viewControllerForLocation:(CGPoint)location {
    CGPoint cellPoint = [self.view convertPoint:location toView:self.tableView];
    NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:cellPoint];
    UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
    if (cell == nil) return nil;

    LXDetailViewController *detailViewController = [[LXDetailViewController alloc] init];
    detailViewController.myTitle = @"你敢在用力一点?";
    NSDictionary *dict = self.dataArray[indexPath.row];
    detailViewController.preferredContentSize = CGSizeMake(0, [dict[@"height"] integerValue]);

    CGRect cellFrame = [self.tableView convertRect:cell.frame toView:self.view];
    cellFrame = [cell convertRect:cell.bounds toView:self.view];
    previewingContext.sourceRect = cellFrame;

    return detailViewController;
}

当我们用力按压的时候该方法就会回调,返回的detailViewController就是我们要预览的控制器,除了previewingContext.sourceRect之外的内容都会产生模糊效果。


Peek quick actions

实现预览视图下面的快捷操作栏也非常简单,只需要我们重写控制器下的这个方法就ok了,

- (NSArray<id<UIPreviewActionItem>> *)previewActionItems {
    NSMutableArray *previewActionArray = [NSMutableArray array];

    UIPreviewAction *action1 = [UIPreviewAction actionWithTitle:@"默认样式" style:UIPreviewActionStyleDefault handler:^(UIPreviewAction * _Nonnull action, UIViewController * _Nonnull previewViewController) {

    }];
    [previewActionArray addObject:action1];
    UIPreviewAction *action2 = [UIPreviewAction actionWithTitle:@"选中样式" style:UIPreviewActionStyleSelected handler:^(UIPreviewAction * _Nonnull action, UIViewController * _Nonnull previewViewController) {

    }];
    [previewActionArray addObject:action2];
    UIPreviewAction *action3 = [UIPreviewAction actionWithTitle:@"破坏性样式" style:UIPreviewActionStyleDestructive handler:^(UIPreviewAction * _Nonnull action, UIViewController * _Nonnull previewViewController) {

    }];
    [previewActionArray addObject:action3];

    UIPreviewActionGroup *group = [UIPreviewActionGroup actionGroupWithTitle:@"组操作" style:UIPreviewActionStyleDefault actions:@[action1, action2, action3]];
    [previewActionArray addObject:group];

    return previewActionArray;
}

这里一共添加了四个action,前三个只是样式不同,最后一个是组操作,组操作的效果不同于普通的操作,当你点击一个组操作的时候,会弹出新的action列表。


说了这么多,不如来个demo实在,下面是我写的一个demo,如果对大家有帮助的话记得给我star哦。
https://github.com/coderLCX/3DTouchDemo.git


参考文档:
1.https://developer.apple.com/library/prerelease/content/documentation/UserExperience/Conceptual/Adopting3DTouchOniPhone/index.html#//apple_ref/doc/uid/TP40016543
2.https://developer.apple.com/library/prerelease/content/documentation/General/Reference/InfoPlistKeyReference/Articles/iPhoneOSKeys.html#//apple_ref/doc/uid/TP40009252-SW36
3.https://developer.apple.com/library/prerelease/content/documentation/UserExperience/Conceptual/Adopting3DTouchOniPhone/3DTouchAPIs.html#//apple_ref/doc/uid/TP40016543-CH4-SW1

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值