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
如果这篇文章对你有一丢丢的帮助 请帮我点个喜欢哦。