0%

iOS下'3D Touch预览'的配置方法

3D Touch预览


  1. 实现UIViewControllerPreviewingDelegate协议

    1
    2
    @interface TableViewController : UITableViewController <UIViewControllerPreviewingDelegate>
    @end
  2. 注册

    1
    2
    3
    if (self.traitCollection.forceTouchCapability == UIForceTouchCapabilityAvailable) {
    [self registerForPreviewingWithDelegate:self sourceView:self.view];
    }
  3. 补全代理方法

    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];
    }