来自firebase的照片中的UITapGestureRecognizer

时间:2018-03-23 11:52:44

标签: swift uitapgesturerecognizer

我正在做一个项目,我从数据库中获取照片,并在集合视图中准备好照片,如何将UITapGestureRecognizer添加到从数据库上传的所有照片中?

3 个答案:

答案 0 :(得分:0)

imageView.isUserInteractionEnabled = true

imageView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(imageTapped(_:))))

func imageTapped(_ recognizer: UIPanGestureRecognizer) {

}

 func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        print(indexPath.item)
}

Above code may help you to add gesture in image view.

答案 1 :(得分:0)

我认为你需要检查一下你点击的图像?为此,您可以将表格单元格的索引分配给imageView标记。就像:

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "CellIedntifier", for: indexPath)
    cell.imageView.tag = indexPath.row
    cell.imageView.isUserInteractionEnabled = true

    let gesture = UITapGestureRecognizer(target: self, action: #selector(actionImageClicked(_:)))
    cell.imageView.addGestureRecognizer(gesture)

    return cell
}

@objc func actionImageClicked(_ recognizer: UITapGestureRecognizer) {
    let index = (recognizer.view as! UIImageView).tag

}

答案 2 :(得分:0)

您需要为单元格中的每个UIImageView添加UITapGestureRecognizer

 import UIKit

 class CollectionViewCell: UICollectionViewCell {

 @IBOutlet private weak var myImageView: UIImageView!

 var imageViewTappedClosure: ((CollectionViewCell?,UIImageView?) -> Void)?

 override func awakeFromNib() {
   super.awakeFromNib()

   myImageView.isUserInteractionEnabled = true

   let tap = UITapGestureRecognizer(target: self, action: #selector(imgViewTapped))
   myImageView.addGestureRecognizer(tap)
 }


 func setContent() {

 }

 @objc private func imgViewTapped() {
    imageViewTappedClosure?(self,myImageView)
 }

}

当你点击UIImageView时,你可以检查点击了什么UIImageView。 (或UIImageView中的图像)

import UIKit

class ViewController: UIViewController {

  @IBOutlet weak var collectionView: UICollectionView!


}

extension ViewController: UICollectionViewDelegate, UICollectionViewDataSource {

  func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return 123
  }


  func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "myIdentifier", for: indexPath) as! CollectionViewCell
    cell.imageViewTappedClosure = { cell, imageView in

      //insert your implement code when you tap in ImageView in your cell
    }
    return cell
  }
}