伴着6S的发布,iOS 9.0开始支持3D Touch功能。使用场景来分一共有三种状况。html
``` - (id <UIViewControllerPreviewing>)registerForPreviewingWithDelegate:(id<UIViewControllerPreviewingDelegate>)delegate sourceView:(UIView *)sourceView NS_AVAILABLE_IOS(9_0); - (void)unregisterForPreviewingWithContext:(id <UIViewControllerPreviewing>)previewing NS_AVAILABLE_IOS(9_0); ```
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 { } ```
``` // 添加一些 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,暴露的组]; } ```
在 Info.plist 中 添加 `UIApplicationShortcutItems ` 字段,这个字段下面的每一个字典结构定义了一个 Item,key 以下: ``` `UIApplicationShortcutItemType (required)` 通常取值和 bundle id 一致,用来判断快速操做类型。 `UIApplicationShortcutItemTitle (required)` 标题 `UIApplicationShortcutItemSubtitle` 副标题,显示在标题下面 `UIApplicationShortcutItemIconType` 图标 `UIApplicationShortcutItemIconFile` 自定义图标文件,会覆盖上面的icon type `UIApplicationShortcutItemUserInfo` 自定义信息 ``` 静态添加的没法修改,而且优先显示。
也就是建立自定义的 `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]; ```
若是程序经过 action 启动,那么能够在`- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions` 函数的 `launchOptions`里经过[UIApplicationLaunchOptionsShortcutItemKey] 取得item 信息。 若是程序在后台,触发了 action 事件,会调用`application:performActionForShortcutItem:completionHandler:`这个函数。
UITouch如今有了 force属性,用于表示用户按压的力度,基于此能够实现不一样力度的一些不一样处理。ios
基于 UIGestureRecognizer
以及 UITouch 的 force 属性,能够很容易写出自定义的重按手势。git
``` #import <UIKit/UIGestureRecognizerSubclass.h> ```
主要是根据 touch 的几种状态,来设置手势的对应状态,重按的检测关键在于touch的力度大到是么程度能够认为识别成功。github
重按若是和 tap 手势一块儿使用,如何让两个手势同时都能识别出来呢,这就须要设置阻断。个人 demo 中在手势失败以前,一直阻断了 tap 手势,不然 tap 会一直同时识别出来。最后与 tap 共存的状态,同 longPress 和 tap 共存的状态相似。xcode
在 6s 如下的机型中是不支持重按的,能够基于机型和系统版原本判断。还有一种状况就是用户在支持重按的机型上禁用了重按功能。设置-通用-辅助功能-3D Touch
,此时就要经过UITraitCollection 的 forceTouchCapability 来判断。
监听这个值的变化,UIScreen, UIWindow, UIViewController, UIPresentationController, 以及 UIView 都实现了 UITraitEnvironment
协议。该协议有以下方法监听变化app
- (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/TP40009252iview