iPhone 6s/6s Plus提供了触摸屏的另一个维度的操作手势-3D Touch,通常有下面两种应用场景:

  1. 在主屏幕上重按APP图标可以提供进入APP特定功能的快捷菜单
  2. 在APP内部,可以通过重按屏幕获得额外的快捷操作

主屏幕快捷菜单

iOS 9 SDK提供了API来定义两种类型的快捷菜单:

  1. 静态快捷菜单:在Info.plist定义UIApplicationShortcutItems数组
  2. 动态快捷菜单:使用UIApplicationShortcutItem类来定义菜单,使用UIApplication里面的shortcutItems属性来设定动态菜单

快捷菜单可以显示最多两行文字和一个可选的图标。

一瞥&弹出 (Peek&Pop)

UIViewController可以对用户点击有不同的响应,随着按击的深度不断加深,页面会有三个阶段:

  1. 标示内容预览是可以的

  2. 显示预览 (一瞥),同时提供快捷动作按钮

  3. 选择性的跳转到新的页面显示预览 (弹出)

   

3D Touch API

iOS 9提供如下3D Touch的API:

  1. 主屏幕快捷菜单API (Home screen quick action API)
  2. UIKit的一瞥和弹出API (UIKit peek and pop API)
  3. WebView的一瞥和弹出API (Web view peek and pop API)
  4. UITouch force属性:让你可以自定义基于力度的用户操作

不管使用何种API,你都需要在运行时来检测3D Touch特性是否可用。

检查3D Touch特性的可用性

if (self.traitCollection.forceTouchCapability == UIForceTouchCapabilityAvailable) {
  NSLog(@"你的手机支持3D Touch!");
} else {
  NSLog(@"你的手机暂不支持3D Touch!");
}

主屏幕快捷菜单

1. 静态菜单

可以在Info.plist里面定义静态的菜单

2. 动态菜单

在AppDelegate里使用UIApplicationShortcutItem来定义菜单,然后设置UIApplication的shortcutItems属性

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
UIApplicationShortcutItem *shortItem1 = [[UIApplicationShortcutItem alloc] initWithType:@"Open1" localizedTitle:@"Open1"];
UIApplicationShortcutItem *shortItem2 = [[UIApplicationShortcutItem alloc] initWithType:@"Open2" localizedTitle:@"Open2"];
NSArray *shortItems = [[NSArray alloc] initWithObjects:shortItem1, shortItem2, nil];
[[UIApplication sharedApplication] setShortcutItems:shortItems];
return YES;
}

处理菜单事件

- (void)application:(UIApplication *)application performActionForShortcutItem:(UIApplicationShortcutItem *)shortcutItem completionHandler:(void (^)(BOOL))completionHandler{
if ([shortcutItem.localizedTitle isEqual: @"Open1"]) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"OPPS!" message:@"Open1" delegate:self cancelButtonTitle:@"ok" otherButtonTitles:nil, nil];
[alert show];
return;
}
}

更多细节可以参考

UIKit的一瞥和弹出(Peek&Pop)

Peek: 当用户点击某些特定的View可以提供一些额外的内容

Pop: 跳转到那个显示额外内容的页面

iOS 9 SDK包含以下3D Touch相关的特性

1. UIViewController里面的新方法用来注册或者注销一个View Controller是否拥有3D Touch特性

2. ViewController的3D Touch的协议(Protocols)

支持Peek的快捷按钮,iOS 9 SDK包含:

通常情况下需要兼容是否支持3D Touch的设备,如果不支持3D Touch可以使用UILongPressGesture来替代。

Interface ViewController () <UIViewControllerPreviewingDelegate>
{
UILongPressGestureRecognizer *_longPress;
}
@end @implementation ViewController - (void)viewDidLoad {
[super viewDidLoad];
UILongPressGestureRecognizer *longPressGr = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressToDo)];
_longPress = longPressGr;
} //检测页面是否处于3DTouch
- (void)check3DTouch{ if (self.traitCollection.forceTouchCapability == UIForceTouchCapabilityAvailable) { [self registerForPreviewingWithDelegate:self sourceView:self.view];
NSLog(@'3D Touch 开启');
//长按停止
_longPress.enabled = NO; }else{
_longPress.enabled = YES;
} } - (void)viewWillAppear:(BOOL)animated{ [self check3DTouch]; }

实现UIViewController里面关于3D Touch的Delegate方法

#pragma mark >> 3D touch 代理方法
//轻按进入浮动预览页面
- (UIViewController *)previewingContext:(id<UIViewControllerPreviewing>)previewingContext viewControllerForLocation:(CGPoint)location{
ReviewViewController *vc = [ReviewViewController alloc] init];
vc.view.frame = self.view.frame;
UIImageView *er = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@'123.png']];
vc.view = er;
return vc;
}

添加预览页面的底部快捷菜单

//预览页面 底部Action Items
- (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;
}

WebView的一瞥和弹出

在WKWebView和UIWebView可以设置属性allowsLinkPreview来支持WebView的Peek和Pop。

1. 这个属性是默认设置为NO的

2. 如果使用WKWebView或者UIWebView,Pop会跳转到Safari

3. 如果希望在APP内部保证Pop,建议使用SFSafariViewController来替代WKWebView

In iOS this property is available on devices that support 3D Touch. Default value is NO in iOS.

If you set this property’s value to YES, an iOS user can press links to preview link destinations and detected data such as addresses and phone numbers. Such previews are known to users as peeks. If a user presses deeper on a link preview, the preview navigates (or pops, in user terminology) to the destination. Because pop navigation switches the user from your app to Safari, it is opt-in for iOS apps.

