快速不一致的平移手势转换值

时间:2019-03-04 22:47:52

标签: ios swift uipangesturerecognizer uipinchgesturerecognizer

我有一个带有一些文本的UITableViewCell和一个UIImageView。我希望UIImageView具有模仿Instagram的捏和平移识别。我真的很困惑,因为我在两个不同的视图中具有UITableViewCell的相同子类,并且它们的行为也不同。在一个视图中,该单元格是表格第一部分中的唯一单元格,它可以平移和捏住。但是有时在另一种视图中(也许是一半的时间),当我捏和平移UIImageView时,移动的距离和手指移动的速度和距离要远得多。我的手指重新开始在图像上的点向着我平移的方向比我的手指移动了更多,图像迅速离开了屏幕。在我看来,如果在func pan()中,翻译的值比其应有的值高得多,因此view.center.x和view.center.y的增加速度太快了,但是我不确定这是否是问题,或者是什么原因造成的?我无法可靠地重现该问题,因此我无法获取很多信息。我使用this tutorial来创建平移和收缩手势。以下是我的一些代码。任何帮助,将不胜感激。我感到困惑。

UITableViewCell:

var isZooming = false
var originalImageCenter: CGPoint?
var secondaryImageCenter: CGPoint?
var accessibleScale: CGFloat = 1 {
    didSet {
        panScale = accessibleScale
    }
}
var panScale: CGFloat = 1

func configureData(image: UIImage?, secondaryImage: UIImage?, exerciseName: String?, explanation: String?) {
    let pinch = UIPinchGestureRecognizer(target: self, action: #selector(self.pinch(sender:)))
    let secondaryPinch = UIPinchGestureRecognizer(target: self, action: #selector(self.pinch(sender:)))
    self.exerciseImageView.addGestureRecognizer(pinch)
    self.secondaryImageView.addGestureRecognizer(secondaryPinch)


    let pan = UIPanGestureRecognizer(target: self, action: #selector(self.pan(sender:)))
    let secondaryPan = UIPanGestureRecognizer(target: self, action: #selector(self.pan(sender:)))
    pan.delegate = self
    secondaryPan.delegate = self
    self.exerciseImageView.addGestureRecognizer(pan)
    self.secondaryImageView.addGestureRecognizer(secondaryPan)
}

@objc func pinch(sender:UIPinchGestureRecognizer) {
    guard let senderView = sender.view as! UIImageView? else {
        return
    }
    let currentScale = senderView.frame.size.width / senderView.bounds.size.width
    var newScale = currentScale * sender.scale
    accessibleScale = newScale
    if sender.state == .began {
        if newScale > 1 {
            self.isZooming = true
        }
    } else if sender.state == .changed {
        senderView.layer.zPosition = 1
        let pinchCenter = CGPoint(x: sender.location(in: senderView).x - senderView.bounds.midX,
                                  y: sender.location(in: senderView).y - senderView.bounds.midY)
        let transform = senderView.transform.translatedBy(x: pinchCenter.x, y: pinchCenter.y)
            .scaledBy(x: sender.scale, y: sender.scale)
            .translatedBy(x: -pinchCenter.x, y: -pinchCenter.y)
        if newScale < 1 {
            newScale = 1
            let transform = CGAffineTransform(scaleX: newScale, y: newScale)
            senderView.transform = transform
            sender.scale = 1
        } else {
            senderView.transform = transform
            sender.scale = 1
        }
    } else if sender.state == .ended || sender.state == .failed || sender.state == .cancelled {
        if senderView == exerciseImageView {
            guard let center = self.originalImageCenter else {return}
            UIView.animate(withDuration: 0.3, animations: {
                senderView.transform = CGAffineTransform.identity
                senderView.center = center
            }) { (bool) in
                senderView.layer.zPosition = 0
                self.isZooming = false
            }
        } else {
            guard let center = self.secondaryImageCenter else {return}
            UIView.animate(withDuration: 0.3, animations: {
                senderView.transform = CGAffineTransform.identity
                senderView.center = center
            }) { (bool) in
                senderView.layer.zPosition = 0
                self.isZooming = false
            }
        }
    }
}

@objc func pan(sender: UIPanGestureRecognizer) {
    guard let senderView = sender.view as! UIImageView? else {
        return
    }
    print(sender.translation(in: self))
    if self.isZooming && sender.state == .began {
        if sender.view == exerciseImageView {
            self.originalImageCenter = sender.view?.center
        } else {
            self.secondaryImageCenter = 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: senderView.superview)
    }
}

0 个答案:

没有答案