How To Use UIScrollView In Swift

How To Use UIScrollView In Swift:-

=> ViewController.swift file and add the following properties:-

var imageView: UIImageView!
var scrollView: UIScrollView!
override func viewDidLoad() {    
  super.viewDidLoad()
  // 1  
  imageView = UIImageView(image: UIImage(named: "strandvagen.jpg"))   
  // 2
  scrollView = UIScrollView(frame: view.bounds)
  scrollView.backgroundColor = UIColor.blackColor()
  // 3
  scrollView.contentSize = imageView.bounds.size
  // 4     
  scrollView.addSubview(imageView)
  view.addSubview(scrollView)
}

class ViewController: UIViewController, UIScrollViewDelegate {
@IBOutlet var scrollView: UIScrollView!
var imageView: UIImageView!

override func viewDidLoad() {
  super.viewDidLoad()

  // 1
  let image = UIImage(named: "photo1.png")!
  imageView = UIImageView(image: image)
  imageView.frame = CGRect(origin: CGPoint(x: 0, y: 0), size:image.size)
  scrollView.addSubview(imageView)

  // 2
  scrollView.contentSize = image.size

  // 3
  var doubleTapRecognizer = UITapGestureRecognizer(target: self, action: "scrollViewDoubleTapped:")
  doubleTapRecognizer.numberOfTapsRequired = 2
  doubleTapRecognizer.numberOfTouchesRequired = 1
  scrollView.addGestureRecognizer(doubleTapRecognizer)

  // 4
  let scrollViewFrame = scrollView.frame
  let scaleWidth = scrollViewFrame.size.width / scrollView.contentSize.width
  let scaleHeight = scrollViewFrame.size.height / scrollView.contentSize.height
  let minScale = min(scaleWidth, scaleHeight);
  scrollView.minimumZoomScale = minScale;

  // 5
  scrollView.maximumZoomScale = 1.0
  scrollView.zoomScale = minScale;

  // 6
  centerScrollViewContents()
}

func centerScrollViewContents() {
  let boundsSize = scrollView.bounds.size
  var contentsFrame = imageView.frame

  if contentsFrame.size.width < boundsSize.width {
    contentsFrame.origin.x = (boundsSize.width - contentsFrame.size.width) / 2.0
  } else {
    contentsFrame.origin.x = 0.0
  }

  if contentsFrame.size.height < boundsSize.height {
    contentsFrame.origin.y = (boundsSize.height - contentsFrame.size.height) / 2.0
  } else {
    contentsFrame.origin.y = 0.0
  }

  imageView.frame = contentsFrame
}
 func scrollViewDoubleTapped(recognizer: UITapGestureRecognizer) {
  // 1       
  let pointInView = recognizer.locationInView(imageView)

  // 2
  var newZoomScale = scrollView.zoomScale * 1.5
  newZoomScale = min(newZoomScale, scrollView.maximumZoomScale)

  // 3
  let scrollViewSize = scrollView.bounds.size
  let w = scrollViewSize.width / newZoomScale
  let h = scrollViewSize.height / newZoomScale
  let x = pointInView.x - (w / 2.0)
  let y = pointInView.y - (h / 2.0)

  let rectToZoomTo = CGRectMake(x, y, w, h);

  // 4
  scrollView.zoomToRect(rectToZoomTo, animated: true)
}
func viewForZoomingInScrollView(scrollView: UIScrollView!) -> UIView! {
  return imageView
}
func scrollViewDidZoom(scrollView: UIScrollView!) {
  centerScrollViewContents()
}

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