【iOS】3DTouch初步学习

With iOS 9, new iPhone models add a third dimension to the user interface.

  • A user can now press your Home screen icon to immediately access functionality provided by your app.
  • Within your app, a user can now press views to see previews of additional content and gain accelerated access to features.

模拟器实现3D Touch功能

1、插件安装

插件下载地址:
https://github.com/DeskConnect/SBShortcutMenuSimulator

  • 下载插件到本地(在terminal操作):
git clone https://github.com/DeskConnect/SBShortcutMenuSimulator.git
【iOS】3DTouch初步学习_第1张图片
插件下载完成
  • 安装插件:
cd SBShortcutMenuSimulator
 make  
插件安装完成

2、插件使用

  • 启动插件
xcrun simctl spawn booted launchctl debug system/com.apple.SpringBoard --environment DYLD_INSERT_LIBRARIES=$PWD/SBShortcutMenuSimulator.dylib
xcrun simctl spawn booted launchctl stop com.apple.SpringBoard

注意:如果运行过程中出现:No devices are booted. 是因为你的模拟器没有启动,这时候启动你的模拟器即可。

  • 预览效果
echo 'com.apple.mobilecal' | nc 127.0.0.1 8000
【iOS】3DTouch初步学习_第2张图片
3D Touch效果

注意:
1、'com.apple.mobilecal'是应用的Bundle ID,如果你要测试自己的应用的3D Touch效果,将该字段换成自己应用的Bundle ID。上面的示例应用是系统日历。
2、如果出现SpringBorad意外退出,可能是你的模拟器高于iOS9.1,可以尝试下载iOS9.1或iOS9.0的模拟器然后重新以上“插件使用”操作。

检测是否支持3D Touch

  • 在UIViewController生命周期的viewWillAppear中做判断:
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    if (self.traitCollection.forceTouchCapability == UIForceTouchCapabilityAvailable) {
        //3D Touch可用
    }
    else{
        //3D Touch不可用
    }
}

UIForceTouchCapability是个枚举

@property (nonatomic, readonly) UIForceTouchCapability forceTouchCapability NS_AVAILABLE_IOS(9_0);
typedef NS_ENUM(NSInteger, UIForceTouchCapability) {
    UIForceTouchCapabilityUnknown = 0,        //3D Touch检测失败
    UIForceTouchCapabilityUnavailable = 1,    //3D Touch不可用
    UIForceTouchCapabilityAvailable = 2       //3D Touch可用
};
  • 也可以通过以下方法实现判断,当界面环境发生改变时会调用该方法:
-(void)traitCollectionDidChange:(UITraitCollection *)previousTraitCollection {
    if (previousTraitCollection.forceTouchCapability == UIForceTouchCapabilityAvailable) {
        NSLog(@"3D Touch可用");
    }
    else{
        NSLog(@"3D Touch不可用");
    }
}

3D Touch开发

iOS9提供了四类API(Home Screen Quick Action、UIKit Peek & Pop 、WebView Peek & Pop 和UITouch Force Properties)用于操作3D Touch(Pressure Sensitivity 、 Peek and Pop 和 Quick Actions)。

1、Home Screen Quick Actions

用力按压主屏幕的应用Icon,可以通过3D Touch呼出一个快捷菜单,点击快速进入相关功能模块。


【iOS】3DTouch初步学习_第3张图片
Map Home Screen Quick Actions
【iOS】3DTouch初步学习_第4张图片

其中系统自带的图片有以下类型:

