標籤: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