1

I implement pinch operation iOS 8.1 with Swift. I reference the following links but the image shakes up, down, left and right by little and little.

UIPinchGestureRecognizer. Make zoom in location of fingers, not only center

UIPinchGestureRecognizer position the pinched view between the two fingers

It is my code in UIViewController and sender.view is UIImageView.

func pinchedView(sender:UIPinchGestureRecognizer) {

    if sender.numberOfTouches() < 2 {
        return
    }

    if sender.state == UIGestureRecognizerState.Began {
        lastScale = 1.0
        lastPoint = sender.locationInView(sender.view)
        return
    }

    let scale = 1.0 - (lastScale - sender.scale)
    sender.view!.transform = CGAffineTransformScale(sender.view!.transform, scale, scale)

    let newPoint = sender.locationInView(sender.view)
    var translation = CGPoint()
    translation.x = newPoint.x - lastPoint.x
    translation.y = newPoint.y - lastPoint.y
    sender.view!.transform = CGAffineTransformTranslate(sender.view!.transform, translation.x, translation.y)

    lastPoint = sender.locationInView(sender.view)
    lastScale = sender.scale
}

Why does UIImageView shake?

Community
  • 1
  • 1
Yuhei
  • 11
  • 2

1 Answers1

0

Instead of

sender.locationInView(sender.view)

try

sender.locationOfTouch(0, inView: sender.view)

locationInView seems to be picking an undefined touch.

richy
  • 2,716
  • 1
  • 33
  • 42