首页 > 解决方案 > 如何为集合视图单元格内的视图添加手势?

问题描述

我正在添加一个images(socialView)在集合视图单元格(也有其他视图)内具有一组的视图,必须在该单元格上执行常见的单击。此单击不应与集合视图单元格单击相同。

我正在考虑在委托方法中添加UITapGestureRecognizer每次。但我想知道这是正确的方法吗?此外,我想获取已调用的 indexPath/位置。socialViewCollectionViewcellForItemAtsocialView

let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(self.socialViewTapped))

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
     let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "bidderAuctionCell", for: indexPath) as! BidderAuctionViewCell
     cell.socialView.addGestureRecognizer(tapGestureRecognizer)
}

@objc func socialViewTapped() {
     // Which item cell's socialview is clicked
}

更新

我已按照以下建议完成,但我无法在自定义单元格中添加 UITapGestureRecognizer 的位置。以下是我创建的自定义单元格。

class CustomViewCell: UICollectionViewCell {

    var index : IndexPath!
    var socialViewDelegate : SocialViewDelegate!

    @IBOutlet weak var socialView: UIView!

    override init(frame: CGRect) {
        super.init(frame:frame)
        let tapGestureRecognizer = UITapGestureRecognizer(target: self, action:   #selector(self.viewTapped))
        socialView.addGestureRecognizer(tapGestureRecognizer)
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder:aDecoder)
    }

    @objc func viewTapped(){
        socialViewDelegate.socialViewTapped(at: index)
    }
}

未调用 init 函数。我也尝试放入所需的初始化,但社交视图未初始化。所以崩溃

最终答案

class CustomViewCell: UICollectionViewCell {

    var index : IndexPath!
    var socialViewDelegate : SocialViewDelegate!

    @IBOutlet weak var socialView: UIView!

    override func awakeFromNib() {
        let tapGesture = UITapGestureRecognizer(target: self, action: #selector(handleTap))
        socialView.isUserInteractionEnabled = true
        socialView.addGestureRecognizer(tapGesture)
    }

    @objc func handleTap(){
        socialViewDelegate.socialViewTapped(at: index)
    }
}
protocol SocialViewDelegate {
    func socialViewTapped(at index: IndexPath)
}

标签: iosswiftuicollectionviewcelluitapgesturerecognizer

解决方案


@Anurag 您可以通过使用以下任一概念委派、闭包、通知来解决此问题。我会建议你使用 iOS 广泛遵循的委托模式,如 UITableView、UICollectionView 等。

  • 在实现委托模式之前,在视图中添加一个轻击手势识别器。
  • 在 CollectionViewCell 中声明一个协议方法来委派您的点击/操作
  • 在您的视图控制器中确认委托实现。
  • 在您的视图控制器中实现您的委托方法。

编码示例:

1. 你的 CollectionViewCell 应该是这样的

   import UIKit

    Protocol ViewTappedDelegate: class {
     func viewTapped(_ photo : String) { }
   }

    class YourCell: UICollectionViewCell, UIGestureRecognizerDelegate {

        @IBOutlet weak var containerView: UIView!
        weak var delegate : ViewTappedDelegate?

        override func awakeFromNib() {
            let tapGesture = UITapGestureRecognizer(target: self,
                                                    action: #selector(handleTap(recognizer:)))
            tapGesture.delegate = self
            containerView.isUserInteractionEnabled = true
            containerView.addGestureRecognizer(tapGesture)
        }

        @objc func handleTap(recognizer:UITapGestureRecognizer) {
            //Call Delegate method from here...
        }

    }

2.在视图控制器中

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


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

              cell.delegate = self
              return cell
          }



    // MARK: Delegate
     extension YourViewController : ViewTappedDelegate {
        func viewTapped(_ photo : String) {
          // Handle delegation here...
        }
    }

推荐阅读