如何在集合视图单元格内为视图添加手势?

时间:2019-01-14 07:04:26

标签: ios swift uicollectionviewcell uitapgesturerecognizer

我要添加一个视图集合视图单元格(也有其他视图)内的一组图像(socialView),必须对其进行通用单击。此点击不应与集合视图单元格点击相同。

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

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函数。我也尝试放入必需的init,但是那里的社交视图尚未初始化。因此崩溃

最终答案

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)
}

3 个答案:

答案 0 :(得分:1)

@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。在ViewController中

        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...
        }
    }

答案 1 :(得分:0)

要添加点击手势,请将以下代码添加到集合视图的单元格中。

let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(self.socialViewTapped))
socialView?.addGestureRecognizer(tapGestureRecognizer)
socialView?.isUserInteractionEnabled = true

将此代码添加到集合视图单元格中。

答案 2 :(得分:0)

如果您仍然找不到办法,我希望这能奏效 您在这样的自定义单元格中使用这样的协议

import UIKit

protocol CustomDelegate {
    func showData(indexPath: IndexPath?)
}

class CustomCell: UICollectionViewCell {

// properties

var delegate: CustomDelegate?
var selectedAtIndex: IndexPath?

// outlets

override func awakeFromNib() {
    super.awakeFromNib()
    myView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(dothis)))
}

@objc func dothis(){
    delegate?.showData(indexPath: selectedAtIndex)
}

@IBOutlet weak var myView: UIView!
}

像这样编写您的视图控制器

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, CustomDelegate{
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! CustomCell
    cell.delegate = self
    cell.selectedAtIndex = indexPath
    return cell
}
func showData(indexPath: IndexPath?) {
    if let ip = indexPath{
            print("\(ip) is the index path for tapped view")
    }
}
}

告诉您是否有帮助