scrollView的偏移和缩放、pageControl(小点)
2016-08-25 本文已影响290人
Dove_Q
cg```swift
class ViewController: UIViewController, UIScrollViewDelegate {
var scrollView: UIScrollView!
var redView: UIView!
var pageControl: UIPageControl!
override func viewDidLoad() {
super.viewDidLoad()
//创建scrollView
scrollView = UIScrollView(frame: CGRect(x: 0, y: 100, width: self.view.frame.size.width, height: 200))
scrollView.backgroundColor = UIColor.cyanColor()
self.view.addSubview(scrollView)
//在scrollview上添加两个标记View
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.redColor()
scrollView.addSubview(greenView)
//UIScrollView能否滚动
//1. scrollEnabled
//2. contentSize: (0, 0) 内容大小,只有当内容尺寸超过本身尺寸时
scrollView.contentSize = CGSize(width: scrollView.frame.size.width * 3, height: scrollView.frame.size.height)
//设置内容距四周的距离(边框大小)
scrollView.contentInset = UIEdgeInsetsMake(10, 0, 10, 0)
//设置偏移量(向左、向上滚动为正)
scrollView.contentOffset = CGPoint(x: 100, y: 0)
//方向锁, 一次只允许一个方向滚动
scrollView.directionalLockEnabled = true
//弹性效果
scrollView.bounces = false
//水平滚动条
scrollView.showsVerticalScrollIndicator = false
//垂直滚动条
scrollView.showsHorizontalScrollIndicator = false
scrollView.delegate = self
//最小缩放倍数
scrollView.minimumZoomScale = 1
//最大缩放倍数
scrollView.maximumZoomScale = 5
//小点处于控件中央
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()
self.view.addSubview(pageControl)
//给小点添加点击事件,将点击小点的事件连接到scrollView上
pageControl.addTarget(self, action: #selector(didChange(_:)), forControlEvents: .ValueChanged)
}
func didChange(sender: UIPageControl){
let offset = CGPoint(x: scrollView.bounds.size.width * CGFloat(sender.currentPage), y: 0)
//1. 没有动画效果
// scrollView.contentOffset = offset
//2. 有动画效果
// scrollView.setContentOffset(offset, animated: true)
//3. 将一个矩形区域设为可见
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) {
//只要有位移就会持续调用
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("停止减速")
//把scrollView的偏移情况在小点上面显示
pageControl.currentPage = Int(scrollView.contentOffset.x / scrollView.bounds.size.width)
}
//设置scrollview上要缩放的View, 返回值为View
func viewForZoomingInScrollView(scrollView: UIScrollView) -> UIView? {
return redView
}
}