【问题标题】:3D Touch peek and pop trouble3D Touch 偷看和弹出问题
【发布时间】:2015-09-24 23:07:03
【问题描述】:

我正在尝试在我的应用程序中实现 peek and pop 功能。但是由于我还不能测试它,我想知道我是否正确地执行它,我觉得有什么问题吗?

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

【问题讨论】:

    标签: ios objective-c xcode ios9 3dtouch


    【解决方案1】:

    我目前正在我们的应用程序中实现这一点,看起来基本正确。我遇到的一个问题是给你的位置坐标是针对 UIViewController 的视图,而不是 UITableView。根据您的设置,它们可能相同,但在我的情况下,我需要将位置转换为 UITableView 的坐标。

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

    当然,您的里程可能会有所不同。

    【讨论】:

    • 我和你一样,坐标不正确,因为它没有考虑到状态栏的存在。
    【解决方案2】:

    如果您使用正确的 sourceView 注册了 previewDelegate,则不需要更正位置。所以不是

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

    你应该这样注册:

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

    然后,您从委托调用中获得的位置将滚动/ contentOffset 考虑在内。

    【讨论】:

    • 对不起,这似乎不适用于tableView,但适用于collectionView。
    • 这不是 tableView 的答案,但阅读这篇文章完全解决了我在使用 UICollectionView(以及使用 Xamarin.iOS)时遇到的问题。一个答案如何为其他人解决另一个问题真是太酷了。
    【解决方案3】:

    您的代码的唯一问题是获得 convertLocation 的方式不正确。

    即使滚动表格视图,下面的代码也会得到正确的位置。

    CGPoint convertedLocation = [self.tableView convertPoint:location fromView:self.view];
    
    NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:convertedLocation];
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多