iOS开发之3D Touch功能实现

3D Touch是一种立体触控技术,被苹果称为新一代多点触控技术,是在Apple Watch上采用的Force Touch,屏幕可感应不同的感压力度触控。3D Touch,有Peek Pop 两种新手势。

  • 主页的 quick action 可以让你的app 直接进入相关行为。
  • Peek and Pop 允许你快速预览内容,并导航到对应界面。
  • UIPreviewInteraction 让你可以更精细地控制 3D Touch。

话不多说,马上开始

1.添加主屏幕快捷菜单 ( Home screen quick action)
(1)静态添加快捷菜单

创建一个静态的快捷菜单,只需要简单地在Info.plist中添加一个 UIApplicationShortcutItems 的 Array 即可。

iOS开发之3D Touch功能实现_第1张图片
UIApplicationShortcutItems

关键字释义:

UIApplicationShortcutItemType: 快捷可选项的特定字符串(必填)
UIApplicationShortcutItemTitle: 快捷可选项的标题(必填)
UIApplicationShortcutItemSubtitle: 快捷可选项的子标题(可选)
UIApplicationShortcutItemIconType: 快捷可选项的图标(可选)
UIApplicationShortcutItemIconFile: 快捷可选项的自定义图标(可选)
UIApplicationShortcutItemUserInfo: 快捷可选项的附加信息(可选)

(2)动态添加快捷菜单

初始化并配置 UIApplicationShortcutItem, UIMutableApplicationShortcutItem 和 UIApplicationShortcutIcon 这三个类,并将其添加到 AppDelegate 中的 shortcutItems 属性即可。

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    UIApplicationShortcutIcon *icon1 = [UIApplicationShortcutIcon iconWithType:UIApplicationShortcutIconTypeAdd];
    UIApplicationShortcutIcon *icon2 = [UIApplicationShortcutIcon iconWithType:UIApplicationShortcutIconTypeShare];
    UIApplicationShortcutItem *item1 = [[UIApplicationShortcutItem alloc]initWithType:@"item1" localizedTitle:@"标题1" localizedSubtitle:nil icon:icon1 userInfo:nil];
    UIApplicationShortcutItem *item2 = [[UIApplicationShortcutItem alloc]initWithType:@"item2" localizedTitle:@"标题2" localizedSubtitle:nil icon:icon2 userInfo:nil];
    NSArray *array = @[item1,item2];
    [UIApplication sharedApplication].shortcutItems = array;
    return YES;
}
2.检测设备是否支持3D Touch

在ViewController.m文件的viewDidLoad中判断3D Touch是否可用,防止设备不支持3D Touch功能时崩溃

-(void)check3dtouch
{
    if(self.traitCollection.forceTouchCapability == UIForceTouchCapabilityAvailable)
    {
        NSLog(@"3DTouch 可用");
    }else{
        NSLog(@"3DTouch 不可用");
    }
}
3.实现快捷选项功能
-(void)application:(UIApplication *)application performActionForShortcutItem:(UIApplicationShortcutItem *)shortcutItem completionHandler:(void (^)(BOOL))completionHandler
{
    if ([shortcutItem.localizedTitle isEqualToString:@"标题1"]) {
        UIAlertView * alert = [[UIAlertView alloc]initWithTitle:@"测试1" message:@"我只是测试" delegate:nil cancelButtonTitle:@"我知道是测试" otherButtonTitles:@"好", nil];
        [alert show];
    }
    else if([shortcutItem.type isEqualToString:@"标题2"]){
        
        //...
        
    }
    else if (completionHandler) {
        completionHandler(YES);
    }
}
4.Peek&Pop

Peek and Pop 将传统的 Push 操作分为了两步,当你的手指按压某行列表,背景开始模式模糊,然后出现一个预览界面,然后继续增加压力,伴随着俏皮的弹性动画,下一个界面呈现在你眼前。在 API 中,这两部分别被称为 Preview 和 Commit。

  • 让需要预览的 ViewController 遵循 UIViewControllerPreviewingDelegate 协议
  • 调用 registerForPreviewingWithDelegate: sourceView: 注册该ViewController
[self registerForPreviewingWithDelegate:self sourceView:self.view];
  • 在preview 代理方法中提供一个预览的ViewController,并设置好 context 的 sourceRect.
  • 在 commit 代理方法中,直接调用 showViewController(_:sender:) 即可。
//BJBaseViewController
//UIViewControllerPreviewingDelegate
//添加peek功能
-(UIViewController *)previewingContext:(id)context viewControllerForLocation:(CGPoint) point
{
    BJOneViewController *childVC = [[BJOneViewController alloc] initWithTitle:@"Peek&Pop"];
    childVC.preferredContentSize = CGSizeMake(0.0f,300.f);
    NSLog(@"%@",childVC);
    return childVC;
}
//实现pop功能
-(void)previewingContext:(id )previewingContext commitViewController:(UIViewController *)viewControllerToCommit
{
    [self showViewController:viewControllerToCommit sender:self];
}
//BJOneViewController
//添加预览界面的ActionSheet
-(NSArray> *)previewActionItems
{
    UIPreviewAction * action1 = [UIPreviewAction actionWithTitle:@"选项1" style:0 handler:^(UIPreviewAction * _Nonnull action, UIViewController * _Nonnull previewViewController) {
        NSLog(@"click1");
    }];
    UIPreviewAction * action2 = [UIPreviewAction actionWithTitle:@"选项2" style:1 handler:^(UIPreviewAction * _Nonnull action, UIViewController * _Nonnull previewViewController) {
        NSLog(@"click2");
    }];
    UIPreviewAction * action3 = [UIPreviewAction actionWithTitle:@"选项3" style:2 handler:^(UIPreviewAction * _Nonnull action, UIViewController * _Nonnull previewViewController) {
        NSLog(@"click3");
    }];
    NSArray * actions = @[action1,action2,action3];
    return actions;
}

你可能感兴趣的:(iOS开发之3D Touch功能实现)