美文网首页iOS DeveloperiOS开发iOS && Android
3D Touch 简单介绍与接入指南(动图演示)

3D Touch 简单介绍与接入指南(动图演示)

作者: yunyang088 | 来源:发表于2016-04-14 17:33 被阅读1088次

0x00 前言

本文内容包括

  • 3D Touch 简单介绍
  • Home Screen Quick Actions(动态生成)
  • Peek&Pop
  • Peek上拉快捷操作

文中的代码均取自本人的Github项目 For3DTouchDemo

0x01 3D Touch 简单介绍

3D Touch是苹果在2015年发布iPhone6s,iPhone 6s plus时加入的全新的触控方式。

官方备忘录提供的快捷功能
  • 在应用中用户可以通过按压视图来预览到更多内容和更快地访问功能
官方邮件应用的预览
Peek的快捷菜单

具体功能说明可以查看官方介绍:iOS Developer Library - Getting Started with 3D Touch

0x02 Home Screen Quick Actions(动态生成)

静态生成方法请查看官方文档: UIApplicationShortcutItems

动态方法是在项目中创建UIApplicationShortcutItem对象(一般放置在AppDelegate 中)

- (void)configShortcutItems{
    UIApplicationShortcutItem *item0 = [[UIApplicationShortcutItem alloc]initWithType:@"0" localizedTitle:@"NO.1"localizedSubtitle:nil icon:[UIApplicationShortcutIcon iconWithType:UIApplicationShortcutIconTypeAdd] userInfo:nil];
    UIApplicationShortcutItem *item1 = [[UIApplicationShortcutItem alloc]initWithType:@"1" localizedTitle:@"NO.2"localizedSubtitle:nil icon:[UIApplicationShortcutIcon iconWithType:UIApplicationShortcutIconTypeAdd] userInfo:nil];
    UIApplicationShortcutItem *item2 = [[UIApplicationShortcutItem alloc]initWithType:@"2" localizedTitle:@"NO.3"localizedSubtitle:nil icon:[UIApplicationShortcutIcon iconWithType:UIApplicationShortcutIconTypeAdd] userInfo:nil];
    
    [UIApplication sharedApplication].shortcutItems = @[item0, item1, item2];
}

注意!该段代码在程序中被执行过一次后才会被添加到主屏幕的 ShortcutItems 菜单中,所以首次安装完后不能直接使用。ShortcutItems最多可以设置4个,且静态生成在动态生成之前。

//这是UIApplicationShortcutItem中图标的类
NS_CLASS_AVAILABLE_IOS(9_0) __TVOS_PROHIBITED
@interface UIApplicationShortcutIcon : NSObject <NSCopying>

// Create an icon using a system-defined image.
+ (instancetype)iconWithType:(UIApplicationShortcutIconType)type;

// Create an icon from a custom image.
// The provided image named will be loaded from the app's bundle
// and will be masked to conform to the system-defined icon style.
+ (instancetype)iconWithTemplateImageName:(NSString *)templateImageName;

UIApplicationShortcutIconType是官方提供的图标标识,少部分才可以在9.0上显示,大部分只能在iOS 9.1之后使用。接入正式项目的话还是自己做图标比较好。

设置好ShortcutItems之后,我们就需要复写Appdelegate中的方法

- (void)application:(UIApplication *)application 
    performActionForShortcutItem:(UIApplicationShortcutItem *)shortcutItem 
    completionHandler:(void (^)(BOOL))completionHandler
- (void)application:(UIApplication *)application performActionForShortcutItem:(UIApplicationShortcutItem *)shortcutItem completionHandler:(void (^)(BOOL))completionHandler{
    SubViewController *subVC = [[SubViewController alloc] init];
    subVC.isPop = YES;
    subVC.number = [shortcutItem.type integerValue] + 1;
    self.window.rootViewController = [[UINavigationController alloc]initWithRootViewController:self.rootviewController];
    [self.rootviewController.navigationController pushViewController:subVC animated:NO];
}

