scrollView的偏移和縮放、pageControl(小點)和scrollView的連線

weixin_33670713發表於2016-08-25

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
}

}

相關文章