3D Touch 将多个按钮合二为一 class

3D Touch to multiple buttons in one class

我正在 iOS.

中实现 3d touch peek an pop 功能

这是我为一个按钮写的代码

- (UIViewController *)previewingContext:(id <UIViewControllerPreviewing>)previewingContext viewControllerForLocation:(CGPoint)location
{

    UIViewController *detailVC = [self.storyboard instantiateViewControllerWithIdentifier:@"detail"];


    detailVC.preferredContentSize = CGSizeMake(0.0, 500.0);

    previewingContext.sourceRect = self.btnDetail.frame;

    return detailVC;
}

这仅适用于同一视图中的一个按钮,我还有 4 个其他按钮,我想在同一视图中应用 3d 触摸 class。我怎样才能做到这一点??

您可以直接查看位置。例如:

- (UIViewController *)previewingContext:(id <UIViewControllerPreviewing>)previewingContext viewControllerForLocation:(CGPoint)location
{
    if(CGRectContainsPoint(self.btnDetail.frame,location)){
        UIViewController *detailVC = [self.storyboard instantiateViewControllerWithIdentifier:@"detail"];


        detailVC.preferredContentSize = CGSizeMake(0.0, 500.0);

        previewingContext.sourceRect = self.btnDetail.frame;

        return detailVC;
    }
    if(CGRectContainsPoint(self.otherBtn.frame,location)){
        UIViewController *otherVC = [self.storyboard instantiateViewControllerWithIdentifier:@"other"];


        otherVC.preferredContentSize = CGSizeMake(0.0, 500.0);

        previewingContext.sourceRect = self.otherBtn.frame;

        return otherVC;
    }
    …
}