iOS-3DTouch学习二:Peek & Pop

1 概述

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

2 场景

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

3 相关说明

  • UIViewControllerPreviewingDelegate:触发3D Touch的类,需要遵循该协议,并要实现协议中的方法。
  • UIPreviewAction:Peek状态下,上划唤出快速操作选项实例。
  • previewActionItems:Peek状态下,上划唤出快速操作选项实例数组。在要被Pop出的控制器中覆写get方法,返回一个UIPreviewAction数组。

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
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 1、通过CocoaPods安装项目名称项目信息 AFNetworking网络请求组件 FMDB本地数据库组件 SD...
    阳明AI阅读 16,074评论 3 119
  • 3D Touch简介 2015年,苹果发布了iOS9以及iphone6s/iphone6s Plus,其中最具有创...
    简简蜗牛阅读 3,841评论 0 0
  • 前言 关于这篇文章 由于iPhone 6S发布不到一年的时间,很多新特性、新技术还未普遍,不管是3D Touch的...
    Tangentw阅读 10,090评论 8 18
  • 文/风言锋语 亦舒说:要么给我很多很多爱,要么就给我很多很多钱。如果两者都没有,那就请给我健康吧! 唐晶在跟罗子君...
    媛姐说职场阅读 7,022评论 8 13
  • 黑色的夜幕挂起来 几颗星贴着 一颗透明的心 直到蓝湖的水底 随风的节奏 呼吸 行走在路上 心已经飘到很远的远方 那...
    蓝色海马阅读 1,576评论 0 1

友情链接更多精彩内容