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. 如果你报createSQLQuery is not valid without active transaction,请看这里

    原文:https://blog.csdn.net/yinjian520/article/details/8666695 很多时候我们使用hibernate的session时,都是让session在某一 ...

  2. .net用url重写URLReWriter实现任意二级域名

    .net用url重写URLReWriter实现任意二级域名 这两天需要用到URLReWriter来搞那个猪头的Blog,网上看到篇好文,收藏 摘要:解释了url重写的相关知识.用asp.net实现二级 ...

  3. CCKiller:Linux轻量级CC攻击防御工具,秒级检查、自动拉黑和释放 《CCKiller:Linux轻量级CC攻击防御工具,秒级检查、自动拉黑和释放》来自张戈博客

    张戈博客很久以前分享过一个CC攻击的防御脚本,写得不怎么样,不过被51CTO意外转载了.博客从此走上了经常被人拿来练手的不归之路. 当然,还是有不少朋友在生产环境使用,并且会留言询问相关问题.根据这些 ...

  4. Atitit.面向接口的web 原理与设计重写 路由启动绑定配置url router rewriting urlpage  mvc mvp的 java c#.net php js

    Atitit.面向接口的web 原理与设计重写 路由启动绑定配置url router rewriting urlpage  mvc mvp的 java c#.net php js 原理 通过vm带入启 ...

  5. centos7 firefox 安装flash

    在官网下载flash的tar包 https://get.adobe.com/flashplayer/?spm=a2h0j.8191423.movie_player.5~5~5~8~A 在下载tar包的 ...

  6. java 原生定时执行程序(ScheduledExecutorService)

    package ThreadPoolTest; import java.util.Date; import java.util.concurrent.*; public class Main { pu ...

  7. Linux Apache安装加载mod_deflate模块

    为了开启apache服务器中的gzip压缩功能,mod_deflate模块是必须安装加载的.现在介绍如何安装.1.进入到mod_deflate.c目录 cd /lamp/httpd-2.2.20/mo ...

  8. 配置LANMP环境(10)-- 安装memcached与redis

    一.安装memcached 1.安装 yum install memcached 2.设置1G内存 memcached -u memcached -p -m -c -d 3.启动与设置开机自启动 sy ...

  9. Iterator模式----一个一个遍历

    说起遍历,我立马就想到for循环,增强for循环,foreach循环这类的循环遍历,这个不错,既然有这么方便的遍历,为什么我们还要学习Iterator这样的遍历呢? 一个重要的理由是:引入Iterat ...

  10. shiro自定义logout filter

    虽然shiro有自己默认的logout过滤器,但是,有些时候,我们需要自己定义一下操作,比如说loutgout后,进入指定页面,或者logout后写入日志操作,这个时候,我们可以通过自定义logout ...