How to Zoom UIScrollView In Swift

 ScrollViewController.swift
var scrollView: UIScrollView!
var imageView: UIImageView!

override func viewDidLoad() {
    super.viewDidLoad()
       
    imageView = UIImageView(image: UIImage(named: "image.png"))
       
    scrollView = UIScrollView(frame: view.bounds)
    scrollView.backgroundColor = UIColor.blackColor()
    scrollView.contentSize = imageView.bounds.size
    scrollView.autoresizingMask = UIViewAutoresizing.FlexibleWidth | UIViewAutoresizing.FlexibleHeight
       
    scrollView.addSubview(imageView)
    view.addSubview(scrollView)
}
scrollView.contentOffset = CGPoint(x: 1000, y: 450)
==============================================================
class ScrollViewController: UIViewController, UIScrollViewDelegate {
func viewForZoomingInScrollView(scrollView: UIScrollView) -> UIView? {
    return imageView
}
    viewDidLoad(){
    scrollView.delegate = self   
    scrollView.minimumZoomScale = 0.1
    scrollView.maximumZoomScale = 4.0
    scrollView.zoomScale = 1.0
       scrollView.delegate = self   
    scrollView.minimumZoomScale = 0.1
    scrollView.maximumZoomScale = 4.0
    scrollView.zoomScale = 1.0
    }


=> Here Cutom Methode Set Zoom Scaling:-

func setZoomScale() {
    let imageViewSize = imageView.bounds.size
    let scrollViewSize = scrollView.bounds.size
    let widthScale = scrollViewSize.width / imageViewSize.width
    let heightScale = scrollViewSize.height / imageViewSize.height
       
    scrollView.minimumZoomScale = min(widthScale, heightScale)
    scrollView.zoomScale = 1.0
}
=> Call This method In ViewDidload:-
setZoomScale()
override func viewWillLayoutSubviews() {
    setZoomScale()
}
=> Here Implemeted UIScrollViewDelegate:-

func scrollViewDidZoom(scrollView: UIScrollView) {
    let imageViewSize = imageView.frame.size
    let scrollViewSize = scrollView.bounds.size
       
    let verticalPadding = imageViewSize.height < scrollViewSize.height ? (scrollViewSize.height - imageViewSize.height) / 2 : 0
    let horizontalPadding = imageViewSize.width < scrollViewSize.width ? (scrollViewSize.width - imageViewSize.width) / 2 : 0
       
    scrollView.contentInset = UIEdgeInsets(top: verticalPadding, left: horizontalPadding, bottom: verticalPadding, right: horizontalPadding)
}
=> Used here Gesture On Double tap:-

 func setupGestureRecognizer() {
    let doubleTap = UITapGestureRecognizer(target: self, action: "handleDoubleTap:")
    doubleTap.numberOfTapsRequired = 2
    scrollView.addGestureRecognizer(doubleTap)
 }
 => Handle Gesture Of Double Tap:-
  
func handleDoubleTap(recognizer: UITapGestureRecognizer) {
       
    if (scrollView.zoomScale > scrollView.minimumZoomScale) {
        scrollView.setZoomScale(scrollView.minimumZoomScale, animated: true)
    } else {
        scrollView.setZoomScale(scrollView.maximumZoomScale, animated: true)
    }
}
=> Call this method In ViewDidLoad:-

setupGestureRecognizer()

Popular posts from this blog

How to Use pagination ScrollView in Swift

UISearchBar search text color , background color swift 3

How To Add Multiple Line in Lable in Swift Ios