gpt4 book ai didi

ios - 双击 Collection View 单元格时如何使图像可见?

转载 作者:行者123 更新时间:2023-11-29 05:16:09 24 4
gpt4 key购买 nike

我有一个包含很多图像的 Collection View ,所有这些图像的右上角都有一个心形图像。当双击大图像时,需要将此心形图像设置为可见,以表明它已被点赞。

我已在 Collection View 中添加了双击手势,现在我需要在所选单元格上发生此手势时将心形图像设置为可见。

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

这是我的 Collection View Controller :

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

}
}

这是我的 Collection View 单元格类:

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

}

}

最佳答案

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

关于ios - 双击 Collection View 单元格时如何使图像可见?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59164817/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com