Pinch to Zoom Effect on Uiimageview Inside Scrollview

UIImageView pinch zooming in UIScrollView

You are setting the UIImageView to the original size of the image instead of the size of the UIScrollView that contains it.

- (void)viewDidLoad
{
UIImage *image = [UIImage imageWithData:appDelegate.selectedOriginalImage];
imgView.image = image;

imgView.frame = scrollView.bounds;
[imgView setContentMode:UIViewContentModeScaleAspectFit];
scrollView.contentSize = CGSizeMake(imgView.frame.size.width, imgView.frame.size.height);
scrollView.maximumZoomScale = 4.0;
scrollView.minimumZoomScale = 1.0;
scrollView.delegate = self;
}

remember to return the imgView on the viewToZoom delegate method

Large image in Scrollview pinch zoom not working

Your function signature is wrong:

func viewForZooming(in scrollView: UIScrollView) -> UIView? {
return imageView
}

Note: If you want to be able to scale your pdf image while keeping the vector-based rendering (so it doesn't get blurry when zoomed), you should probably use PDFKit and a PDFView.

Add your myMap.pdf file to your bundle... not to your Asset Catalog.

import UIKit
import PDFKit

class ZoomingPDFViewController: UIViewController {

override func viewDidLoad() {
super.viewDidLoad()

guard let fileURL = Bundle.main.url(forResource: "myMap", withExtension: "pdf") else {
fatalError("Could not load myMap.pdf!")
}

// Add PDFView to view controller.
let pdfView = PDFView(frame: self.view.bounds)
pdfView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
self.view.addSubview(pdfView)

// Load myMap.pdf file from app bundle.
pdfView.document = PDFDocument(url: fileURL)

pdfView.autoScales = true
pdfView.maxScaleFactor = 5.0
pdfView.minScaleFactor = pdfView.scaleFactorForSizeToFit

}

}

Adding Pinch/Zoom effect to a UIImageView inside a UIScrollView

If you are using NSDefaultRunLoopMode, UIAPPlication adds a run loop mode UITrackingRunLoopMode for tracking scrollview events like scrolling. Since the UIApplication switches from NSDefaultRunLoopMode to UITrackingRunLoopMode any events on NSDefaultRunLoopMode will not be called until UIAPPlication switch back to NSDefaultRunLoopMode.

It might be the problem, the fix is change NSDefaultRunLoopMode to NSRunLoopCommonModes .

If you are not sure whether you are using runloop or not as you mentioned in comment. Just search NSDefaultRunLoopMode in your project.

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:

Sample Image

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 and UIScrollView zooming

All you need to do is add your UIImageView (or any view you want to zoom) inside your UIScrollView.

Set your maximumZoomScale on your UIScrollView to any value higher than 1.0f.

Set yourself as the delegate of your UIScrollView and return the UIImageView in the viewForZooming delegate method.

That's it. No pinch gesture needed, no nothing. UIScrollView handles pinch zooming for you.



Related Topics



Leave a reply



Submit