美文网首页UIKitiOS DeveloperiOS 开发
3DTouch(二) Peek和Pop功能实现

3DTouch(二) Peek和Pop功能实现

作者: 夜凉听风雨 | 来源:发表于2016-09-19 23:45 被阅读333次

代码下载链接:GitHub - jiangbin1993/3DTouch  欢迎下载点赞

上一篇文章介绍了3DTouch重按App的启动图标可以显示快捷功能(Quick Action)

链接:3DTouch的简单实现(一)重按启动图标出现选项 - 简书 

在App中,选中某个内容,可以使用peek(轻按)进行预览,使用pop(重按)切换控制器,显示完整内容。本篇文章介绍,如何使用Peek/Pop功能。


1.轻按虚化背景重点显示选中区域,接着加大按压力度预览下一页内容,继续加大按压力度,跳转到下一页,实现效果图如下:

代码实现:

首先遵循代理: UIViewControllerPreviewingDelegate

// 如果forcetouch可用,则注册,并且设置代理

if(self.traitCollection.forceTouchCapability == UIForceTouchCapabilityAvailable){

[self registerForPreviewingWithDelegate:self sourceView:self.tableView];

}

代理方法的实现

#pragma mark  UIViewControllerPreviewingDelegate代理方法

- (UIViewController *)previewingContext:(id)previewingContext viewControllerForLocation:(CGPoint)location {

NSIndexPath *indexpath = [_tableView indexPathForRowAtPoint:location];

UITableViewCell *cell = [_tableView cellForRowAtIndexPath:indexpath];

if (!cell) {

return nil;

}

//pre-peek状态下,重点显示的区域

previewingContext.sourceRect = cell.frame;

SecondViewController *vc = [[SecondViewController alloc] init];

vc.labelText = [NSString stringWithFormat:@"%lu",indexpath.row];

//peek状态下,显示的view大小

vc.preferredContentSize = CGSizeMake(0, 0);

return vc;

}

2.轻按虚化背景重点显示选中区域,接着加大按压力度预览下一页内容,手指按压选中区域向上拖动出现previewAction,点击previewAction选项进行相应操作。实现效果如下:


代码实现:

在要跳转到的界面中写 我建的是SecondViewController 下面是SecondViewController.m文件

#import "SecondViewController.h"

@interface SecondViewController ()

//新增一个数组属性,来存储PreviewAction;

@property (nonatomic,strong) NSArray *arrayPreviewActions;

@end

@implementation SecondViewController

//previewActionItems方法中返回存储PreviewAction的数组;

-(NSArray> *)previewActionItems

{

return self.arrayPreviewActions;

}

- (void)viewDidLoad {

[super viewDidLoad];

self.view.backgroundColor = [UIColor redColor];

UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(100, 100, 100, 30)];

label.text = self.labelText;

[self.view addSubview:label];

//设置previewActions

self.arrayPreviewActions = @[[UIPreviewAction actionWithTitle:@"OK" style:UIPreviewActionStyleDefault handler:^(UIPreviewAction * _Nonnull action, UIViewController *_Nonnull previewViewController) {

NSLog(@"Press OK");

}],

[UIPreviewAction actionWithTitle:@"Cancel" style:UIPreviewActionStyleDestructive handler:^(UIPreviewAction * _Nonnull action, UIViewController * _Nonnull previewViewController) {

NSLog(@"Press Cancel");

}],

];

}

@end

如果这篇文章对你有一丢丢的帮助 请帮我点个喜欢哦。

相关文章

  • 3DTouch(二) Peek和Pop功能实现

    代码下载链接:GitHub - jiangbin1993/3DTouch 欢迎下载点赞 上一篇文章介绍了3DTou...

  • 3DTouch Peek和Pop功能

    在我们的app中使用3D Touch功能,主要分为以下三个模块: Home Screen Quick Action...

  • iOS 3DTouch

    3DTouch是苹果在2015年,iPhone 6s、iOS9之后新推出的新功能。有Peek和Pop两种新手势。有...

  • iOS 3DTouch

    一.什么是3DTouch? 效果图: 点击icon: Peek(预览)和Pop(跳至预览的详细界面): 看完这个,...

  • 3DTouch_Peek and Pop

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

  • 3DTouch中Peek与Pop

    效果如下:

  • iOS9 3D Touch 使用第二篇

    前一篇文章介绍了给图片添加快捷方式,这篇主要介绍应用内,3D Touch的peek和pop功能的实现 当前的Vie...

  • iOS 3DTouch

    3D Touch,苹果iPhone 6s的新功能 有Peek Pop 两种新手势 实现点击app出现小弹框界面的方...

  • 数据结构---数组栈

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

  • 栈、队列

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

网友评论

  • L了个Y:基本上入了门只是previewingContext还是找不到sourceRect 这个属性
  • L了个Y:给个demo吧大兄弟
    夜凉听风雨:@文刀洋 代码链接已经贴上了
  • L了个Y:previewingContext.sourceRect = cell.frame;这句报错

本文标题:3DTouch(二) Peek和Pop功能实现

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