0
votes

I'm doing a page view controller with a photo album, and since I haven't been able to find a way to pan the image zoomed without changing the picture, I want the image to return to its original size when the user stops pinching.

This is my code:

  @objc func pinch(sender:UIPinchGestureRecognizer) {
    if sender.state == .began || sender.state == .changed {
        let currentScale = self.contentImageView.frame.size.width / self.contentImageView.bounds.size.width
        let newScale = currentScale*sender.scale
        let transform = CGAffineTransform(scaleX: newScale, y: newScale)
        self.contentImageView.transform = transform
        sender.scale = 1

       if sender.state == .ended {
            contentImageView.transform = CGAffineTransform.identity
        }

    }
}

The zooming part works fine, but when I release the pinch it doesn't do anything, any suggestions would be very appreciated.

1

1 Answers

1
votes

From your code, you never get reset:

  if sender.state == .ended {
        self.contentImageView.transform = CGAffineTransform.identity
  }

Because you try to validate it inside of your first if. Try to change it like this:

if sender.state == .began || sender.state == .changed { ... } else if sender.state == .ended { self.contentImageView.transform = CGAffineTransform.identity }