Here the scenario is, UICollectionView is in UITableViewCell and UITableView is on UIViewController, now I have to use UILongPressGestureRecognizer
if 3D Touch is not available I need to register it in UICollectionView Cell, UILongPressGestureRecognizer
is working perfectly, but I'm not able to register registerForPreviewing
in the cell for 3D Touch. What is the way to register this in cell
.
Any help will be appreciated.
Thanks
3D Touch can register only on UIViewController
or instance of UIViewController class
Now, you have to register your tableView with 3D Touch. The only problem is getting selected UICollectionViewCell which is in UITableViewCell.
You can get that in UIViewControllerPreviewingDelegate
delegate methods
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
}
output: