如何显示 UICollectionViewCell 的 MenuController?

How to show MenuController of UICollectionViewCell?

我如何重现 iPhone 上的消息的复制粘贴功能,如果您长按一条消息,消息单元格会变灰并弹出 [=12] =] 出现。如何在我的 UICollectionViewCells 上显示相同的菜单?

事实证明,他的功能已经内置,并且与实现三个 collectionView: 委托方法一样简单。我用一个名为 copyableProperty 的 属性 创建了一个协议 CopyableCell,一个单元格想要复制到剪贴板的字符串,我可以复制的单元格必须遵循该字符串。从那时起就很简单了:

  func collectionView(collectionView: UICollectionView, shouldShowMenuForItemAtIndexPath indexPath: NSIndexPath) -> Bool {
    if let _ = collectionView.cellForItemAtIndexPath(indexPath) as? CopyableCell {
      return true
    }
    return false
  }
  func collectionView(collectionView: UICollectionView, canPerformAction action: Selector, forItemAtIndexPath indexPath: NSIndexPath, withSender sender: AnyObject?) -> Bool {
    if action.description == "copy:" {
      return true
    }

    return false
  }

  func collectionView(collectionView: UICollectionView, performAction action: Selector, forItemAtIndexPath indexPath: NSIndexPath, withSender sender: AnyObject?) {
    //No more checking is needed here since we only allow for copying
    if let cell = collectionView.cellForItemAtIndexPath(indexPath) as? CopyableCell {
      UIPasteboard.generalPasteboard().string = cell.copyableProperty
    }
  }