3D-touch

苹果在6s 中引入的心的功能.虽然有点鸡肋的感觉.但还是要看一下.

1. Home Screen Quick Actions

如图中所示,当按压图标时出现菜单列表


3D-touch_第1张图片

1). 在启动应用*- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions *后我们添加需要弹出的列表项

- (void)creatShortcutItems {
    // 创建 icon
    UIApplicationShortcutIcon *icon1 = [UIApplicationShortcutIcon iconWithTemplateImageName:@"money"];
    UIApplicationShortcutIcon *icon2 = [UIApplicationShortcutIcon iconWithTemplateImageName:@"gouwuche"];
    
    UIMutableApplicationShortcutItem *item1 = [[UIMutableApplicationShortcutItem alloc] initWithType:@"m" localizedTitle:@"钱包" localizedSubtitle:@"这里是钱包" icon: icon1 userInfo:nil];
    UIMutableApplicationShortcutItem *item2 = [[UIMutableApplicationShortcutItem alloc] initWithType:@"g" localizedTitle:@"购物车" localizedSubtitle:@"这里是购物车" icon: icon2 userInfo:nil];
    
    [UIApplication sharedApplication].shortcutItems = @[item1, item2];
}

注意:最多只能添加4项,至于图标在左或者在右显示,取决于 app 的位置.

2)实现代理方法处理3d-touch事件

// 处理3dtouch 事件
- (void)application:(UIApplication *)application performActionForShortcutItem:(UIApplicationShortcutItem *)shortcutItem completionHandler:(void (^)(BOOL))completionHandler {
    NSLog(@"%@", shortcutItem);
    if ([shortcutItem.type isEqualToString:@"m"]) {
    /*
        if ([application.keyWindow.rootViewController isKindOfClass:[DZTestTouchViewController class]]) {
            [self.window.rootViewController dismissViewControllerAnimated:YES completion:nil];
        }
    */
        DZTestTouchViewController *vc = [[DZTestTouchViewController alloc] init];
        [self.window.rootViewController presentViewController:vc animated:YES completion:^{
            NSLog(@"推出了钱包");
        }];
    } else {
        NSLog(@"推出了购物车");
    }
}

Peek and Pop

根据官方文档说明,用户使用3d touch 进行交互时,有三个阶段
(1)提示用户有交互


3D-touch_第2张图片

(2)继续按压,会出现预览视图

3D-touch_第3张图片

(3)通过视图上的交互控件交互

3D-touch_第4张图片

(1)注册3d touch

// 界面将要发生变化的时候调用
- (void)traitCollectionDidChange:(UITraitCollection *)previousTraitCollection
{
    [self check3DTouch];
}

- (void)check3DTouch {
    // 判断是否支持3dtouch
    if (self.traitCollection.forceTouchCapability == UIForceTouchCapabilityAvailable) {
        // 注册代理
        // 如果是使用的 imageView, 一定要记得先设置可以交互.
        [self registerForPreviewingWithDelegate:self sourceView:self.touchBtn];
    }
}

(2)实现代理方法

// 返回一个预览的 VC
- (UIViewController *)previewingContext:(id)previewingContext viewControllerForLocation:(CGPoint)location {
    if ([self.presentedViewController isKindOfClass:[DZTestPeekViewController class]]) {
        return nil;
    }    
    DZTestPeekViewController *vc = [[DZTestPeekViewController alloc] init];
    return vc;
}

// 最终展示的 vc, 当出现预览的 Vc 后继续按压展示的 VC, 可以和 预览的 VC 用一个控制器
- (void)previewingContext:(id)previewingContext commitViewController:(UIViewController *)viewControllerToCommit {
    DZTestPopViewController *vc = [[DZTestPopViewController alloc] init];
    [self showViewController:vc sender:self];
}

(3)预览 vc 的交互事件

- (NSArray> *)previewActionItems {
    UIPreviewAction *action1 = [UIPreviewAction actionWithTitle:@"action1" style:UIPreviewActionStyleDefault handler:^(UIPreviewAction * _Nonnull action, UIViewController * _Nonnull previewViewController) {
        NSLog(@"点击事件1");
    }];
    
    UIPreviewAction *action2 = [UIPreviewAction actionWithTitle:@"action2" style:UIPreviewActionStyleDefault handler:^(UIPreviewAction * _Nonnull action, UIViewController * _Nonnull previewViewController) {
        NSLog(@"点击事件2");
    }];

// 当然事件多的话也能添加分组
//    UIPreviewActionGroup *group = [UIPreviewActionGroup actionGroupWithTitle:@"分组一" style:UIPreviewActionStyleDefault actions:@[action1, action2]];
//    return @[group];
    return @[action1, action2];
}

你可能感兴趣的:(3D-touch)