然后在真机上运行APP,就像这样


按压APP

0x03 Peek&Pop

交互过程分为三个步骤:

  1. 轻压UI元素,周围界面变模糊,提示用户这边3d touch中peek可用
  2. 再稍微用力,会弹出对应元素的预览视图,若视图上有交互控件,通过向上滑动,可以进一步操作
  3. 继续用力,触发pop方法,进入预览的视图或者其他自定义的操作

注意! 按压触发需要的力度跟用户的设置有关

首先需要遵循系统的代理方法UIViewControllerPreviewingDelegate

检查3D Touch是否可用

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

然后需要实现两个代理方法

  • peek
- (nullable UIViewController *)previewingContext:(id<UIViewControllerPreviewing>)previewingContext viewControllerForLocation:(CGPoint)location{
    NSIndexPath *indexPath = [self.tableview indexPathForRowAtPoint:location];
    if (indexPath && indexPath.row >= 0 && indexPath.row < 6) {
        SubViewController *subVC = [[SubViewController alloc] init];
        subVC.number = indexPath.row + 1;
        return subVC;
    }
    return nil;
}
  • pop
- (void)previewingContext:(id <UIViewControllerPreviewing>)previewingContext commitViewController:(UIViewController *)viewControllerToCommit {
    SubViewController *vc = (SubViewController *)viewControllerToCommit;
    vc.isPop = YES;
    [self.navigationController pushViewController:vc animated:YES];
}

效果图如下


peek&pop

0x04 peek上拉快捷操作

被预览的视图控制器中实现以下方法

- (NSArray<id<UIPreviewActionItem>> *)previewActionItems;
  • 具体代码
- (NSArray<id<UIPreviewActionItem>> *)previewActionItems{
    UIPreviewAction *item1 = [UIPreviewAction actionWithTitle:@"Share me!" style:UIPreviewActionStyleDefault handler:^(UIPreviewAction * _Nonnull action, UIViewController * _Nonnull previewViewController) {
        NSLog(@"share");
    }];
    NSArray *actions = @[item1];
    return actions;
}

效果图如下


peek menu

(感谢您的阅读与支持_

相关文章

  • 3D Touch 简单介绍与接入指南(动图演示)

    0x00 前言 本文内容包括 3D Touch 简单介绍 Home Screen Quick Actions(动态...

  • 3D Touch 开发

    一. 3D Touch开发 官方文档给出的应用介绍主要有两块: 简单来说 3d touch 就是通过区分轻按和...

  • #3DTouch

    3D touch介绍 3D touch 是ios9+、iphone6s+的新功能,简单的说3Dtouch就是用力按...

  • 3D touch

    3D Touch 简单应用 - 简书 3D Touch学习笔记 - 简书

  • iOS 3D touch开发 Force Properties-

    iOS 3D touch开发(三) Force Properties-按压力度 3D touch介绍 3D tou...

  • iOS开发之3D Touch

    1.简单的介绍一下3D Touch 3D Touch的触控技术,被苹果称为新一代多点触控技术。其实,就是此前在Ap...

  • python 简单RPC示例

    利用 SimpleXMLRPCServer 演示 简单RPC例子 最后效果 动图演示 服务端 客户端 思路: 服务...

  • SwiftCafe 快报 - 3D Touch 之 Peek &

    在前面几期快报中,我们介绍了 3D Touch 的基本内容,以及如何创建 3D Touch 桌面快捷菜单。这次咱们...

  • iOS 3D Touch

    一、3D Touch 介绍 3D Touch 是 Apple 推出的通过压力感触区分轻按和重按来进行不同的用户交互...

  • Objc 精选 - 3D Touch 之 Peek &

    在前面几期快报中,我们介绍了 3D Touch 的基本内容,以及如何创建 3D Touch 桌面快捷菜单。这次咱们...

网友评论

    本文标题:3D Touch 简单介绍与接入指南(动图演示)

    本文链接:https://www.haomeiwen.com/subject/xbrglttx.html