     fileprivate var topTitlesView: UIView!
     fileprivate var topScrollView: UIScrollView!
     fileprivate var contentCollectionView: UICollectionView!
+    fileprivate var coverView: UIView!
     
     fileprivate var titles: [String]
     fileprivate var childVC: [UIViewController]
     fileprivate var parentVC: UIViewController
+    
+    fileprivate var titleLabels: [UILabel] = [UILabel]()
+    fileprivate var startOffsetX : CGFloat = 0
+    fileprivate var currentIndex : Int = 0
+    
+    // MARK: 计算属性
+    fileprivate lazy var normalColorRGB : (r : CGFloat, g : CGFloat, b : CGFloat) = self.getRGBWithColor(UIColor(r: 0, g: 0, b: 0))
+    
+    fileprivate lazy var selectedColorRGB : (r : CGFloat, g : CGFloat, b : CGFloat) = self.getRGBWithColor(UIColor(r: 255, g: 0, b: 0))
     
     init(frame: CGRect, titles: [String], childVC: [UIViewController], parentVC: UIViewController) {
         self.titles = titles

     }
    
    func setupTitlesView() {
        topTitlesView = UIView(frame: CGRect(x: 0, y: 0, width: kScreenW, height: kNaviHeight))
        topScrollView = UIScrollView(frame: topTitlesView.bounds)
        topTitlesView.addSubview(topScrollView)
        
        let itemW: CGFloat = 60
        let itemH: CGFloat = kNaviHeight
        topScrollView.contentSize = CGSize(width: itemW*CGFloat(titles.count), height: kNaviHeight)
        topScrollView.showsHorizontalScrollIndicator = false
        addSubview(topTitlesView)
        
        for i in 0..<titles.count {
            let label = UILabel()
            label.frame = CGRect(x: CGFloat(i)*itemW, y: 0, width: itemW, height: itemH)
            label.backgroundColor = UIColor.randomColor()
            label.textAlignment = .center
            label.font = UIFont.systemFont(ofSize: 14)
            label.text = titles[i]
            topScrollView.addSubview(label)
        }
    }
    
    func setupContentView() {

    }
}

extension PageScrollView: UIScrollViewDelegate {

    func scrollViewWillBeginDragging(_ scrollView: UIScrollView) {
        startOffsetX = scrollView.contentOffset.x
    }
    
    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        if scrollView == topScrollView {
            
        } else { // collectionView
            // 1.定义获取需要的数据
            var progress : CGFloat = 0
            var sourceIndex : Int = 0
            var targetIndex : Int = 0
            
            // 2.判断是左滑还是右滑
            let currentOffsetX = scrollView.contentOffset.x
            let scrollViewW = scrollView.bounds.width
            if currentOffsetX > startOffsetX { // 左滑
                // 1 计算 progress
                progress = currentOffsetX / scrollViewW - floor(currentOffsetX / scrollViewW)
                // 2.计算sourceIndex
                sourceIndex = Int(currentOffsetX / scrollViewW)
                // 3.计算targetIndex
                targetIndex = sourceIndex + 1
                if targetIndex >= titleLabels.count {
                    targetIndex = titleLabels.count - 1
                }
                // 4.如果完全划过去
                if currentOffsetX - startOffsetX == scrollViewW {
                    progress = 1
                    targetIndex = sourceIndex
                }
            } else { // 左滑
                // 1.计算progress 
                progress = 1 - (currentOffsetX / scrollViewW - floor(currentOffsetX / scrollViewW))
                // 2.计算targetIndex
                targetIndex = Int(currentOffsetX / scrollViewW)
                // 3.计算sourceIndex
                sourceIndex = targetIndex + 1
                if sourceIndex >= titleLabels.count {
                    sourceIndex = titleLabels.count - 1
                }
            }
            
            // 设置顶部 titleLabel
            // 1.取出sourceLabel/targetLabel
            let sourceLabel = titleLabels[sourceIndex]
            let targetLabel = titleLabels[targetIndex]

            // 3.颜色的渐变(复杂)
            // 3.1.取出变化的范围
            let colorDelta = (selectedColorRGB.r - normalColorRGB.r, selectedColorRGB.g - normalColorRGB.g, selectedColorRGB.b - normalColorRGB.b)
            
            sourceLabel.textColor = UIColor(r: selectedColorRGB.r - colorDelta.0 * progress, g: selectedColorRGB.g - colorDelta.1*progress, b: selectedColorRGB.b - colorDelta.2*progress)
            targetLabel.textColor = UIColor(r: normalColorRGB.r + colorDelta.0*progress, g: normalColorRGB.g + colorDelta.1*progress, b: normalColorRGB.b + colorDelta.2*progress)
            
            // 4.记录最新的index
            currentIndex = targetIndex
            
            let moveTotalX = targetLabel.frame.origin.x - sourceLabel.frame.origin.x
            let moveTotalW = targetLabel.frame.width - sourceLabel.frame.width
            
            coverView.frame.size.width = (sourceLabel.frame.width + 2 * 10 + moveTotalW * progress)
            coverView.frame.origin.x = (sourceLabel.frame.origin.x - 10 + moveTotalX * progress)
        }
    }
}

extension PageScrollView {
    fileprivate func getRGBWithColor(_ color : UIColor) -> (CGFloat, CGFloat, CGFloat) {
        guard let components = color.cgColor.components else {
            fatalError("请使用RGB方式给Title赋值颜色")
        }
        return (components[0] * 255, components[1] * 255, components[2] * 255)
    }
}



