Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

3D Touch peek and pop trouble

I'm trying to implement peek and pop feature in my application.but since I can't test it yet, I wanted to know if I was doing it correctly, I feel something is wrong?

- (UIViewController *)previewingContext:(id<UIViewControllerPreviewing>)previewingContext viewControllerForLocation:(CGPoint)location 
{
    NSLog(@"TEST");

    NSIndexPath *indexPath = [self.myTableView indexPathForRowAtPoint:location];

    if (indexPath) {
        UITableViewCell *cell = [self.myTableView cellForRowAtIndexPath:indexPath];
        NSDictionary *dict = [self.array objectAtIndex:indexPath.row];
        cell.textLabel.text = [dict objectForKey:@"name"];

        DetailViewController *previewController = [[DetailViewController alloc] init];
        //     previewController.content = content;

        previewingContext.sourceRect = cell.frame;

        return previewController;
    }

return nil;
}
like image 369
Nick Lasta Avatar asked Sep 24 '15 23:09

Nick Lasta


3 Answers

I'm currently implementing this in our app and that looks mostly right. A problem I encountered is that the coordinates given to you in location are for the UIViewController's view, not the UITableView. Depending on your setup, they may be the same, but in my case, I needed to convert the location to the UITableView's coordinates.

CGPoint convertedLocation = [self.view convertPoint:location toView:self.tableView];

Of course, your mileage may vary.

like image 91
KingWulfgar Avatar answered Oct 19 '22 07:10

KingWulfgar


You should not need to correct the location if you registered the previewDelegate with the correct sourceView. So instead of

[self registerForPreviewingWithDelegate:self sourceView:self.view];

you should register it this way:

[self registerForPreviewingWithDelegate:self sourceView:self.tableView];

Then location you get from the delegate call takes scrolling / contentOffset into account.

like image 3
FrankWest Avatar answered Oct 19 '22 08:10

FrankWest


The only problem with your code is the way to get convertedLocation is not right.

Below code will get correct location even after you scroll the table view.

CGPoint convertedLocation = [self.tableView convertPoint:location fromView:self.view];

NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:convertedLocation];
like image 1
Frank Wang Avatar answered Oct 19 '22 08:10

Frank Wang