2.在App中,选中某个内容,可以使用peek(轻按)进行预览或者使用pop(重按)切换控制器,ians完整内容
peek:轻按,预览。3种状态:
peek可用,按中格浮现出来,其他的变模糊。
peek,可以快速预览接下来的controller内容
Peek+PreviewAction,快捷操作
pop:一般导航到peek状态下预览的控制器。
(1)如果forcetouch可用,则注册
-(void)viewDidLoad{
[super viewDidLoad];
//注册forceTouch
if(self.traitCollection.forceTouchCapability == UIForceTouchCapabilityAvailable){
[self registerForPreviewingWithDelegate:self sourceView:self.view];
}
}
(2)设置代理并实现代理方法UIViewControllerPreviewingDelegate
- -(UIViewController *)previewingContext:(id<UIViewControllerPreviewing>)previewingContext viewControllerForLocation:(CGPoint)location {
- //获取点击的cell
- NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:location];
- UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
- if (!cell) {
- return nil;
- }
- //pre-peek状态下,重点显示的区域
- previewingContext.sourceRect = cell.frame;
- HCViewController *vc = [[HCViewController alloc] init];
- vc.labelText = self.dataList[indexPath.row];
- //peek状态下,显示的view大小
- vc.preferredContentSize = CGSizeMake(0, 0);
- return vc;
- }
- - (void)previewingContext:(id <UIViewControllerPreviewing>)previewingContext commitViewController:(UIViewController *)viewControllerToCommit
- {
- [self.navigationController pushViewController:viewControllerToCommit animated:YES];
- }
(3)Peek中PreviewAction的实现
新增数组,储存PreviewAction
@property (nonatomic,strong) NSArray *arrayPreviewActions;
previewActionItems方法中返回存储PreviewAction的数组;
- -(NSArray<id<UIPreviewActionItem>> *)previewActionItems
- {
- return self.arrayPreviewActions;
- }
设置PreviewAction数组内容,并实现功能。(即快捷操作)
- -(void)viewDidLoad{
- [super viewDidLoad];
- self.view.backgroundColor = [UIColor whiteColor];
- [self 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");
- }],
- ];
- }