[英]3D Touch peek and pop trouble
我正在嘗試在我的應用程序中實現窺視和彈出功能。但是由於無法測試,我想知道自己是否做得正確,我覺得有問題嗎?
- (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;
}
我目前正在我們的應用程序中實現此功能,這看起來基本上是正確的。 我遇到的一個問題是,在位置上給您的坐標是UIViewController的視圖的,而不是UITableView的。 根據您的設置,它們可能相同,但是就我而言,我需要將位置轉換為UITableView的坐標。
CGPoint convertedLocation = [self.view convertPoint:location toView:self.tableView];
當然,您的里程可能會有所不同。
如果您使用正確的sourceView注冊了PreviewDelegate,則無需校正位置。 所以代替
[self registerForPreviewingWithDelegate:self sourceView:self.view];
您應該這樣注冊:
[self registerForPreviewingWithDelegate:self sourceView:self.tableView];
然后,您從委托調用獲得的位置將考慮滾動/ contentOffset。
您的代碼唯一的問題是獲取convertedLocation的方法不正確。
即使滾動表格視圖,下面的代碼也將獲得正確的位置。
CGPoint convertedLocation = [self.tableView convertPoint:location fromView:self.view];
NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:convertedLocation];
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.