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

代码下载链接: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的主要作用: 0.demo地址在最下面 1.Home Screen Quick Actions ...
    哆啦_阅读 2,311评论 6 5
  • 专著:http://www.jianshu.com/p/3443a3b27b2d 1.简单的介绍一下3D Touc...
    violafa阅读 1,041评论 1 0
  • 3DTouch目前在手机上有两种体现方式,一种是用力按下app的图片icon,会弹出选项菜单,就像电脑上的右键。一...
    莦婼姑娘阅读 467评论 2 3
  • 一、3D Touch 简介 1.什么是3D Touch? 3D Touch是一种立体触控技术,被苹果称为新一代多点...
    予独爱秋天的梅花阅读 811评论 0 0
  • 1.简单的介绍一下3D Touch 3D Touch的触控技术,被苹果称为新一代多点触控技术。其实,就是此前在Ap...
    Camille_chen阅读 12,104评论 19 33