swift 實踐- 14 -- UIScrollView

來源:互聯網
上載者:User

標籤:turn   load   can   max   delegate   frame   resources   最大   self   

import UIKit

 

class ViewController: UIViewController ,UIScrollViewDelegate{

 

    override func viewDidLoad() {

        super.viewDidLoad()

        

        let scrollView = UIScrollView.init()

        scrollView.frame = self.view.bounds

        

        let imageView = UIImageView.init(image: UIImage.init(named: "1"))

        scrollView.contentSize = imageView.bounds.size

        scrollView.addSubview(imageView)

        self.view.addSubview(scrollView)

        

        

        // 手勢縮放大小

        // 要實現放大縮小功能, 需要指定 UIScrollView 的允許縮放的最大和最小比例 (預設都是 1.0)

        // 同時 delegate 屬性指定一個委託類, 委託類要繼承 UIScrollViewDelegate 協議, 並實現 viewForZooming 方法

        

        scrollView.minimumZoomScale = 0.1 //最小比例

        scrollView.maximumZoomScale = 3 //最大比例

        scrollView.delegate = self

        

        

        

    }

    

    func viewForZooming(in scrollView: UIScrollView) -> UIView? {

        for subview : AnyObject in scrollView.subviews {

            if subview.isKind(of: UIImageView.classForCoder()) {

                return subview as? UIView

            }

        }

        return nil

    }

    

    //視圖滾動中一直觸發

    func scrollViewDidScroll(_ scrollView: UIScrollView) {

        print("x:\(scrollView.contentOffset.x) y:\(scrollView.contentOffset.y)")

    }

    

 

    override func didReceiveMemoryWarning() {

        super.didReceiveMemoryWarning()

        // Dispose of any resources that can be recreated.

    }

 

 

}

swift 實踐- 14 -- UIScrollView

相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.