我有一个带有集合视图的视图控制器,如下所示:
import UIKit
private let reuseIdentifier = "LightboxCollectionViewCell"
class LightboxViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
var items: [Image]?
@IBOutlet weak var collectionView: UICollectionView!
override func viewDidLoad() {
super.viewDidLoad()
self.collectionView.delegate = self
self.collectionView.dataSource = self
let collectionViewCell = UINib(nibName: reuseIdentifier, bundle: nil)
self.collectionView.register(collectionViewCell, forCellWithReuseIdentifier: reuseIdentifier)
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return items!.count
}
func collectionView(_ collectionView: UICollectionView, didEndDisplaying cell: UICollectionViewCell, forItemAt indexPath: IndexPath) {
guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath as IndexPath) as? LightboxCollectionViewCell else {
fatalError(String(format: "The dequeued cell is not an instance of %s.", reuseIdentifier))
}
// Reset the zoom
cell.imageView.transform = CGAffineTransform.identity
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath as IndexPath) as? LightboxCollectionViewCell else {
fatalError(String(format: "The dequeued cell is not an instance of %s.", reuseIdentifier))
}
if let item = items?[indexPath.row] {
cell.initialize(media: item)
}
return cell
}
}
这是单元格类:
import UIKit
import Kingfisher
class LightboxCollectionViewCell: UICollectionViewCell {
var media: Image?
// Sets the maximum zoom
let maxZoom: CGFloat = 10
@IBOutlet weak var background: UIView!
@IBOutlet weak var imageView: UIImageView!
func initialize(media: PostImage) {
self.media = media
if let thumbnailUrl = media.thumbnailUrl {
imageView.kf.setImage(with: URL(string: thumbnailUrl))
}
}
override func awakeFromNib() {
super.awakeFromNib()
// Create pinch gesture recognizer to handle zooming
let pinch = UIPinchGestureRecognizer(target: self, action: #selector(self.pinchToZoom(sender:)))
self.imageView.addGestureRecognizer(pinch)
}
/**
Handles pinch zooming.
*/
@objc func pinchToZoom(sender: UIPinchGestureRecognizer) {
if sender.state == .began || sender.state == .changed {
let currentScale = self.imageView.frame.size.width / self.imageView.bounds.size.width
var newScale = currentScale * sender.scale
if newScale < 1 {
newScale = 1
}
if newScale > maxZoom {
newScale = maxZoom
}
let transform = CGAffineTransform(scaleX: newScale, y: newScale)
self.imageView.transform = transform
sender.scale = 1
}
}
}
如您所见,在didEndDisplaying
中,我试图重置单元格的图像视图缩放,因为我具有让用户放大图像的功能。但是由于某种原因,变焦没有被重置,我也不知道为什么。
答案 0 :(得分:1)
您要使一个新的单元出队(仅与cellForItemAt
相关),而不使用提供的单元。将您的代码更改为以下代码,您应该会很好:
func collectionView(_ collectionView: UICollectionView, didEndDisplaying cell: UICollectionViewCell, forItemAt indexPath: IndexPath) {
guard let cell = cell as? LightboxCollectionViewCell else {
fatalError("The cell is not an instance of \(reuseIdentifier).")
}
// Reset the zoom
cell.imageView.transform = CGAffineTransform.identity
}
答案 1 :(得分:0)
您可以在单元格的 prepareForReuse 中设置图像的变换值作为标识,而不必使用 didEndDisplaying 功能。
在LightboxCollectionViewCell文件中,只需添加此功能
var distinctValues = theList.Distinct().ToArray();
for(int i = 0; i < distinctValues.Length; i++)
{
var cnt = theList.Count(e => e == distinctValues[i]);
Console.WriteLine($"Element {distinctValues[i]}, count {cnt}");
}
并从LightboxViewController中删除 didEndDisplaying 函数。