3D触摸一个类中的多个按钮

时间:2016-04-28 22:06:02

标签: ios objective-c cocoa-touch 3dtouch

我正在iOS中实现3D touch peek 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 touch。我怎样才能做到这一点??

1 个答案:

答案 0 :(得分:1)

您只需查看位置即可。例如:

- (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;
    }
    …
}