3D Touch预览
实现
UIViewControllerPreviewingDelegate协议1
2@interface TableViewController : UITableViewController <UIViewControllerPreviewingDelegate>
@end注册
1
2
3if (self.traitCollection.forceTouchCapability == UIForceTouchCapabilityAvailable) {
[self registerForPreviewingWithDelegate:self sourceView:self.view];
}补全代理方法
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18#pragma mark - previewing Delegate
- (UIViewController *)previewingContext:(id<UIViewControllerPreviewing>)previewingContext viewControllerForLocation:(CGPoint)location {
CGPoint point = [self.tableView convertPoint:location fromView:self.view];
// 如果point在tableview中
if (CGRectContainsPoint(self.tableView.bounds, point)) {
NSIndexPath * indexPath = [self.tableView indexPathForRowAtPoint:point];
if (!indexPath) {
return nil;
}
UIViewController * previewController = [self.storyboard instantiateViewControllerWithIdentifier:@"UIViewController"];
return previewController;
}
return nil;
}
- (void)previewingContext:(id<UIViewControllerPreviewing>)previewingContext commitViewController:(UIViewController *)viewControllerToCommit {
[self showViewController:viewControllerToCommit sender:self];
}