khenid

Untitled

Feb 6th, 2015
368
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //
  2. //  CustomScrollViewController.swift
  3. //  ScrollViews
  4. //
  5. //  Created by Anil on 05/02/15.
  6. //  Copyright (c) 2015 Variya Soft Solutions. All rights reserved.
  7. //
  8.  
  9. import UIKit
  10.  
  11. class CustomScrollViewController: UIViewController, UIScrollViewDelegate {
  12.  
  13.     @IBOutlet var scrollView: UIScrollView!
  14.     var containerView: UIView!
  15.    
  16.     override func viewDidLoad() {
  17.         super.viewDidLoad()
  18.  
  19.         // Set up the container view to hold your custom view hierarchy
  20.         let containerSize = CGSize(width: 640.0, height: 640.0)
  21.         containerView = UIView(frame: CGRect(origin: CGPoint(x: 0, y: 0), size:containerSize))
  22.         scrollView.addSubview(containerView)
  23.        
  24.         // Set up your custom view hierarchy
  25.         let redView = UIView(frame: CGRect(x: 0, y: 0, width: 640, height: 80))
  26.         redView.backgroundColor = UIColor.redColor();
  27.         containerView.addSubview(redView)
  28.        
  29.         let blueView = UIView(frame: CGRect(x: 0, y: 560, width: 640, height: 80))
  30.         blueView.backgroundColor = UIColor.blueColor();
  31.         containerView.addSubview(blueView)
  32.        
  33.         let greenView = UIView(frame: CGRect(x: 160, y: 160, width: 320, height: 320))
  34.         greenView.backgroundColor = UIColor.greenColor();
  35.         containerView.addSubview(greenView)
  36.        
  37.         let imageView = UIImageView(image: UIImage(named: "slow.png"))
  38.         imageView.center = CGPoint(x: 320, y: 320);
  39.         containerView.addSubview(imageView)
  40.        
  41.         // Tell the scroll view the size of the contents
  42.         scrollView.contentSize = containerSize;
  43.        
  44.         // Set up the minimum & maximum zoom scales
  45.         let scrollViewFrame = scrollView.frame
  46.         let scaleWidth = scrollViewFrame.size.width / scrollView.contentSize.width
  47.         let scaleHeight = scrollViewFrame.size.height / scrollView.contentSize.height
  48.         let minScale = min(scaleWidth, scaleHeight)
  49.        
  50.         scrollView.minimumZoomScale = minScale
  51.         scrollView.maximumZoomScale = 1.0
  52.         scrollView.zoomScale = 1.0
  53.        
  54.         centerScrollViewContents()
  55.        
  56.     }
  57.    
  58.     func centerScrollViewContents() {
  59.         let boundsSize = scrollView.bounds.size
  60.         var contentsFrame = containerView.frame
  61.        
  62.         if contentsFrame.size.width < boundsSize.width {
  63.             contentsFrame.origin.x = (boundsSize.width - contentsFrame.size.width) / 2.0
  64.         } else {
  65.             contentsFrame.origin.x = 0.0
  66.         }
  67.        
  68.         if contentsFrame.size.height < boundsSize.height {
  69.             contentsFrame.origin.y = (boundsSize.height - contentsFrame.size.height) / 2.0
  70.         } else {
  71.             contentsFrame.origin.y = 0.0
  72.         }
  73.        
  74.         containerView.frame = contentsFrame
  75.     }
  76.    
  77.     func viewForZoomingInScrollView(scrollView: UIScrollView!) -> UIView! {
  78.         return containerView
  79.     }
  80.    
  81.     func scrollViewDidZoom(scrollView: UIScrollView!) {
  82.         centerScrollViewContents()
  83.     }
  84. }
Advertisement
Add Comment
Please, Sign In to add comment