gpt4 book ai didi

ios - UICollectionView 中的缩放 ImageView 不一致

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

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

我希望图像可以像 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

})
}
}

我希望更了解其工作原理的人可以帮助我。

最佳答案

我找到了一个解决方案,如果有人遇到同样的问题,我会将其留在这里。

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

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

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)

}

关于ios - UICollectionView 中的缩放 ImageView 不一致,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56995752/

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