我有一个UICollectionView
,其单元格的大小为CollectionView
本身,因此启用了分页,并且您可以像 basic Gallery应用程序一样滑动。 >
我希望图像像在 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
})
}
}
我希望对此工作原理有更多了解的人能对我有所帮助。
答案 0 :(得分:0)
我找到了解决方案,如果有人遇到同样的问题,我将其留在这里。
问题在于设置函数被多次调用,因此添加了多个相同的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)
}