typedef NS_ENUM(NSInteger, UIApplicationShortcutIconType) {
    UIApplicationShortcutIconTypeCompose,
    UIApplicationShortcutIconTypePlay,
    UIApplicationShortcutIconTypePause,
    UIApplicationShortcutIconTypeAdd,
    UIApplicationShortcutIconTypeLocation,
    UIApplicationShortcutIconTypeSearch,
    UIApplicationShortcutIconTypeShare,
    UIApplicationShortcutIconTypeProhibit       NS_ENUM_AVAILABLE_IOS(9_1),
    UIApplicationShortcutIconTypeContact        NS_ENUM_AVAILABLE_IOS(9_1),
    UIApplicationShortcutIconTypeHome           NS_ENUM_AVAILABLE_IOS(9_1),
    UIApplicationShortcutIconTypeMarkLocation   NS_ENUM_AVAILABLE_IOS(9_1),
    UIApplicationShortcutIconTypeFavorite       NS_ENUM_AVAILABLE_IOS(9_1),
    UIApplicationShortcutIconTypeLove           NS_ENUM_AVAILABLE_IOS(9_1),
    UIApplicationShortcutIconTypeCloud          NS_ENUM_AVAILABLE_IOS(9_1),
    UIApplicationShortcutIconTypeInvitation     NS_ENUM_AVAILABLE_IOS(9_1),
    UIApplicationShortcutIconTypeConfirmation   NS_ENUM_AVAILABLE_IOS(9_1),
    UIApplicationShortcutIconTypeMail           NS_ENUM_AVAILABLE_IOS(9_1),
    UIApplicationShortcutIconTypeMessage        NS_ENUM_AVAILABLE_IOS(9_1),
    UIApplicationShortcutIconTypeDate           NS_ENUM_AVAILABLE_IOS(9_1),
    UIApplicationShortcutIconTypeTime           NS_ENUM_AVAILABLE_IOS(9_1),
    UIApplicationShortcutIconTypeCapturePhoto   NS_ENUM_AVAILABLE_IOS(9_1),
    UIApplicationShortcutIconTypeCaptureVideo   NS_ENUM_AVAILABLE_IOS(9_1),
    UIApplicationShortcutIconTypeTask           NS_ENUM_AVAILABLE_IOS(9_1),
    UIApplicationShortcutIconTypeTaskCompleted  NS_ENUM_AVAILABLE_IOS(9_1),
    UIApplicationShortcutIconTypeAlarm          NS_ENUM_AVAILABLE_IOS(9_1),
    UIApplicationShortcutIconTypeBookmark       NS_ENUM_AVAILABLE_IOS(9_1),
    UIApplicationShortcutIconTypeShuffle        NS_ENUM_AVAILABLE_IOS(9_1),
    UIApplicationShortcutIconTypeAudio          NS_ENUM_AVAILABLE_IOS(9_1),
    UIApplicationShortcutIconTypeUpdate         NS_ENUM_AVAILABLE_IOS(9_1)
} NS_ENUM_AVAILABLE_IOS(9_0) __TVOS_PROHIBITED;
创建Quick Action有两种方式:静态和动态
  • 静态创建
    静态创建只需在Info.plist上添加相关功能设置:


    【iOS】3DTouch初步学习_第5张图片

    也可以右击Info.plist -> Open As -> Source Code,输入:

UIApplicationShortcutItems
    
        
            UIApplicationShortcutItemIconType
            UIApplicationShortcutIconTypeSearch
            UIApplicationShortcutItemSubtitle
            搜索好友
            UIApplicationShortcutItemTitle
            搜索
            UIApplicationShortcutItemType
            1
            UIApplicationShortcutItemUserInfo
            
        
        
            UIApplicationShortcutItemIconType
            UIApplicationShortcutIconTypeAdd
            UIApplicationShortcutItemSubtitle
            添加好友
            UIApplicationShortcutItemTitle
            添加
            UIApplicationShortcutItemType
            2
            UIApplicationShortcutItemUserInfo
            
        
    
  • 动态创建
    代码创建3D Touch的item:
    UIApplicationShortcutItem *item1 = [[UIApplicationShortcutItem alloc] initWithType:@"3" localizedTitle:@"扫一扫"];
    UIApplicationShortcutIcon *icon = [UIApplicationShortcutIcon iconWithTemplateImageName:@"3DSearch"];
    UIApplicationShortcutItem *item2 = [[UIApplicationShortcutItem alloc] initWithType:@"4" localizedTitle:@"设置" localizedSubtitle:nil icon:icon userInfo:nil];
    [UIApplication sharedApplication].shortcutItems = @[item1,item2];

创建方法:

-(instancetype)initWithType:(NSString *)type localizedTitle:(NSString *)localizedTitle localizedSubtitle:(nullable NSString *)localizedSubtitle icon:(nullable UIApplicationShortcutIcon *)icon userInfo:(nullable NSDictionary *)userInfo NS_DESIGNATED_INITIALIZER;
-(instancetype)initWithType:(NSString *)type localizedTitle:(NSString *)localizedTitle;

设置图片方法:

+(instancetype)iconWithType:(UIApplicationShortcutIconType)type;
+(instancetype)iconWithTemplateImageName:(NSString *)templateImageName;。
【iOS】3DTouch初步学习_第6张图片

【iOS】3DTouch初步学习_第7张图片
动态+静态 创建

注意:
(1)系统限制每个App最多能够显示4个Action Item,其中包括静态方式和动态方式进行创建的,超过个数不显示。
(2)如果静态和动态方式同时使用的时候,给UIApplication的shortcutItems赋值的时候不会覆盖静态创建的items。
(3)动态创建的方式只有在程序第一次启动之后才会显示。
(4)如果你要显示系统图片时,info.plist中不要添加
UIApplicationShortcutItemIconFile属性,因为当
UIApplicationShortcutItemIconFile和UIApplicationShortcutItemIconType同时存在时,会优先使用UIApplicationShortcutItemIconFile设置的图片。
(5)Quick Actions显示的icon在左边或者右边,这个是跟你的app 放在你手机的位置有关系,这个iOS会自动处理掉。

点击Home Screen Quick Actions的相应回调
  • iOS9新增以下方法实现点击 Home Screen Quick Action的相应回调,我们可以通过shortcutItem的type或者localizedTitle属性(因为这两个属性时必须设置的)判断点击了哪个item:
