在同一视图控制器

时间:2016-07-21 17:52:28

标签: ios swift uicollectionview 3dtouch peek-pop

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

以下是我实现3D触控功能的方法:

我把它放在VC的viewDidAppear中,因为它因为某些原因在viewDidLoad中没有工作:

  override func viewDidAppear(animated: Bool) {

  if( traitCollection.forceTouchCapability == .Available){

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

}

以下是我在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))

}

gif of what's happening

1 个答案:

答案 0 :(得分:4)

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

我在viewController的顶部有一个scrollView,在底部有一个tableView。我希望其中两个人采用偷看和流行音乐。以下是我的所作所为。

首先,您需要将UIViewControllerPreviewingDelegate添加到您想要实现peek和pop函数的viewController。

其次,通过调用

注册容器视图(通常是self.view)
if self.traitCollection.forceTouchCapability == .Available {
    registerForPreviewingWithDelegate(self, sourceView: view)
}

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

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
}

然后对滚动视图执行完全相同的操作,然后您就可以在同一个viewController中查看和弹出scrollView和tableView。

最后实现pop的第二个委托

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

它非常适合我。希望它也会帮助你。

相关问题