用途非常广泛,基本上能滚动的都是ScrollView,网页,QQ,微信等......
import UIKit
class ViewController: UIViewController, UIScrollViewDelegate {
var scrollView: UIScrollView!
var pageControl: UIPageControl!
var redView: UIView!
override func viewDidLoad() {
super.viewDidLoad()
//UIScrollView
// let textView = UITextView(frame: CGRect(x: 100, y: 100, width: 200, height: 100))
// textView.backgroundColor = UIColor.redColor()
// self.view.addSubview(textView)
//1. 滚动
//2. 缩放
scrollView = UIScrollView()
scrollView.frame = CGRectMake(0, 100, self.view.bounds.size.width, 200)
scrollView.backgroundColor = UIColor.cyanColor()
self.view.addSubview(scrollView)
redView = UIView(frame: CGRect(x: 0, y: 0, width: 50, height: 50))
redView.backgroundColor = UIColor.redColor()
scrollView.addSubview(redView)
let greenView = UIView(frame: CGRect(x: scrollView.frame.size.width - 50, y: scrollView.frame.size.height - 50, width: 50, height: 50))
greenView.backgroundColor = UIColor.greenColor()
scrollView.addSubview(greenView)
//UIScrollView能否滚动
//1. scrollEnabled
//2. contentSize: (0, 0) 内容大小,只有当内容尺寸超过本身尺寸时才滚动
// print(scrollView.scrollEnabled)
scrollView.contentSize = CGSize(width: scrollView.frame.size.width * 3, height: scrollView.frame.size.height)
scrollView.pagingEnabled = true
scrollView.delegate = self
// 缩放
scrollView.maximumZoomScale = 10
scrollView.minimumZoomScale = 0.5
// scrollView.contentInset = UIEdgeInsets(top: 10, left: 0, bottom: 10, right: 0)
//向左/向上滚动:正方向
// scrollView.contentOffset = CGPoint(x: -100, y: 0)
//一次只允许一个方向滚动
// scrollView.directionalLockEnabled = true
//弹性效果
// scrollView.bounces = false
// 禁止滚动回弹效果
// scrollView.showsHorizontalScrollIndicator = false
// scrollView.showsVerticalScrollIndicator = false
//小点处于控件中央
pageControl = UIPageControl(frame: CGRect(x: 0, y: scrollView.frame.origin.y + scrollView.frame.size.height - 20, width: scrollView.frame.size.width, height: 20))
pageControl.numberOfPages = 3
pageControl.currentPageIndicatorTintColor = UIColor.blueColor() //当前页蓝色小点
pageControl.pageIndicatorTintColor = UIColor.yellowColor() //其他页黄色小点
pageControl.addTarget(self, action: #selector(didClicked(_:)), forControlEvents: .ValueChanged)
self.view.addSubview(pageControl)
}
func didClicked(sender: UIPageControl) {
print(sender.currentPage)
// scrollView.contentOffset = CGPoint(x: scrollView.bounds.size.width * CGFloat(sender.currentPage), y: 0)
let offset = CGPoint(x: scrollView.bounds.size.width * CGFloat(sender.currentPage), y: 0)
// scrollView.setContentOffset(offset, animated: true)
let rect = CGRect(x: offset.x, y: 0, width: scrollView.bounds.size.width, height: scrollView.bounds.size.height)
scrollView.scrollRectToVisible(rect, animated: true)
}
func scrollViewDidScroll(scrollView: UIScrollView) {
//1. 只要有位移就会持续调用
print(scrollView.contentOffset)
}
func scrollViewWillBeginDragging(scrollView: UIScrollView) {
print("begin dragging 开始拖动")
}
// 点击拖动,停下后才松开,则调用"结束拖动",如果在拖动过程中松开,则调用"减速"
func scrollViewWillEndDragging(scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>) {
print("结束拖动")
}
func scrollViewDidEndDragging(scrollView: UIScrollView, willDecelerate decelerate: Bool) {
if decelerate {
print("开始减速")
}
else {
print("无需减速")
pageControl.currentPage = Int(scrollView.contentOffset.x / scrollView.bounds.size.width)
}
}
func scrollViewDidEndDecelerating(scrollView: UIScrollView) {
print("停止减速")
pageControl.currentPage = Int(scrollView.contentOffset.x / scrollView.bounds.size.width)
}
func changePage() {
}
func viewForZoomingInScrollView(scrollView: UIScrollView) -> UIView? {
return redView
}
}
编译运行结果如下:
双手开合实现缩放:
可以试着把图片加入到里面,图片设置的位置加在ScrollView上,宽高和ScrollView一样。