我想对我的应用程序的两个 Collection View 进行 3D 触摸(仅限“Peek”功能)。它们都包含在同一个 View Controller 中。无论我尝试什么语法,它总是在第一个 Collection View 中显示单元格的图像和文本,即使我在第二个 Collection View 中选择一个单元格也是如此。我如何将它们分开,以便只有一个专门针对我选择的 collectionview 单元格?
以下是我实现 3D 触摸功能的方式:
我把它放在 VC 的 viewDidAppear 中,因为由于某种原因它在 viewDidLoad 中不起作用:
override func viewDidAppear(animated: Bool) {
if( traitCollection.forceTouchCapability == .Available){
registerForPreviewingWithDelegate(self, sourceView: collectionView1)
registerForPreviewingWithDelegate(self, sourceView: collectionView2)
}
这是我在包含两个 Collection View 的 VC 中的内容:
func previewingContext(previewingContext: UIViewControllerPreviewing, viewControllerForLocation location: CGPoint) -> UIViewController? {
guard let previewVC = storyboard?.instantiateViewControllerWithIdentifier("PreviewVC") as? PreviewViewController
else{ return nil }
guard let indexPath = collectionView1?.indexPathForItemAtPoint(location) else { return nil }
guard let indexPath2 = collectionView2.indexPathForItemAtPoint(location) else { return nil }
guard let cell1 = collectionView1?.cellForItemAtIndexPath(indexPath) else { return nil }
guard let cell2 = collectionView2.cellForItemAtIndexPath(indexPath) else { return nil}
previewVC.selectedItem = String(Gifs.row1[indexPath.row])
previewVC.selectedItem2 = String(Gifs.row2[indexPath.rown])
previewVC.preferredContentSize = CGSize(width: 245, height: 200)
previewingContext.sourceRect = cell1.frame
return previewVC
}
这是我在 previewVC 中的内容:
class PreviewViewController: UIViewController {
@IBOutlet weak var selectedGifImageView: UIImageView!
@IBOutlet weak var textLabel: UILabel!
var selectedItem: String?
var selectedItem2: String?
var delegate: MoveScrollViewController?
override func viewWillAppear(animated: Bool) {
textLabel.text = Gifs.gifDictionary[selectedItem]
selectedGifImageView.setGifImage(UIImage(name: selectedItem))
textLabel.text = Gifs.gifDictionary[selectedItem2]
selectedGifImageView.setGifImage(UIImage(name: selectedItem2))
}
最佳答案
这就是我在项目中所做的。
我在 viewController 的顶部有一个 scrollView,在底部有一个 tableView。我希望其中两个采用 peek and pop。所以以下是我所做的。
首先,您需要将 UIViewControllerPreviewingDelegate 添加到要实现 peek 和 pop 功能的 viewController。
其次,通过调用注册容器 View (一般是self.view)
if self.traitCollection.forceTouchCapability == .Available {
registerForPreviewingWithDelegate(self, sourceView: view)
}
第三,将 View 坐标系的 CGPoint 转换为您希望在 previewingContext 委托(delegate)中具有查看和弹出功能的任何 View
func previewingContext(previewingContext: UIViewControllerPreviewing, viewControllerForLocation location: CGPoint) -> UIViewController?
例如:
一个。将 self.view 上的 CGPoint 转换为 tableView 内的点
let tableViewPoint = tableView.convertPoint(location, fromView: view)
有了这个点之后,就可以确认tableViewPoint是否在tableView内部,从tableViewPoint中获取tableViewCell,并用它做一些事情。
if self.tableView.pointInside(tableViewPoint, withEvent: nil) {
guard let indexPath = self.tableView.indexPathForRowAtPoint(tableViewPoint) else {
return nil
}
guard let tableViewCell = self.tableView.cellForRowAtIndexPath(indexPath) else {
return nil
}
// do something with your tableView cell here...
let yourViewController = UIStoryboard(name: "Main", bundle: nil).instantiateViewControllerWithIdentifier("YOURVIEWCONTROLLER") as! YOURVIEWCONTROLLER
// set up the contentSize for peek
yourViewController.preferredContentSize = CGSize(width: 0.0, height: 600)
// set up the focusing rect for the tableViewCell so that it won't be blurred out.
let viewPoint = self.view.convertPoint(tableCell.frame.origin, fromView: self.tableView)
previewingContext.sourceRect = CGRect(origin: viewPoint, size: tableCell.frame.size)
return yourViewController
}
然后对 ScrollView 执行完全相同的操作,然后您将能够在同一个 viewController 中查看和弹出 scrollView 和 tableView。
最后实现弹出的第二个委托(delegate)
func previewingContext(previewingContext: UIViewControllerPreviewing, commitViewController viewControllerToCommit: UIViewController) {
showViewController(viewControllerToCommit, sender: self)
}
它非常适合我。希望它也能帮助您。
关于ios - 在同一 View Controller 中对多个 Collection View 或 TableView 实现 3D 触摸,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38511133/