Search code examples
iosobjective-ccocoa-touch3dtouch

3D Touch to multiple buttons in one class


i am implementing 3d touch peek an pop feature in iOS.

This is the code i have written for one button

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

This is just for one button in that same view i have 4 other buttons which i want to apply 3d touch in the same class. How can i do this??


Solution

  • You can just check the location. For example:

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