3D Touch peek and pop trouble
Asked Answered
T

3

5

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;
}
Tuggle answered 24/9, 2015 at 23:7 Comment(0)
F
6

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.

Force answered 30/9, 2015 at 15:38 Comment(1)
im the same as yourself, the coordinates were incorrect because it wasnt taking into consideration that the status bar was present.Salba
C
3

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.

Canute answered 9/2, 2016 at 3:13 Comment(2)
Sorry for the confusion, this does not seem to work for a tableView but does for a collectionView.Canute
This wasn't the answer for the tableView, but reading this fixed exactly the issue I was having with the UICollectionView (and with Xamarin.iOS). So pretty cool how an answer leads to fixing another issue for someone else.Parcenary
C
1

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];
Companionway answered 19/11, 2015 at 5:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.