Hello,
I am not able to make a Zoom on a Picture. Please help! I saw a couple of tutorials on this topic, but it doesn’t work! There is no proper Tutorial Outside.
Thank you and hopefully, somebody could clarify me, h
Hello,
I am not able to make a Zoom on a Picture. Please help! I saw a couple of tutorials on this topic, but it doesn’t work! There is no proper Tutorial Outside.
Thank you and hopefully, somebody could clarify me, h
Hi @David_Neugebauer Welcome to the community!
Give this whirl:
extension UIImageView {
func enableZoom() {
let pinchGesture = UIPinchGestureRecognizer(target: self, action: #selector(startZooming(_:)))
isUserInteractionEnabled = true
addGestureRecognizer(pinchGesture)
}
@objc private func startZooming(_ sender: UIPinchGestureRecognizer) {
let scaleResult = sender.view?.transform.scaledBy(x: sender.scale, y: sender.scale)
guard let scale = scaleResult, scale.a > 1, scale.d > 1 else { return }
sender.view?.transform = scale
sender.scale = 1
}
}
Blessings,
—Mark
Thank you very much!
BUT:
I paste your code into my ViewController and add an UIPinchGestureRecognizer and an IUImage to the Main.storyboard. But it doesn’t work.
Maybe I should add, I am a bloody beginner.
Hi David,
No worries, we are all beginners here. Good on you for keeping at it.
I added a pict to the assets, added one UIImageView to the StoryBoard and added the image to that.
In the viewDidLoad I called the IImageView on the extension. This is what it looked like.
import UIKit
class ViewController: UIViewController {
@IBOutlet var image: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
image.enableZoom()
}
}
extension UIImageView {
func enableZoom() {
let pinchGesture = UIPinchGestureRecognizer(target: self, action: #selector(startZooming(_:)))
isUserInteractionEnabled = true
addGestureRecognizer(pinchGesture)
}
@objc private func startZooming(_ sender: UIPinchGestureRecognizer) {
let scaleResult = sender.view?.transform.scaledBy(x: sender.scale, y: sender.scale)
guard let scale = scaleResult, scale.a > 1, scale.d > 1 else { return }
sender.view?.transform = scale
sender.scale = 1
}
}
Worked ok on my iPad Mini. Let me know if it does not work for you.
Blessings,
—Mark
Thank you very very much.
Now it´s working. Maybe you could help me by the next step?
I want to enable movement in the picture, like in the Fotos App. I guess it works with Pangesture and touchesMoved, but how I can correctly interconnect them?
Hi David,
Since I am sitting around a lot these days, I am working on that very problem myself.
At the heart of the problem is that a UIImageView can not scroll around, it has to be a UIScrollView. I have yet to get this working in a couple of my apps, but as I get to a a solution, I will be happy to share it with you.
Blessings,
—Mark
I found this tutorial:
It looks really good but nevertheless, it doesn’t work, but maybe it could help you.
David,
Fabulous find! Thank you so much.
Woe, was I making this SO MUCH harder that it needed to be!!
I have some trial and error suggestions after following the tutorial.
First, the zoom method has been updated.
func viewForZooming(in scrollView: UIScrollView) -> UIView? {
return imageView //the name at the variable for you UIImageVIew
}
If you are using storyboard, turn off Content Layout Guides. (see pict) the scrollView does ALL kinds of weird behavior if it is on!
Turn off all the bounces and be sure scrolling is enabled.
Scrap all the code I suggested above. This tutorial is Way more elegant!
Thanks again fo rthe great find. You saved me a zillion hours of frustration!
Great Teamwork