首页 > 解决方案 > 在 UICollectionView 单元内的录音按钮上显示视图

问题描述

我正在开发一个包含collectionview的iOS应用程序,在这个collectionview中我有一个按钮,如下所示:

ViewController
-UICollectionView(myColl)
--UICollectionViewCell
---UIButton (myButton)
-UIView (myView)

我想要做的是在我点击它时显示myViewmyButton我尝试的是:

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
   let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "TestCell", 
          for: indexPath) as! TestCell
   cell.myButton.addTarget(self, action: #selector(self.showEditView), for: .touchUpInside)
}

并且在showEditView()

@objc func showEditView(sender:UIButton!)  {
        let position: CGPoint = sender.convert(CGPoint.zero, to: self.myColl)
        myView.center = position
}

但这没有用,我该怎么做才能得到它?

标签: iosswiftuicollectionviewuibuttonuicollectionviewcell

解决方案


 @objc func showEditView(sender:UIButton!)  {
        let position: CGPoint = sender.convert(CGPoint.zero, to: self.collectionview)
        let indexPath = self.collectionview.indexPathForItem(at: position)
        if indexPath != nil {
         myView.center = position
        //if your view is hidden
         myView.isHidden = false
        }
    }

推荐阅读