手把手教你如何实现3D Touch

很久之前就想写一篇关于3D Touch的博客,因为各种原因一直到现在才开始写(其实就是懒>_<)。咳咳,趁着上班时间赶紧来写一篇…demo请戳这里


3D Touch分为以下三大模块:
1.Home Screen Quick Actions
2.Peek and Pop
3.UITouch


Home Screen Quick Actions

重按主界面应用图标后弹出的快捷菜单,例如重按微信会出现这样的效果。
这里写图片描述
实现Quick Actions分为两种:静态和动态。

一、Static(静态)Quick Actions
Static Quick Actions只需要我们在项目的info.plist文件中配置,在用户安装程序后就可以使用。
配置Static Quick Actions只需要在info.plist文件中添加如下键值。

1、必填项(下面两个键值是必须设置的):
UIApplicationShortcutItemType 这个键值设置一个快捷通道类型的字符串
UIApplicationShortcutItemTitle 这个键值设置标签的标题
2、选填项(下面这些键值不是必须设置的):
UIApplicationShortcutItemSubtitle 设置标签的副标题
UIApplicationShortcutItemIconType 设置标签Icon类型
UIApplicationShortcutItemIconFile 设置标签的Icon文件
UIApplicationShortcutItemUserInfo 设置信息字典(用于传值)
这里写图片描述
如图配置好info.plist文件之后,运行程序,真机上测试效果如下:
这里写图片描述

二、Dynamic(动态) Quick Actions
Dynamic Quick Actions是我们在程序中通过代码添加,与之相关的类主要有3个:
UIApplicationShortcutItem 创建3DTouch action的类
UIMutableApplicationShortcutItem 创建可变的3DTouch action的类
UIApplicationShortcutIcon 创建标签中图片icon的类

UIApplicationShortcutIcon *icon1 = [UIApplicationShortcutIcon iconWithType:UIApplicationShortcutIconTypeInvitation];
    UIMutableApplicationShortcutItem *item1 = [[UIMutableApplicationShortcutItem alloc]initWithType:@"1" localizedTitle:@"么么哒"];
    item1.icon = icon1;

    UIApplicationShortcutIcon *icon2 = [UIApplicationShortcutIcon iconWithType:UIApplicationShortcutIconTypeContact];
    UIApplicationShortcutItem *item2 = [[UIApplicationShortcutItem alloc]initWithType:@"2" localizedTitle:@"萌萌哒" localizedSubtitle:@"看这里是不是想到了tableView" icon:icon2 userInfo:nil
                                                   ];
    application.shortcutItems = @[item1,item2];

我在application:didFinishLaunchingWithOptions:方法里面添加了以上代码,运行程序,真机测试的效果如下:
这里写图片描述
上述代码里面设置的icon不是UIImage,而是一个叫UIApplicationShortcutIcon的类。
这里写图片描述
API很简单吧~值得注意的是官方API里面的这句话:

Icons should be square, single color, and 35x35 points, as shown in these template files and as described in Template Images in UIKit User Interface Catalog and in iOS Human Interface Guidelines.

三、Actions 被点击后的响应
类似推送,当我们点击Actions进入应用程序时,也可以进行一些操作,我们可以发现,在application中增加了这样一个方法:
这里写图片描述
当我们通过点击Actions进入app时,就会在appdelegate中调用这样一个回调,我们可以获取shortcutItem的信息进行相关逻辑操作。

- (void)application:(UIApplication *)application performActionForShortcutItem:(UIApplicationShortcutItem *)shortcutItem completionHandler:(void (^)(BOOL))completionHandler {

    if ([shortcutItem.localizedTitle isEqualToString:@"New Message"])
    {

        UIAlertController *alertC = [UIAlertController alertControllerWithTitle:@"老板" message:@"过来办公室,发妹子了" preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *action = [UIAlertAction actionWithTitle:@"马上就来" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            [alertC dismissViewControllerAnimated:YES completion:nil];
        }];
        [alertC addAction:action];
        [self.window.rootViewController presentViewController:alertC animated:YES completion:nil];

    }
    else if ([shortcutItem.localizedTitle isEqualToString:@"Favorites"])
    {

        UINavigationController *naVC = (UINavigationController *)self.window.rootViewController;
        [naVC popToRootViewControllerAnimated:NO];
        ThirdViewController *thirdVC = [[ThirdViewController alloc]init];
        [naVC pushViewController:thirdVC animated:YES];
    }
}

