首页 > 代码库 > swift 实践- 14 -- UIScrollView
swift 实践- 14 -- UIScrollView
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