在同一个视图控制器中的多个集合视图或表格视图上实现 3D 触摸 [英] Implement 3D touch on multiple collection views or table views in the same view controller

查看:27
本文介绍了在同一个视图控制器中的多个集合视图或表格视图上实现 3D 触摸的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想对我的应用程序的两个集合视图进行 3D 触摸(仅查看"功能).它们都包含在同一个视图控制器中.无论我尝试什么语法,它总是在第一个集合视图中显示单元格的图像和文本,即使我在第二个集合视图中选择一个单元格.我如何将这些分开,以便只有一个专门针对我选择的 collectionview 单元格发生?

I would like to have 3D touch on both of the collection views of my app(Only the "Peek" functionality). They are both contained in the same view controller. No matter what syntax I try it always shows the image and text for the a cell in the first collectionview, even if I choose a cell in the second collection view. How do I separate these so that only one happens specifically for the collectionview cell I have selected?

以下是我实现 3D 触摸功能的方式:

Here is how I am implementing the 3D touch functionality:

我把它放在 VC 的 viewDidAppear 中,因为它由于某种原因在 viewDidLoad 中不起作用:

I put this in VC's viewDidAppear because it wasn't working in viewDidLoad for some reason:

  override func viewDidAppear(animated: Bool) {

  if( traitCollection.forceTouchCapability == .Available){

    registerForPreviewingWithDelegate(self, sourceView: collectionView1)
    registerForPreviewingWithDelegate(self, sourceView: collectionView2)

}

这是我在包含两个集合视图的 VC 中的内容:

Here is what I also have in the VC that houses the two collectionviews:

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 中的内容:

And here is what I have in the 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))

}

正在发生的事情的 gif

推荐答案

这就是我在我的项目中所做的.

This is what I did in my project.

我在 viewController 的顶部有一个 scrollView,在底部有一个 tableView.我希望其中两个采用 peek 和 pop.所以以下是我所做的.

I have a scrollView on the top of the viewController and a tableView on the bottom. And I would like two of them to adopt the peek and pop. So the following is what I did.

首先,您需要将 UIViewControllerPreviewingDelegate 添加到要实现查看和弹出功能的视图控制器中.

First, you need to add UIViewControllerPreviewingDelegate to the viewController that you would like to implement the peek and pop function.

二、通过调用注册容器视图(通常是self.view)

Second, register the container view (usually is self.view) by calling

if self.traitCollection.forceTouchCapability == .Available {
    registerForPreviewingWithDelegate(self, sourceView: view)
}

第三,您将视图坐标系的 CGPoint 转换为您希望在 previewingContext 委托中具有窥视和弹出功能的任何视图

Third, you convert the CGPoint of the view coordinate system to any view that you would like to have peek and pop feature in the previewingContext delegate

func previewingContext(previewingContext: UIViewControllerPreviewing, viewControllerForLocation location: CGPoint) -> UIViewController?

例如:

一个.将 self.view 上的 CGPoint 转换为 tableView 内的点

a. Convert CGPoint on the self.view to the point inside tableView

let tableViewPoint = tableView.convertPoint(location, fromView: view)

B.有了这个点之后,就可以确认tableViewPoint是否在tableView里面,从tableViewPoint中获取tableViewCell,并对其进行处理.

b. After you have this point, you can confirm if the tableViewPoint is inside the tableView, get the tableViewCell from the tableViewPoint and do something with it.

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
}

然后对滚动视图执行完全相同的操作,然后您将能够在同一个 viewController 中同时对 scrollView 和 tableView 进行 peek 和 pop.

And then do the exactly the same to the scroll view, then you will be able to have peek and pop for both scrollView and tableView in the same viewController.

最后为 pop 实现第二个委托

Last implement the second delegate for pop

func previewingContext(previewingContext: UIViewControllerPreviewing, commitViewController viewControllerToCommit: UIViewController) {
    showViewController(viewControllerToCommit, sender: self)
}

它非常适合我.希望它也能帮到你.

It works for me perfectly. Hopefully it will help you out as well.

这篇关于在同一个视图控制器中的多个集合视图或表格视图上实现 3D 触摸的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