我在application: performActionForShortcutItem: completionHandler:方法里面写了这样一段代码,然后通过点击外界的New Message Action进入APP,就会走第一个if里面的语句,点击外界的Favorites Action进入APP,就会走第二个if里面的语句。真机测试效果如下:
这里写图片描述

这里有一点需要注意:我们在app的入口函数:
- (BOOL)application:(UIApplication )application didFinishLaunchingWithOptions:(NSDictionary )launchOptions;
需要进行一下判断,在luanchOptions中有个UIApplicationLaunchOptionsShortcutItemKey这样一个键,通过它,我们可以区别是否是从标签进入的APP,如果是则处理结束逻辑后,返回NO,防止处理逻辑被反复回调。


Peek and Pop

Peek and Pop实际上就是基于重按的两个操作:peek 和pop。使用场景是item的详细信息不能完全展示出来时,通过3D Touch来预览以及跳转至详情页。苹果在这方面的封装很严实,我们不能修改动画的样式,只能修改preview(预览)窗的高度。什么?不知道具体的效果?好吧,先带你们看一下Peek and Pop的效果。
这里写图片描述

1、Peek
在第一次重按item时触发的操作,会弹出预览框。弹出预览框后,如果这时松手,那么预览框也会随即消失。
2、Pop
Peek操作完成后不松手,再次重按,就可以跳转至详情页了。在跳转至详情页的过程中,我们还可以看到一个轻微的纵向弹性动画。

看完了Peek and Pop 我们就来简单的实现一下吧。

//首先接受代理
@interface ForthTableViewController () <UIViewControllerPreviewingDelegate>
//然后在viewDidLoad里面注册一下registerForPreviewingWithDelegate:sourceView:
- (void)viewDidLoad {
    [super viewDidLoad];

    //判断是否支持3D Touch,如果支持则需要注册一下
    if (self.traitCollection.forceTouchCapability == UIForceTouchCapabilityAvailable) {
        [self registerForPreviewingWithDelegate:self sourceView:self.view];
    }
}
//实现代理方法
//Peek手势触发,进入预览界面
- (nullable UIViewController *)previewingContext:(id <UIViewControllerPreviewing>)previewingContext viewControllerForLocation:(CGPoint)location
{
    NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:location];
    UITableViewCell *cell  = [self.tableView cellForRowAtIndexPath:indexPath];
    previewingContext.sourceRect = cell.frame;
    ThirdViewController *childVC = [[ThirdViewController alloc] init];
    UILabel *label      = [[UILabel alloc]init];
    label.text          = cell.textLabel.text;
    label.textAlignment = NSTextAlignmentCenter;
    label.textColor     = [UIColor redColor];
    childVC.view        = label;
    childVC.view.backgroundColor = [UIColor whiteColor];
    childVC.preferredContentSize = CGSizeMake(0.0f,400.0f);

    return childVC;
}

//Pop手势触发,进入详情界面
- (void)previewingContext:(id <UIViewControllerPreviewing>)previewingContext commitViewController:(UIViewController *)viewControllerToCommit
{
    [self showViewController:viewControllerToCommit sender:self];
}

代码写到这里,真机运行一下看看效果:
这里写图片描述
效果看起来还不错~~^_^

接下来我们想起来还有个家伙叫UIPreviewAction,它是Peek预览时上滑底部会出现的菜单。那么该如何添加预览时上滑底部的菜单呢?
只需要在我们创建的预览控制器(就是上方的ThirdViewController *childVC)里面重写一下如下这个方法:

- (NSArray <id <UIPreviewActionItem>> *)previewActionItems {
    UIPreviewAction *p1 = [UIPreviewAction actionWithTitle:@"点我!"style:UIPreviewActionStyleDefault handler:^(UIPreviewAction * _Nonnull action, UIViewController * _Nonnull previewViewController) {

        NSLog(@"点击了点我!");
    }];
    UIPreviewAction *p2 = [UIPreviewAction actionWithTitle:@"别点我!" style:UIPreviewActionStyleDefault handler:^(UIPreviewAction * _Nonnull action, UIViewController * _Nonnull previewViewController) {

        NSLog(@"点击了别点我!");
    }];
    NSArray *actions = @[p1,p2];
    return actions;
}

我写的代码如上所示,然后直接真机跑起来看看效果:
这里写图片描述


UITouch

UITouch里面多了两个API,分别是力度和最大力度。这个就可以很个性化地做一些酷炫的交互操作了!没啥好说的,touch.force和touch.maximumPossibleForce。大家可以在官方API里面很容易找到他们。看一下就哦了。
这里写图片描述


结束语:刚开始写博客,如有疏漏或者错误之处,欢迎留言指正。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值