美文网首页
iOS-3DTouch学习二:Peek & Pop

iOS-3DTouch学习二:Peek & Pop

作者: yuyangkk | 来源:发表于2018-09-14 17:45 被阅读26次

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

相关文章

  • iOS-3DTouch学习二:Peek & Pop

    1 概述 上篇文章《iOS-3DTouch学习一:添加主屏快速操作》我们一起了解了在主屏幕的3D Touch快速操...

  • 数据结构---数组栈

    interface push、pop、peek 实践---> ()[]{}、([)]

  • 栈、队列

    Stack先进后出,常用函数3P: peek push pop Queue先进先出,常用函数POP: peek o...

  • 3DTouch_Peek and Pop

    Peek 和 Pop,全新功能登场。 Peek 和 Pop 让你能够预览所有类型的内容,甚至可对内容进行操作,却不...

  • iOS9-3DTouch peek&&pop

    前言 对于3DTouch很多人都已经并不陌生了,因为iPhone 6s想必大家都已经体验过了,深度按压带来的体验仁...

  • 3D Touch实战演练

    摘要 iOS9提供了四类API(Home Screen Quick Action、UIKit Peek & Pop...

  • iOS 3D Touch

    前言 目录 这里只做讨论 UIKit peek and pop API首先直接上Demo:首先实现协议 UIVie...

  • iOS 3D Touch(二) peek and pop

    作为3D Touch的一部分,peek and pop (预览和弹出)个人觉得还是很有意思的,今天就说说如何去实现...

  • 构建栈对象:pop()方法: 获取栈顶元素,同时将元素移除,弹出栈 peek()方法: 获取栈顶元素

  • 基于栈结构的字符串反转

    构建栈对象:pop()方法: 获取栈顶元素,同时将元素移除,弹出栈 peek()方法: 获取栈顶元素

网友评论

      本文标题:iOS-3DTouch学习二:Peek & Pop

      本文链接:https://www.haomeiwen.com/subject/bamggftx.html