I have an UIImageView containing a single image. In storyboard I placed a pan gesture recognizer on the image view. It works, however, I can only reposition the entire UIImageView and not just the image. In the example below I want the red frame to remain the same, always. I only want to reposition the underlying image.
The code for my pinch gesture recognizer is obviously transforming the imageView which is the problem. But I don't see a way to attach the gesture recognizers to the image itself. Or if that's even what I should be trying to do...
@IBAction func scaleImage(_ sender: UIPinchGestureRecognizer) {
imageView.transform = CGAffineTransform(scaleX: sender.scale, y: sender.scale)
}
When you're saying
reposition the entire UIImageView and not just the image
it sounds like you think that there's some view container for image inside UIImageView
.
But it's not true. UIImageView
is already the lowest level you can get. There's a layer
underneath, but it's bound to the view.
When you're changing transform of UIImageView
this affects gestures calculation, that's the reason of your problems.
I suggest you wrapping UIImageView
with UIView
and adding gesture recognizer for this container view.
Also depending on your needs, the simplest solution may be placing image inside a UIScrollView
and enabling zoom, this will allow user both to zoom and scroll through image.