Swift - 滚动视图(UIScrollView)的用法
2016-07-29 本文已影响291人
MangoJ
import UIKit
class ViewController: UIViewController ,UIScrollViewDelegate{
override func viewDidLoad() {
super.viewDidLoad()
var SCREEN_WIDTH = self.view.frame.width
var SCREEN_HIGHT = self.view.frame.height
let scrollView = UIScrollView()
scrollView.frame = CGRectMake(0, 260, SCREEN_WIDTH, 100)
let imageView = UIImageView(image: UIImage(named: "hello"))
scrollView.contentSize = imageView.bounds.size
scrollView.addSubview(imageView)
scrollView.delegate = self
scrollView.showsVerticalScrollIndicator = false
scrollView.showsHorizontalScrollIndicator = false
//设置最大最小缩放比
scrollView.minimumZoomScale = 0.1
scrollView.maximumZoomScale = 3
self.view.addSubview(scrollView)
}
func scrollViewDidScroll(scrollView: UIScrollView) {
print("x:\(scrollView.contentOffset.x) y:\(scrollView.contentOffset.y)")
}
//手势放大缩小
//要实现放大缩小功能,需要指定UIScrollView的允许缩放最大比例和最小比例(默认都是是1.0)。
// 同时delegate属性指定一个委托类,委托类要继承UIScrollViewDelegate协议,并在委托类中实现viewForZoomingInScrollView方法。
func viewForZoomingInScrollView(scrollView: UIScrollView) -> UIView? {
for subview : AnyObject in scrollView.subviews {
if subview.isKindOfClass(UIImageView) {
return subview as? UIView
}
}
return nil
}
}