2017-07-23 84 views
0

我创建了一个音板,并且当您长时间按压拼贴块(CollectionView Cell)时,它会变成3D Touch弹出式文本的拼贴。3D Touch不在CollectionView中

我有一个ViewCollection,故事板ID为'tilePreview'。下面的代码,但3D触摸没有。

我该怎么做?有谁能够帮助我?

import UIKit 
import AVFoundation 

class CollectionViewController: UICollectionViewController, UIViewControllerPreviewingDelegate { 

    @IBOutlet var soundBoard: UICollectionView! 

    var list = [String]() 
    var buttonOn: Bool = true 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     if traitCollection.forceTouchCapability == UIForceTouchCapability.available { 
      registerForPreviewing(with: self, sourceView: collectionView!) 
     } 
    } 

    override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> MyCollectionViewCell { 

     let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! MyCollectionViewCell 

     let tileText = list[indexPath.row] 

     cell.cellTitle.text = tileText 

     return cell 
    } 


    override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) { 
     Speak(currentTitle: list[indexPath.row]) 
    } 

    // MARK: - Preview tile 
    func previewingContext(_ previewingContext: UIViewControllerPreviewing, viewControllerForLocation location: CGPoint) -> UIViewController? { 

     guard let indexPath = collectionView?.indexPathForItem(at: location) else { return nil } 
     guard let cell = collectionView?.cellForItem(at: indexPath) else { return nil } 
     guard let detailVC = storyboard?.instantiateViewController(withIdentifier: "tilePreview") as? DetailViewController else { return nil } 
     previewingContext.sourceRect = cell.frame 

     return detailVC 
    } 

    func previewingContext(_ previewingContext: UIViewControllerPreviewing, commit viewControllerToCommit: UIViewController) { 

     show(viewControllerToCommit, sender: self) 

    } 


} 
+0

上UICollectionView [强制触摸的可能的复制在UITableViewCell里面](https://stackoverflow.com/questions/41498963/force-touch-on-uicollectionview-inside-of-uitablev iewcell) – sschale

+0

您需要注册每个collectionView,而不是更大的对象。在'cellForItemAt'内部做到这一点,并看到链接的帖子。 – sschale

+0

你为什么[回滚编辑nathan制作](https://stackoverflow.com/posts/45261272/revisions)?在我看来,它通过改进语法明显地阐明了你的文章。我没有看到他改变你问题意义的地方。你不喜欢什么?或者是一个意外? –

回答

1

1)在viewDidLoad中检查3D Touch是可用或不可

override func viewDidLoad() { 
    super.viewDidLoad() 

    //3D touch 
    if(traitCollection.forceTouchCapability == .available){ 
     registerForPreviewing(with: self, sourceView: view) 
    } 
} 

2)代表的方法用于三维触摸

//3D Touch delegate method 
extension PostViewController : UIViewControllerPreviewingDelegate { 
    @available(iOS 9.0, *) 
    func previewingContext(_ previewingContext: UIViewControllerPreviewing, commit viewControllerToCommit: UIViewController) { 
     self.navigationController?.show(viewControllerToCommit, sender: self) 
    } 

    @available(iOS 9.0, *) 
    func previewingContext(_ previewingContext: UIViewControllerPreviewing, viewControllerForLocation location: CGPoint) -> UIViewController? { 
     let locationCell : CGPoint = self.collectionView.convert(location, from: self.view) 

     guard let indexPath = self.collectionView?.indexPathForItem(at: locationCell) else { 
      print(locationCell) 
      print("Cell Not found") 
      return nil 
     } 

     print("indexPath.row :: \(indexPath.row)") 
     guard let cell = self.collectionView?.cellForItem(at: indexPath) else { return nil } 

     let didSelectProduct : ProductDM = self.products[indexPath.row] 
     let destination: ProductDetailsViewController = UIStoryboard(storyboard: .main).instantiateViewController() 
     destination.selectedProduct = didSelectProduct 
     //destination.preferredContentSize = CGSize(width: 0.0, height: 300) 

     previewingContext.sourceRect = cell.frame 
     return destination 
    } 
} 
相关问题