Wednesday, January 14, 2015

UIScrollView - Zoom in/zoom out and scroll/move an image with ScrollView

1. Drag a UIScrollView to the storyboard.

2. Control-drag the UIScrollView to

    @IBOutlet weak var myUIScrollView: UIScrollView!


3. Complete the code as below:

import UIKit

class ViewController: UIViewController, UIScrollViewDelegate {

    @IBOutlet weak var myUIScrollView: UIScrollView!
    
    var myUIImageView: UIImageView!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.myUIScrollView.maximumZoomScale = 5.0
        self.myUIScrollView.minimumZoomScale = 0.5
        self.myUIScrollView.delegate = self
        
        myUIImageView = UIImageView(image: UIImage(named: "big_image.png"))
        self.myUIScrollView.addSubview(myUIImageView)
    }

    func viewForZoomingInScrollView(scrollView: UIScrollView) -> UIView? {
        return myUIImageView

    }

No comments:

Post a Comment