使用操作对 TableViewCell 和集合视图单元格进行查看和弹出效果
1)您应该将调用者 viewController 类寻址为 UIViewControllerPreviewing Delegate
@interfaceMyTableViewController()
2)创建@property用于存储信息。
@property (nonatomic, strong) id previewingContext;
3)在ViewDidLoad中调用forceTouchIntialize方法
- (void)viewDidLoad {
[super viewDidLoad];
[self forceTochIntialize];
}
4)检查压力触摸是否可用
-(void)forceTouchIntialize{
if ([self isForceTouchAvailable]) {
self.previewingContext = [self registerForPreviewingWithDelegate:self sourceView:self.view];
}
}
- (BOOL)isForceTouchAvailable {
BOOL isForceTouchAvailable = NO;
if ([self.traitCollection respondsToSelector:@selector(forceTouchCapability)]) {
isForceTouchAvailable = self.traitCollection.forceTouchCapability == UIForceTouchCapabilityAvailable;
}
return isForceTouchAvailable;
}
5)指定视图控制器要预览哪些内容(FOR PEEK)
- (UIViewController *)previewingContext:(id<UIViewControllerPreviewing> )previewingContext viewControllerForLocation:(CGPoint)location{
CGPoint cellPostion = [yourTableView convertPoint:location fromView:self.view];
NSIndexPath *path = yourTableView indexPathForRowAtPoint:cellPostion];
if (path) {
UITableViewCell *tableCell = [yourTableView
cellForRowAtIndexPath:path];
//Pushing to a nib File
PushViewController *previewController = [[PushViewController alloc] initWithNibName:@"PushViewController" bundle:nil];
//To Pass data to Preview ViewController
// id temp = [yourDataArray objectAtIndex:path.row];
// PushViewController.anyObject=temp;
previewingContext.sourceRect = [self.view convertRect:tableCell.frame fromView: yourTableView
]; return previewController;
}
return nil;
}
6)深度压力下的 POP(适用于 POP)
-(void)previewingContext:(id )previewingContext commitViewController: (UIViewController *)viewControllerToCommit {
[self.navigationController showViewController:viewControllerToCommit sender:nil];
}
7)当控制器看到并向上滑动时,如果您想添加删除等按钮,请将此方法添加到您的预览视图控制器中(在此示例中为“ PushViewController”)
- (NSArray<id> *)previewActionItems {
UIPreviewAction *previewAction1 = [UIPreviewAction actionWithTitle:@"delete" style:UIPreviewActionStyleDefault handler:^(UIPreviewAction *action, UIViewController *previewViewController){
}];
UIPreviewAction *previewAction2 = [UIPreviewAction actionWithTitle:@"archive" style:UIPreviewActionStyleDefault handler:^(UIPreviewAction *action, UIViewController *previewViewController){
}];
return @[previewAction1,previewAction2];
}