If you want to support link preview in iOS but also want to keep users within your app, you can switch from using the WKWebView class to the SFSafariViewController class. If you are using a web view as an in-app browser, making this change is best practice. The Safari view controller class automatically supports link previews.

UITouch的force属性

UITouch新增两个属性:

force

1. float值,1.0代表通常的按压力度

2. 只在支持3D Touch的设备才生效,需要在使用前在运行时检查3D Touch是否可用

The force of the touch, where a value of 1.0 represents the force of an average touch (predetermined by the system, not user-specific). (read-only)

This property is available on devices that support 3D Touch. To check at runtime if a device supports 3D Touch, read the value of the forceTouchCapability property on the trait collection for any object in your app with a trait environment.

maximumPossibleForce

用于设置按压力度的最大值

总结

3D Touch为iOS提供了一种在z轴方向的操作方式,更加丰富了屏幕操作的维度。在主屏幕和APP内部都可以提供内容预览或者快捷菜单的操作,同时提供force属性来检测按压力度,相信基于这个属性可以做出很多有趣的事情。

iOS9 - 采用3D Touch的更多相关文章

  1. iOS 3D Touch功能 3 -备

    新的触摸体验——iOS9的3D Touch 一.引言 二.在模拟器上学习和测试3D Touch 附.SBShortcutMenuSimulator的安装和使用 三.3D Touch的主要应用 四.3D ...

  2. ios 3D Touch功能的实现

    ios9中3D Touch功能是一个新的亮点,这个方便快捷的功能实现也比较简单,废话不多说直接上代码, 一.3D Touch功能添加分为两种(1).静态标签 (2).动态标签 (1).静态添加 这个方 ...

  3. iOS 3D Touch功能

    新的触摸体验——iOS9的3D Touch 一.引言 在iphone6s问世之后,很多果粉都争先要体验3D Touch给用户带来的额外维度上的交互,这个设计之所以叫做3D Touch,其原理上是增加了 ...

  4. iOS9新特性-3D Touch

    本文主要讲解3DTouch各种场景下的开发方法,开发主屏幕应用icon上的快捷选项标签(Home Screen Quick Actions),静态设置 UIApplicationShortcutIte ...

  5. 在iOS9 中使用3D Touch

    iOS9提供了四类API( Home Screen Quick Action . UIKit Peek & Pop . WebView Peek & Pop 和 UITouch For ...

  6. iOS- 指压即达,如何集成iOS9里的3D Touch

    1.前言   随着6S的到来,3DTouch被各大热门APP迅速普及,博主亲自体验后,发现使用便捷性大幅提高,随后自己照着文档,写了个Demo出来,分享给大家,希望能对有需要的朋友提供有一些帮助. 2 ...

  7. Swift 玩转 3D Touch 之 Peek & Pop

    什么是3D Touch 3D Touch 是iOS9之后专为 iPhone6s 机型加入的新特性,这一新技术移植于 Mac Book 上的 ForceTouch 更准确地说应该是 ForceTouch ...

  8. 3D Touch ? 木有6s,也阔以玩!!!

    3D Touch 之 Peek & Pop 3D Touch 是iOS9之后专为 iPhone6s 机型加入的新特性,这一新技术移植于 Mac Book 上的 ForceTouch 更准确地说 ...

  9. 3D touch 的 应用 --备用

    在iPhone 6s和iPhone 6s Plus中Apple引入了3D Touch技术.3D Touch的触控技术,被苹果称为新一代多点触控技术.其实,就是此前在Apple Watch上采用的For ...

随机推荐

  1. xUtils介绍 -- DbUtils、ViewUtils、HttpUtils、BitmapUtils

    转载注明出处:https://github.com/wyouflf/xUtils xUtils简单介绍 xUtils 包括了非常多有用的android工具. xUtils 支持大文件上传,更全面的ht ...

  2. 【BIEE】由于排序顺序不兼容,集合操作失败

    问题描述 使用BIEE数据透视表时,使用了UNION进行数据组合,但是在浏览结果时意外出错了,报错如下截图: 问题分析 原因暂时未知 问题解决 目前使用UNION进行聚合,只需要将UNION修改为UN ...

  3. 简单的 nginx 多站点配置

    测试环境:基于CentOS6.8 编译安装LNMP(http://www.cnblogs.com/afee666/p/6836161.html) 一 需求 在一个 VPS 主机上配置 web 服务器, ...

  4. (九)Thymeleaf用法——Themeleaf注释

    4. 注释 模板名称:comment.html 4.1 标准 HTML/XML注释       语法:<!--     -->      4.2 解析器级注释块(Parser-level ...

  5. 修改pip源为国内网站

    import os,sys,platformini="""[global]index-url = https://pypi.doubanio.com/simple/[in ...

  6. 自定义ListView和GridView

    1 http://blog.chengyunfeng.com/?p=465 2

  7. hdu3336解读KMP算法的next数组

    查看原题 题意大致是:给你一个字符串算这里面全部前缀出现的次数和.比方字符串abab,a出现2次.ab出现2次,aba出现1次.abab出现1次.总计6次. 而且结果太大.要求对1007进行模运算. ...

  8. css 温故而知新 1px的问题

    解决方法1: //border @mixin border($pos, $color) { content: ""; position: absolute; transform-o ...

  9. 启动spring boot 异常

    再我搭建spring boot工程后,run application的时候抛出下面异常 Exception /slf4j-log4j12-.jar). If you are using WebLogi ...

  10. Unix编程第7章 进程环境

    准备雄心勃勃的看完APUE,但是总感觉看着看着就像进入一本字典,很多地方都是介绍函数的用法的,但是给出例子远不及函数介绍的多.而且这本书还是个大部头呢.第7章的讲的进程环境,进程是程序设计中一个比较重 ...