代码改变世界

3D Touch初探

2017-01-20 23:17  v2m  阅读(490)  评论(0编辑  收藏  举报

伴着6S的发布,iOS 9.0开始支持3D Touch功能。使用场景来分一共有三种情况。

一、基于UIViewController的扩展

1. 首先要注册需要监听重按手势的 source view:
```
- (id <UIViewControllerPreviewing>)registerForPreviewingWithDelegate:(id<UIViewControllerPreviewingDelegate>)delegate sourceView:(UIView *)sourceView NS_AVAILABLE_IOS(9_0);
- (void)unregisterForPreviewingWithContext:(id <UIViewControllerPreviewing>)previewing NS_AVAILABLE_IOS(9_0);
```
2. 重按手势识别出来之后需要按照 UIViewControllerPreviewingDelegate协议去处理 peek 和 pop 事件,这个协议有两个方法
```
// 对应 peek 事件,返回的是一个预览界面。
- (UIViewController *)previewingContext:(id<UIViewControllerPreviewing>)previewingContext viewControllerForLocation:(CGPoint)location {


UIViewController* previewVC;
// 期望展示的大小
previewVC.preferredContentSize = CGSizeZero;
// 源rect,除了这个rect之外的区域都会模糊
previewingContext.sourceRect = CGRectZero;
return previewVC;
}
```

```
  // 对应 pop 事件,做一些对应处理
- (void)previewingContext:(id<UIViewControllerPreviewing>)previewingContext commitViewController:(UIViewController *)viewControllerToCommit {
	
}
```
3. 另外,对于 pop 出来的预览界面,可以提供一些额外操作,这些操作需要添加在预览VC里面
```
// 添加一些 action
- (NSArray<id<UIPreviewActionItem>> *)previewActionItems
{
// action
UIPreviewAction *action = [UIPreviewAction actionWithTitle:@"--" style:UIPreviewActionStyleDefault handler:^(UIPreviewAction * _Nonnull action, UIViewController * _Nonnull previewViewController) {
	//do something
}];

// action group
UIPreviewActionGroup *previewGroup = 包含一组action;
return @[暴露的action,暴露的组];
}
```
4. XCode 7.1 之后的版本,如果支持 iOS 9.1 以上的系统,可以通过 XCode 直接把 segue 设置成 peek&pop 去实现这种效果。

二、APP 图标重按

1. 静态添加
在 Info.plist 中 添加 `UIApplicationShortcutItems ` 字段,这个字段下面的每个字典结构定义了一个 Item,key 如下:

```
`UIApplicationShortcutItemType (required)`
一般取值和 bundle id 一致,用来判断快速操作类型。
`UIApplicationShortcutItemTitle (required)`
标题
`UIApplicationShortcutItemSubtitle`
副标题,显示在标题下面
`UIApplicationShortcutItemIconType`
图标
`UIApplicationShortcutItemIconFile`
自定义图标文件,会覆盖上面的icon type
`UIApplicationShortcutItemUserInfo`
自定义信息
```

静态添加的无法修改,并且优先显示。
2. 动态添加
也就是创建自定义的 `UIApplicationShortcutItem` 然后设置给 UIApplication。看代码:

```
NSArray <UIApplicationShortcutItem *> *existingShortcutItems = [[UIApplication sharedApplication] shortcutItems];
UIApplicationShortcutItem *anExistingShortcutItem = [existingShortcutItems objectAtIndex: anIndex];
NSMutableArray <UIApplicationShortcutItem *> *updatedShortcutItems = [existingShortcutItems mutableCopy];
UIMutableApplicationShortcutItem *aMutableShortcutItem = [anExistingShortcutItem mutableCopy];
[aMutableShortcutItem setLocalizedTitle: @“New Title”];
[updatedShortcutItems replaceObjectAtIndex: anIndex withObject: aMutableShortcutItem];
[[UIApplication sharedApplication] setShortcutItems: updatedShortcutItems];
```
3. 获取ShortcutItem信息
如果程序通过 action 启动,那么可以在`- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions` 函数的    `launchOptions`里通过[UIApplicationLaunchOptionsShortcutItemKey] 取得item 信息。
如果程序在后台,触发了 action 事件,会调用`application:performActionForShortcutItem:completionHandler:`这个函数。

三、基于UITouch的 force 属性

UITouch现在有了 force属性,用于表示用户按压的力度,基于此可以实现不同力度的一些不同处理。

基于 UIGestureRecognizer 以及 UITouch 的 force 属性,可以很容易写出自定义的重按手势。

1.引入头文件
```
#import <UIKit/UIGestureRecognizerSubclass.h>
```
2.定义识别过程

主要是根据 touch 的几种状态,来设置手势的对应状态,重按的检测关键在于touch的力度大到是么程度可以认为识别成功。

3.其他手势阻断

重按如果和 tap 手势一起使用,如何让两个手势同时都能识别出来呢,这就需要设置阻断。我的 demo 中在手势失败之前,一直阻断了 tap 手势,否则 tap 会一直同时识别出来。最后与 tap 共存的状态,同 longPress 和 tap 共存的状态类似。

四、支持判断

在 6s 以下的机型中是不支持重按的,可以基于机型和系统版本来判断。还有一种情况就是用户在支持重按的机型上禁用了重按功能。设置-通用-辅助功能-3D Touch,此时就要通过UITraitCollection 的 forceTouchCapability 来判断。
监听这个值的变化,UIScreen, UIWindow, UIViewController, UIPresentationController, 以及 UIView 都实现了 UITraitEnvironment 协议。该协议有如下方法监听变化

- (void)traitCollectionDidChange:(nullable UITraitCollection *)previousTraitCollection

参考:
1.https://developer.apple.com/reference/uikit/uiapplicationshortcutitem
2.https://developer.apple.com/library/content/documentation/General/Reference/InfoPlistKeyReference/Articles/iPhoneOSKeys.html#//apple_ref/doc/uid/TP40009252