iOS9新特性の3D-touch-下篇

再看一下如何在应用内集成3Dtouch.
3D-touch操作分为轻按peek跟重按pop,根据apple官方设计规范,一般来说peek呼出预览视图,我在storyboard里面创建一个PreviewController用于描述呼出的预览视图,同时创建了一个DetailViewController用于显示最终显示的视图,同时我们应该注意,pop之后的视图应该与用户正常点击进去之后没有任何区别.
此时我们应用层级为

tableView---(peek)---->previewController---(pop)--->DetailviewController

无论何时,我们在使用3D-touch的时候应当先检查3D-touch的可用性

- (void)check3Dtouch
{
    //检测3Dtouch是否可用,若可用注册预览视图代理
    if (self.traitCollection.forceTouchCapability == UIForceTouchCapabilityAvailable) {
        [self registerForPreviewingWithDelegate:self sourceView:self.view];
    }
}

我们在viewWillAppear里面和用户进行3D-touch相关设置的时候都要检查可用性

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    [self check3Dtouch];
}

///用户可能进入设置开启/关闭3Dtouch的时候,在这里需要再次检测3Dtouch可用性
- (void)traitCollectionDidChange:(UITraitCollection *)previousTraitCollection
{
    [self check3Dtouch];
}

别忘了设置UIViewControllerPreviewingDelegate预览视图代理.

接下来只需要在对应的代理方法里面返回相应的控制器即可


- (UIViewController *)previewingContext:(id)previewingContext viewControllerForLocation:(CGPoint)location{
    
    if ([self.presentedViewController isKindOfClass:[PreviewViewController class]]) {return nil;}
    
    UIStoryboard *sb = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
    PreviewViewController *previewController = [sb instantiateViewControllerWithIdentifier:@"preview"];
    
    location = CGPointMake(10, 100);
    return previewController;
}

- (void)previewingContext:(id)previewingContext commitViewController:(UIViewController *)viewControllerToCommit{
    
    UIStoryboard *sb = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
    DetailViewController *detailView = [sb instantiateViewControllerWithIdentifier:@"detailView"];
    
    [self showViewController:detailView sender:self];
    
}

此时peekpop手势都已经集成完毕,这时候还想实现类似在preview下面添加alert的操作,如系统message里面的快捷回复

iOS9新特性の3D-touch-下篇_第1张图片
8.png

其实只需要在previewController里面实现 - (NSArray> *)previewActionItems方法即可.
PS:在xcode6.3时OC添加了制定数组元组类型的功能,有利于写出更加严谨的代码,好评~

- (NSArray> *)previewActionItems
{
    UIPreviewAction *action1 = [UIPreviewAction actionWithTitle:@"action1" style:UIPreviewActionStyleDefault handler:^(UIPreviewAction * _Nonnull action, UIViewController * _Nonnull previewViewController) {
        NSLog(@"action1 click");
    }];
    UIPreviewAction *action2 = [UIPreviewAction actionWithTitle:@"action2" style:UIPreviewActionStyleDestructive handler:^(UIPreviewAction * _Nonnull action, UIViewController * _Nonnull previewViewController) {
        NSLog(@"action2 click");
    }];
    UIPreviewAction *action3 = [UIPreviewAction actionWithTitle:@"action3" style:UIPreviewActionStyleSelected handler:^(UIPreviewAction * _Nonnull action, UIViewController * _Nonnull previewViewController) {
        NSLog(@"action3 click");
    }];
    
    
    UIPreviewActionGroup *group1 = [UIPreviewActionGroup actionGroupWithTitle:@"group1" style:UIPreviewActionStyleDefault actions:@[action1,action2]];
    UIPreviewActionGroup *group2 = [UIPreviewActionGroup actionGroupWithTitle:@"group2" style:UIPreviewActionStyleDestructive actions:@[action1,action3]];
    UIPreviewActionGroup *group3 = [UIPreviewActionGroup actionGroupWithTitle:@"group3" style:UIPreviewActionStyleSelected actions:@[action2,action3]];
    
//    NSArray *items = @[action1,action2,action3];
//    NSArray *items = @[group1,group2,group3];
    NSArray *items = @[action1,group1,action2,group2,action3,group3];
    return items;
}

有兴趣的话可以自己动手体会一下UIPreviewActionGroupUIPreviewAction的区别.

你可以在github下载到此源码

你可能感兴趣的:(iOS9新特性の3D-touch-下篇)