Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
Help us understand the problem. What is going on with this article?

画像をダブルタップとピンチイン・ピンチアウトで拡大・縮小する

More than 5 years have passed since last update.

アプリ画像

アプリ内容

  • 拡大・縮小の範囲は8倍〜1倍
  • ダブルタップ時は3倍ずつ拡大して、上限を超えたら1倍に戻る。

このサンプル画像めっちゃ面白い。
「詳細! Objective-C iPhoneアプリ開発 入門ノート」という書籍のChapter6-4のサンプルアプリをSwiftにしました。そこで使われている画像です。

コード

ViewController.swift
class ViewController: UIViewController , UIScrollViewDelegate{

    @IBOutlet var myImageView: UIImageView!
    @IBOutlet var myScrollView: UIScrollView!

    override func viewDidLoad() {
        super.viewDidLoad()

        // スクロールビューの設定
        self.myScrollView.delegate = self
        self.myScrollView.minimumZoomScale = 1
        self.myScrollView.maximumZoomScale = 8
        self.myScrollView.scrollEnabled = true
        self.myScrollView.showsHorizontalScrollIndicator = true
        self.myScrollView.showsVerticalScrollIndicator = true

        var doubleTapGesture: UITapGestureRecognizer = UITapGestureRecognizer(target: self
            , action:"doubleTap:")
        doubleTapGesture.numberOfTapsRequired = 2
        self.myImageView.userInteractionEnabled = true
        self.myImageView.addGestureRecognizer(doubleTapGesture)
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }

    // ピンチイン・ピンチアウト
    func viewForZoomingInScrollView(scrollView: UIScrollView) -> UIView? {
        print("pinch")
        return self.myImageView
    }
    // ダブルタップ
    func doubleTap(gesture: UITapGestureRecognizer) -> Void {

        print(self.myScrollView.zoomScale)
        if ( self.myScrollView.zoomScale < self.myScrollView.maximumZoomScale ) {

            var newScale:CGFloat = self.myScrollView.zoomScale * 3
            var zoomRect:CGRect = self.zoomRectForScale(newScale, center: gesture.locationInView(gesture.view))
            self.myScrollView.zoomToRect(zoomRect, animated: true)

        } else {
            self.myScrollView.setZoomScale(1.0, animated: true)
        }
    }
    // 領域
    func zoomRectForScale(scale:CGFloat, center: CGPoint) -> CGRect{
        var zoomRect: CGRect = CGRect()
        zoomRect.size.height = self.myScrollView.frame.size.height / scale
        zoomRect.size.width = self.myScrollView.frame.size.width / scale

        zoomRect.origin.x = center.x - zoomRect.size.width / 2.0
        zoomRect.origin.y = center.y - zoomRect.size.height / 2.0

        return zoomRect
    }
}

ソースアップ

https://github.com/hanoopy/ios_study_pinchapp.git

コメント

hanoopy
PHPプログラマー。 iPhone開発勉強中。
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away