UIImageView pinch zoom swift
I decided to add the imageView to a UIScrollView. It allows the user to zoom and pan over. Here is the code I used.
in order to set max/min zoom I used :
scrollImg.minimumZoomScale = 1.0
scrollImg.maximumZoomScale = 10.0
here is the rest of the code.
var vWidth = self.view.frame.width
var vHeight = self.view.frame.height
var scrollImg: UIScrollView = UIScrollView()
scrollImg.delegate = self
scrollImg.frame = CGRectMake(0, 0, vWidth!, vHeight!)
scrollImg.backgroundColor = UIColor(red: 90, green: 90, blue: 90, alpha: 0.90)
scrollImg.alwaysBounceVertical = false
scrollImg.alwaysBounceHorizontal = false
scrollImg.showsVerticalScrollIndicator = true
scrollImg.flashScrollIndicators()
scrollImg.minimumZoomScale = 1.0
scrollImg.maximumZoomScale = 10.0
defaultView!.addSubview(scrollImg)
imageView!.layer.cornerRadius = 11.0
imageView!.clipsToBounds = false
scrollImg.addSubview(imageView!)
I also had to add this as well
func viewForZoomingInScrollView(scrollView: UIScrollView) -> UIView? {
return self.imageView
}
Swift 3 & above function prototype
func viewForZooming(in scrollView: UIScrollView) -> UIView? {
return self.mainImage
}
UIImageView Pinch Zoom and Drag swift
You can get that working by implementing UIScrollViewDelegate's
scrollViewDidZoom(_:)
method, i.e.
func scrollViewDidZoom(_ scrollView: UIScrollView) {
if scrollView.zoomScale > scrollView.maximumZoomScale {
scrollView.zoomScale = scrollView.maximumZoomScale
} else if scrollView.zoomScale < scrollView.minimumZoomScale {
scrollView.zoomScale = scrollView.minimumZoomScale
}
}
In the above code, check if the scrollView's
zoomScale
lies within maximumZoomScale
and minimumZoomScale
. If it doesn't, update it as per the limits allowed.
Pinch to Zoom UIImageView
Couple reasons your code is not working like the tutorial.
1 - You missed setting the scroll view delegate (unless you set it in Storyboard). If you did not set it in Storyboard:
override func viewDidLoad() {
super.viewDidLoad()
if let image = selectedImage {
imageView.image = image
}
// add this
scrollView.delegate = self
}
2 - It will still not be quite correct, because the tutorial sets the image in Storyboard, but you're setting it in viewDidLoad()
. To fix that:
// remove this
//override func viewWillLayoutSubviews() {
// super.viewWillLayoutSubviews()
// updateMinZoomScaleForSize(view.bounds.size)
//}
// add this
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
updateMinZoomScaleForSize(scrollView.bounds.size)
updateConstraintsForSize(scrollView.bounds.size)
}
3 - To get rid of the constraint errors in your Storyboard, give the image view Width and Height constraints (such as 100 each), and set them as Placeholders so they will not be used at run-time:
Zoom & Pin on the same UIImageView
OK, I solved the problem.
Turns out UITapGestureRecognizer
also has .location(in:)
function.
For those who are experiencing the same problem, I add the working code below.
let longPressRecognizer = UILongPressGestureRecognizer(target: self, action: #selector(longPressed))
longPressRecognizer.minimumPressDuration = 0.5
longPressRecognizer.delaysTouchesBegan = true
longPressRecognizer.delegate = self
self.imageView.addGestureRecognizer(longPressRecognizer)
@objc func longPressed(sender: UILongPressGestureRecognizer) {
if sender.state == UIGestureRecognizer.State.ended {
let location = sender.location(in: self.imageView)
let touchedRect = CGRect(x: location.x - 10, y: location.y - 10, width: 20, height: 20)
let touchedInCourt = touchedRect.intersects(self.imageView.bounds)
switch touchedInCourt {
case true:
if self.imageView.subviews.count > 0 {
var flagForIntersect = false
for subview in self.imageView.subviews {
let subviewFrame = subview.frame
if subviewFrame.intersects(touchedRect) {
subview.removeFromSuperview()
flagForIntersect = true
}
}
if !flagForIntersect {
addDot(withLocation: location, toPhoto: self.imageView)
}
} else {
addDot(withLocation: location, toPhoto: self.imageView)
}
case false: break
}
}
}
I used UILongPressGestureRecognizer
but UITapGestureRecognizer
works as well.
Xcode Swift image zoom gesture
Use UIScrollView and add UIImgeView in scroll view
import UIKit
class ViewController: UIViewController,UIScrollViewDelegate
{
var scrollV : UIScrollView!
var imageView : UIImageView!
override func viewDidLoad()
{
super.viewDidLoad()
self.navigationController?.navigationBarHidden = true
scrollV=UIScrollView()
scrollV.frame = CGRectMake(0, 0, self.view.frame.width, self.view.frame.height)
scrollV.minimumZoomScale=1
scrollV.maximumZoomScale=3
scrollV.bounces=false
scrollV.delegate=self;
self.view.addSubview(scrollV)
imageView=UIImageView()
imageView.image = UIImage(imageLiteral: "neymar.jpg")
imageView.frame = CGRectMake(0, 0, scrollV.frame.width, scrollV.frame.height)
imageView.backgroundColor = .blackColor()
imageView.contentMode = .ScaleToFill
scrollV.addSubview(imageView)
}
func viewForZoomingInScrollView(scrollView: UIScrollView) -> UIView?
{
return imageView
}
}
Related Topics
How to Make a Swiftui List Scroll Automatically
Examples of Delegates in Swift
Swift 5.0: 'Withunsafebytes' Is Deprecated: Use 'Withunsafebytes≪R≫(...)
Share Data Between Main App and Widget in Swiftui For iOS 14
Photopicker Discovery Error: Error Domain=Pluginkit Code=13
How to Generate a Random Number in a Range (10...20) Using Swift
Accessing Code in Swift 3 Error
Closure With Generic Parameters
How to Remove Diacritics from a String in Swift
What Is the Cause of This Type Error
Lidar and Realitykit - Capture a Real World Texture For a Scanned Model
Storing Values in Completionhandlers - Swift
Load Local Web Files & Resources in Wkwebview
Segue from Modal View to Tab Bar View Controller and Not Lose Tab Bar
What Is the Point of Having Two Different Names For the Same Parameter