-(void)application:(UIApplication *)application performActionForShortcutItem:(UIApplicationShortcutItem *)shortcutItem completionHandler:(void(^)(BOOL succeeded))completionHandler {
    NSInteger index = shortcutItem.type.integerValue;
    switch (index) {
        case 1:
            NSLog(@"搜索");
            break;
        case 2:
            NSLog(@"添加");
            break;
        case 3:
            NSLog(@"扫一扫");
            break;
        case 4:
            NSLog(@"设置");
            break;
        default:
            break;
    }
}
  • 还可以通过入口方法里面进行判断(该方法只会在程序从未启动到启动过程中才会被调用):
-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    
    if (launchOptions) {
        UIApplicationShortcutItem *item = [launchOptions valueForKey:UIApplicationLaunchOptionsShortcutItemKey];
        NSInteger index = item.type.integerValue;
        switch (index) {
            case 1:
                NSLog(@"搜索");
                break;
            case 2:
                NSLog(@"添加");
                break;
            case 3:
                NSLog(@"扫一扫");
                break;
            case 4:
                NSLog(@"设置");
                break;
            default:
                break;
        }
    }
    
    return YES;
}

2、Peek & Pop

A peek :
Appears while a user presses on an item that supports peek and disappears when the user’s finger lifts
Opens a detailed view of the item—called a pop—when users press a little deeper on the peek view
Can provide quick actions related to the item when users swipe up within the peek view

【iOS】3DTouch初步学习_第8张图片
A peek preview in Safari
【iOS】3DTouch初步学习_第9张图片
Quick actions in a Safari peek
UIKit Peek & Pop
  • 给ViewController上的view注册3D Touch(先判断设备是否支持3D Touch)
if (self.traitCollection.forceTouchCapability == UIForceTouchCapabilityAvailable)
    {
        [self registerForPreviewingWithDelegate:(id)self sourceView:self.view];
        
    }
  • 新建一个新的UIViewController名为:ContentViewController,以便之后peek时显示该ContentViewController,在ContentViewController中重写以下方法实现Quick actions:
-(NSArray> *)previewActionItems{
    UIPreviewAction *action1 = [UIPreviewAction actionWithTitle:@"action1" style:UIPreviewActionStyleDefault handler:^(UIPreviewAction * _Nonnull action, UIViewController * _Nonnull previewViewController) {
        NSLog(@"action1");
    }];
    
    UIPreviewAction *action2 = [UIPreviewAction actionWithTitle:@"action2" style:UIPreviewActionStyleSelected handler:^(UIPreviewAction * _Nonnull action, UIViewController * _Nonnull previewViewController) {
        NSLog(@"action2");
    }];
    NSArray *actions = @[action1,action2];
    UIPreviewActionGroup *group1 = [UIPreviewActionGroup actionGroupWithTitle:@"Action Group" style:UIPreviewActionStyleDefault actions:actions];
    NSArray *array = @[group1];
    return array;
}

注意:
1、该方法不是必须要重写,根据自己的需求而定。
2、这里是把两个action放在了一个组里显示,也可以直接显示两个action。

  • ViewController遵循UIViewControllerPreviewingDelegate协议:
//UIViewControllerPreviewingDelegate
//稍微重按调用该方法:(peek)
-(UIViewController *)previewingContext:(id)previewingContext viewControllerForLocation:(CGPoint)location{
    //防止重复加入
    if ([self.presentedViewController isKindOfClass:[ContentViewController class]]){
        return nil;
    }
    else {
        ContentViewController *contentVC = [[ContentViewController alloc] init];
        return contentVC;
    }
}
//加重按压调用该方法:(pop)
-(void)previewingContext:(id)previewingContext commitViewController:(UIViewController *)viewControllerToCommit{
    //跳转到某个ViewController
    ContentViewController *contentVC = [[ContentViewController alloc] init];
[self.navigationController pushViewController : contentVC animated:YES];
}
  • 效果图:


    【iOS】3DTouch初步学习_第10张图片
    peek

    【iOS】3DTouch初步学习_第11张图片
    Quick actions

    【iOS】3DTouch初步学习_第12张图片
    点击Action Group

    【iOS】3DTouch初步学习_第13张图片
    pop
WebKit Peek & Pop

WebKit Peek & Pop 的操作和UIKit差不多,只是显示的是Web网页。
显示网页有三种方法:
1、openUrl离开应用进入safari打开网页
2、UIWebView或者WKWebView自定义视图在应用内打开网页
3、在iOS9.0后新增SFSafariViewController类,用于显示web网页。并通过SFSafariViewControllerDelegate的以下方法实现回到应用操作。

-(void)safariViewControllerDidFinish:(SFSafariViewController *)controller{
    [controller dismissViewControllerAnimated:YES completion:nil];
}

注意:
1、方法1不能实现3D Touch。
2、使用方法2实现3D Touch时,要设置属性 allowsLinkPreview 为Yes(默认是NO)。

参考文档

http://pingguohe.net/2015/10/12/3D-Touch-2.html
http://www.devzeng.com/blog/ios9-3d-touch.html

你可能感兴趣的:(【iOS】3DTouch初步学习)