LoginSignup
17
17

More than 5 years have passed since last update.

UIScrollViewとUIImageViewで画像を拡大、縮小できるようにする。

Last updated at Posted at 2015-08-02

 できあがるもの

Gyazo

「写真」のプレビュー表示です。(ほんとにこれだけ。)

作り方

  1. Storyboard or xibでviewにuiscrollview,uiimageviewをのせます。
  2. uiscrollviewのContentmodeをCenterにします。(←これが一番大事)Constrainsを適当に決めます。
  3. uiimageviewのConstrainsにCenterX,Yを追加します。scrollviewとの位置関係も適当に決めます。
    こんなかんじ↓
    Screen Shot 2015-08-02 at 20.48.13.png

  4. viewControllerはこんなかんじ↓

import UIKit

class ViewController: UIViewController, UIScrollViewDelegate {

    @IBOutlet weak var scrollView: UIScrollView!
    @IBOutlet weak var imageView: UIImageView!
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }

    override func viewDidAppear(animated: Bool) {
        scrollView.delegate = self
        scrollView.maximumZoomScale = 8.0
        scrollView.minimumZoomScale = 1.0
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


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

}

5.完成です。

17
17
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
17
17