开发者社区> 问答> 正文

当集合视图单元格被双击时,如何使图像可见?

我有一个集合视图,有很多图像,所有这些图像都有一个心脏图像在右方。这个心脏图像需要设置为可见,当大的图像被双击作为一个指标,它已经被喜欢。

我已经添加了一个双击手势到我的集合视图,现在我需要将心脏图像设置为可见时,此手势发生在选定的细胞。

对我怎么做有什么建议吗?我在任何地方都找不到答案。

这是我的集合视图控制器:

import UIKit

class OevelserCollectionViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource {

    // MARK: - Properties

    @IBOutlet weak var OevelserCollectionView: UICollectionView!
    @IBOutlet var tap: UITapGestureRecognizer!

    var oevelseCollectionViewFlowLayout: UICollectionViewFlowLayout!

    let oevelseArray = OevelseArray()

    // MARK: - Init

    override func viewDidLoad() {
        super.viewDidLoad()

        setupOevelseCollectionView()

    }

    // MARK: - Functions

    @IBAction func didDoubleTap(_ sender: UITapGestureRecognizer) {
        print("tapped")

    }

    override func viewWillLayoutSubviews() {
        super.viewWillLayoutSubviews()
        setupOevelseCollectionViewItemSize()
    }

    private func setupOevelseCollectionView() {
        self.OevelserCollectionView.delegate = self
        self.OevelserCollectionView.dataSource = self
        let nib = UINib(nibName: "OevelseCollectionViewCell", bundle: nil)
        OevelserCollectionView.register(nib, forCellWithReuseIdentifier: "OevelseCollectionViewCell")
    }

    private func setupOevelseCollectionViewItemSize() {
        if oevelseCollectionViewFlowLayout == nil {
            let numberOfItemPerRow: CGFloat = 1
            let lineSpacing: CGFloat = 20
            let interItemSpacing: CGFloat = 8

            let width = (OevelserCollectionView.frame.width - (numberOfItemPerRow - 1) * interItemSpacing) / numberOfItemPerRow
            let height = width - 50

            oevelseCollectionViewFlowLayout = UICollectionViewFlowLayout()

            oevelseCollectionViewFlowLayout.itemSize = CGSize(width: width, height: height)
            oevelseCollectionViewFlowLayout.sectionInset = UIEdgeInsets.zero
            oevelseCollectionViewFlowLayout.scrollDirection = .vertical
            oevelseCollectionViewFlowLayout.minimumLineSpacing = lineSpacing
            oevelseCollectionViewFlowLayout.minimumInteritemSpacing = interItemSpacing

        OevelserCollectionView.setCollectionViewLayout(oevelseCollectionViewFlowLayout, animated: true)
        }
    }

    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return oevelseArray.oevelser.count
    }

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

        let oevelseText = oevelseArray.oevelser[indexPath.item].oevelseName
        let oevelseImage = oevelseArray.oevelser[indexPath.item].oevelseImage
        cell.oevelseLabel.text = oevelseText
        cell.oevelseImageView.image = UIImage(named: oevelseImage)

        return cell
    }

    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {

    }
}

下面是我的集合视图单元格类:

import UIKit

class OevelseCollectionViewCell: UICollectionViewCell {

    @IBOutlet weak var oevelseImageView: UIImageView!
    @IBOutlet weak var oevelseLabel: UILabel!
    @IBOutlet weak var isLikedImageView: UIImageView!
    @IBOutlet weak var heartImageWidthConstraint: NSLayoutConstraint!

    override func awakeFromNib() {
        super.awakeFromNib()

    }

}

展开
收起
游客5akardh5cojhg 2019-12-04 13:58:57 560 0
1 条回答
写回答
取消 提交回答
  • 内cellForRowAt

    cell.oevelseImageView.image = UIImage(named: oevelseImage)
    cell.oevelseImageView.tag = indexPath.row
    let tapGR = UITapGestureRecognizer(target: self, action: #selector(handleTap)) 
    tapGR.numberOfTapsRequired = 2
    cell.oevelseImageView.addGestureRecognizer(tapGR)
    

    @objc func handleTap(_ gesture: UITapGestureRecognizer){ let index = gesture.view!.tag guard let cell = tableView.cellForRow(at:IndexPath(row:index,section:0)) else { return } arr[index].isLiked.toggle() cell.isLikedImageView.image = arr[index].isLiked ? <#likeImg#> : <#defImg#> } 或

    @objc func handleTap(_ gesture: UITapGestureRecognizer){ arr[gesture.view!.tag ].isLiked.toggle() self.tableView.reloadRows(at:[IndexPath(row:index,section:0)],with:.none) }

    2019-12-04 13:59:49
    赞同 展开评论 打赏
问答地址:
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载