UICollectionView 中的 Zoom ImageView 不一致

Zoom ImageView in UICollectionView inconsistent

我有一个 UICollectionView,其单元格的大小与 CollectionView 本身的大小相同,因此启用了分页功能,您可以像 基本图库应用程序一样滑动[=26] =].

我希望图像可以像在 Instagram 上那样缩放(在手势结束时快速返回)。我在一篇文章的帮助下实现了捏合和平移手势,它确实有效。

但是我遇到了一个奇怪的问题。它缩放的速度非常不一致,而且似乎每翻一页都变得更快。因此,第一个页面的缩放和平移非常正常,第二个页面要快得多,第三个页面就很荒谬了。 我很困惑,因为我在分页 ScrollView 上有这个确切的代码 运行,而且它工作得很好。

这里是UICollectionViewCell中的相关代码。

func setup(){
    imageView.isUserInteractionEnabled = true

    let pinch = UIPinchGestureRecognizer(target: self, action: 
#selector(pinch(sender:)))
    pinch.delegate = self
    imageView.addGestureRecognizer(pinch)

    let pan = UIPanGestureRecognizer(target: self, action: 
#selector(pan(sender:)))
    pan.delegate = self
    imageView.addGestureRecognizer(pan)

    let tap = UITapGestureRecognizer(target: self, action: 
#selector(handleTap))
    imageView.addGestureRecognizer(tap)

    imageView.clipsToBounds = false
}


 @objc func pan(sender: UIPanGestureRecognizer) {
    if(isZooming){
        if self.isZooming && sender.state == .began {
            self.originalImageCenter = sender.view?.center
        } else if self.isZooming && sender.state == .changed {
            let translation = sender.translation(in: self)
            if let view = sender.view {
                view.center = CGPoint(x:view.center.x + 
                                      translation.x,
                                      y:view.center.y + 
                                      translation.y)
            }
            sender.setTranslation(CGPoint.zero, in: 
self.imageView.superview)
        }
    }else{
        print("blocked pan")
    }

}

@objc func pinch(sender:UIPinchGestureRecognizer) {
    NotificationCenter.default.post(name: 
Notification.Name(rawValue: "disablePage"), object: nil)

    allowedToTap = false

    if sender.state == .began {
        let currentScale = self.imageView.frame.size.width / 
self.imageView.bounds.size.width
        let newScale = currentScale*sender.scale
        if newScale > 1 {
            self.isZooming = true
        }
    } else if sender.state == .changed {
        guard let view = sender.view else {return}
        let pinchCenter = CGPoint(x: sender.location(in: view).x - 
                                  view.bounds.midX,
                                  y: sender.location(in: view).y - 
                                  view.bounds.midY)
        let transform = view.transform.translatedBy(x: 
pinchCenter.x, y: pinchCenter.y)
            .scaledBy(x: sender.scale, y: sender.scale)
            .translatedBy(x: -pinchCenter.x, y: -pinchCenter.y)
        let currentScale = self.imageView.frame.size.width / 
self.imageView.bounds.size.width
        var newScale = currentScale*sender.scale
        if newScale < 1 {
            newScale = 1
            let transform = CGAffineTransform(scaleX: newScale, y: 
newScale)
            self.imageView.transform = transform
            sender.scale = 1
        }else {
            view.transform = transform
            sender.scale = 1
        }
    } else if sender.state == .ended || sender.state == .failed || 
 sender.state == .cancelled {
        guard let center = self.originalImageCenter else {return}
        UIView.animate(withDuration: 0.3, animations: {
            self.imageView.transform = CGAffineTransform.identity
            self.imageView.center = center
        }, completion: { _ in
            self.isZooming = false

            NotificationCenter.default.post(name: 
Notification.Name(rawValue: "enablePage"), object: nil)
            self.allowedToTap = true

        })
    }
}

我希望对它的工作原理有更多了解的人可以帮助我。

我找到了解决方案,我会把它留在这里,以防有人遇到同样的问题。

问题是设置函数被多次调用,因此添加了多个相同的 GestureRecognizers。

所以我刚刚添加了一个快速检查。

func setup(){
    imageView.isUserInteractionEnabled = true
    playPauseImageView.alpha = 0

    if(!gesturesAreAttached){
        gesturesAreAttached = true
        addZoom()
    }
    imageView.clipsToBounds = false
}

func addZoom(){
    print("adding Zoom")
    let pinch = UIPinchGestureRecognizer(target: self, action: #selector(pinch(sender:)))
    pinch.delegate = self
    imageView.addGestureRecognizer(pinch)

    let pan = UIPanGestureRecognizer(target: self, action: #selector(pan(sender:)))
    pan.delegate = self
    imageView.addGestureRecognizer(pan)

    let tap = UITapGestureRecognizer(target: self, action: #selector(handleTap))
    imageView.addGestureRecognizer(tap)

}