iOS-3DTouch学习二:Peek & Pop

2018-09-14  本文已影响26人  yuyangkk

1 概述

上篇文章《iOS-3DTouch学习一:添加主屏快速操作》我们一起了解了在主屏幕的3D Touch快速操作,本篇我们一起学一下在应用内使用3D Touch,对内容进行展示自定义预览和操作。
官方开发文档

2 场景

  1. 使用一定力度按压屏幕,触发Peek操作。
  2. 在Peek状态下,上划唤出Peek快速操作(UIPreviewAction)。
  3. Peek状态下,再次用更大力度按压屏幕,转场到预览的控制器。
    简易OC版本Demo地址官方swift版本地址

3 相关说明

4使用

4.1 注册

在遵循UIViewControllerPreviewingDelegate协议的控制器中调用registerForPreviewingWithDelegate: sourceView:

遵循协议

// 遵循协议
@interface MainViewController ()<UIViewControllerPreviewingDelegate>
@end

注册代理,并传入响应3D Touch的视图

// Peek & Pop Demo code
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(150, 200, 150, 40)];
// 开启用户交互
label.userInteractionEnabled = YES;
label.text = @"Peek & Pop";
label.textColor = [UIColor blueColor];
[self.view addSubview:label];

self.view.backgroundColor = [UIColor lightGrayColor];

// 注册代理,并传入响应3D Touch的视图
[self registerForPreviewingWithDelegate:self sourceView:label];
4.2 实现UIViewControllerPreviewingDelegate协议

可以通过设置previewingContext.sourceRect启用其他UI元素的模糊,区域内不被虚化,并在预览时放大动画。
如果不设置,将使用系统提供的合适区域。

当系统检测到3D Touch时,它会调用previewingContext:viewControllerForLocation代理方法,传递一个符合UIViewControllerPreviewing协议的previewingContext对象。使用此方法配置并返回一个视图控制器以进行预览。

- (UIViewController *)previewingContext:(id<UIViewControllerPreviewing>)previewingContext viewControllerForLocation:(CGPoint)location{
    
    PopedViewController *popVC = [[PopedViewController alloc] init];
    
    // 启用其他UI元素的模糊,区域内不被虚化,并在预览时放大动画。
    // 在每次调用之前,此rect将设置为sourceView的边界previewingContext:viewControllerForLocation:
    // 如果不设置,将使用系统提供的合适区域。
    previewingContext.sourceRect = CGRectMake(150, 200, 150, 40);
    // 如果返回的是nil,则不会执行预览(Peek)操作
    return popVC;
}

当系统察觉到足够的压力可以触发3D Touch 而Pop出ViewController时,会调用previewingContext:commitViewController:代理方法:

- (void)previewingContext:(id<UIViewControllerPreviewing>)previewingContext commitViewController:(UIViewController *)viewControllerToCommit{
    // 将配置好的控制器推入导航栈
    [self.navigationController pushViewController:viewControllerToCommit animated:YES];
}
4.3 添加Peek状态下,上划时的快速操作

如果我们想在预览的时候,想做一些操作,那我们可以在Peek状态下,添加一些Action或者Group,以提供快速操作。要添加快速操作,需要我们在控制器中覆写previewActionItems方法。

UIPreviewAction *firstAction = [UIPreviewAction actionWithTitle:@"选项一" style:UIPreviewActionStyleDefault handler:^(UIPreviewAction * _Nonnull action, UIViewController * _Nonnull previewViewController) {
   NSLog(@"选中了第一个action");
}];
    
UIPreviewAction *secondAction = [UIPreviewAction actionWithTitle:@"选项二" style:UIPreviewActionStyleSelected handler:^(UIPreviewAction * _Nonnull action, UIViewController * _Nonnull previewViewController) {
   NSLog(@"选中了第二个action");
}];
    
UIPreviewAction *thirdAction = [UIPreviewAction actionWithTitle:@"选项三" style:UIPreviewActionStyleDestructive handler:^(UIPreviewAction * _Nonnull action, UIViewController * _Nonnull previewViewController) {
   NSLog(@"选中了第三个action");
}];
    
// 你可以直接返回三个选项
// 可以返回不同的分组,当你点击分组时,会再次弹出分组中的actions
// 当然也可以同时返回
UIPreviewActionGroup *group = [UIPreviewActionGroup actionGroupWithTitle:@"group" style:UIPreviewActionStyleDefault actions:@[firstAction,secondAction]];
    
return @[group,thirdAction];

至此,简单的3D Touch使用我们就学习完了,希望能够帮助一些小伙伴。如果小伙伴觉得有帮助,记得给个小💗💗哦。

来张效果图


效果图.gif
上一篇 下一篇

猜你喜欢

热点阅读