swift 集合视图单元格缩放未重置

axr492tv  于 2022-12-10  发布在  Swift
关注(0)|答案(3)|浏览(162)

我有一个视图控制器,其中包含一个集合视图,如下所示:

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
    }

}

下面是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中,我试图重置单元格的图像视图缩放,因为我有一个函数可以让用户放大图像。但是由于某种原因,缩放没有被重置,我不知道为什么。

xghobddn

xghobddn1#

您正在取消一个新单元格的队列(仅在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
}
yqkkidmi

yqkkidmi2#

您可以在单元格的prepareForReuse中将图像的转换值设置为identity,而不使用didEndDisplaying函数。
在LightboxCollectionViewCell文件中只添加此函数

override func prepareForReuse() {
    super.prepareForReuse()
    self.imageView.transform = CGAffineTransform.identity
 }

并从LightboxViewController中删除didEndDisplaying函数。

4nkexdtk

4nkexdtk3#

willDisplay&didEndDisplaying中重置滚动视图的缩放比例,并且不要对单元格使用双队列。

func collectionView(_ collectionView: UICollectionView, willDisplay cell: UICollectionViewCell, forItemAt indexPath: IndexPath) {
    guard let cell = cell as? CollectionViewCell else {
        fatalError("Error")
    }
    cell.imageContainerScrollView.setZoomScale(0.0, animated: true)
}

func collectionView(_ collectionView: UICollectionView, didEndDisplaying cell: UICollectionViewCell, forItemAt indexPath: IndexPath) {
    guard let cell = cell as? CollectionViewCell else {
        fatalError("Error")
    }
    cell.imageContainerScrollView.setZoomScale(0.0, animated: true)
}

相关问题