ios - 3D Touch:UICollectionViewCell 中的 registerForPreviewing
问题描述
这里的场景是,UICollectionView 在 UITableViewCell 和 UITableView 在 UIViewController 上,现在我必须使用UILongPressGestureRecognizer
如果 3D Touch 不可用我需要在 UICollectionView 单元格中注册它,UILongPressGestureRecognizer
工作正常,但我无法registerForPreviewing
在单元格中注册用于 3D 触控。注册这个的方法是什么cell
。
任何帮助将不胜感激。
谢谢
解决方案
3D Touch 只能在UIViewController
或instance of UIViewController class
现在,您必须使用 3D Touch 注册您的 tableView。唯一的问题是选择 UITableViewCell 中的 UICollectionViewCell。
你可以在UIViewControllerPreviewingDelegate
委托方法中得到它
func previewingContext(_ previewingContext: UIViewControllerPreviewing, viewControllerForLocation location: CGPoint) -> UIViewController? {
//Get indexPath for selected tableViewCell using location
guard let tableViewIndexPath = tableView.indexPathForRow(at: location) else {
return nil
}
//Get selected tableViewCell
//NestedCell is UITableViewCell
guard let tableViewCell = tableView.cellForRow(at: tableViewIndexPath) as? NestedCell else {
return nil
}
//CollectionView which is in every tableViewCell
let collectionView = tableViewCell.collectionView
//Get the location of collectionView
let collectionViewLocation = tableView.convert(location, to: collectionView)
//Get indexPath of selected collectionViewCell
guard let collectionViewIndexPath = collectionView.indexPathForItem(at: collectionViewLocation) else {
return nil
}
//Finally, you get selected collectionViewCell
guard let collectionViewCell = tableViewCell.collectionView.cellForItem(at: collectionViewIndexPath) else {
return nil
}
let preview = PreviewVC()
preview.view.backgroundColor = collectionViewCell.backgroundColor
preview.preferredContentSize = CGSize(width: 0.0, height: 600)
//Modify CGRect/frame for selected collectionview cell
var sourceRect = collectionViewCell.frame
sourceRect.origin.x = sourceRect.origin.x - collectionView.contentOffset.x
let y = tableViewCell.frame.origin.y + collectionViewCell.frame.origin.y
sourceRect.origin.y = y
previewingContext.sourceRect = sourceRect
return preview
}
输出:
推荐阅读
- .net - 无法通过 NuGet 解决依赖关系
- node.js - Firebase 事务一次更新文档中的多个字段
- angular - Angular 8 Universal - 生产构建后./dist 中没有 index.html
- c++ - 条件 C++ 类成员定义
- javascript - 当锚定元素在视图中时如何将滚动事件发送到父组件React JS
- python - 无法将 micropython 闪存到 esp 32:打开串行错误
- c# - 是否可以在将在 Linux 中运行的 .Net Core Console App 中使用 .Net Framework 4.5 Dll 参考
- python - 如何将命令作为函数调用
- python - 找不到页面 (404) notes.url
- macos - 检查并从 MAC 上的 ELF 文件中获取二进制文件