             
             // 2,设置pageControl的个数
             pageControl.numberOfPages = cycleModels?.count ?? 0
+            
+            
+            // 3,默认滚动到中间某一位置
+            let indexPath = IndexPath(item: (cycleModels?.count ?? 0) * 10, section: 0)
+            collectionView.scrollToItem(at: indexPath, at: .left, animated: false)
+            
+            // 4,添加定时器
+            removeCycleTimer()
+            addCycleTimer()
         }
     }
 

class RecommendCycleView: UIView {
    
    // 定义属性
    var cycleModels : [CycleModel]? {
        didSet {
            // 1,刷新collectionView

        let offsetX = scrollView.contentOffset.x + scrollView.bounds.width * 0.5
        
        // 2,计算pageControl的currentIndex
        pageControl.currentPage = Int(offsetX / scrollView.bounds.width) % (cycleModels?.count ?? 1)
    }
    
    func scrollViewWillBeginDragging(_ scrollView: UIScrollView) {
        removeCycleTimer()
    }
    
    func scrollViewDidEndDragging(_ scrollView: UIScrollView, willDecelerate decelerate: Bool) {
        addCycleTimer()
    }
}

// MARK:- 对定时器的操作方法
extension RecommendCycleView {
    fileprivate func addCycleTimer() {
        cycleTimer = Timer(timeInterval: 3.0, target: self, selector: #selector(self.scrollToNext), userInfo: nil, repeats: true)
        RunLoop.main.add(cycleTimer!, forMode: RunLoopMode.commonModes)
    }
    
    fileprivate func removeCycleTimer() {
        cycleTimer?.invalidate() //从运行循环中移除
        cycleTimer = nil
    }
    
    @objc private func scrollToNext() {
        
        //1,获取滚动的偏移量
        let currentOffsetX = collectionView.contentOffset.x
        let offsetX = currentOffsetX + kScreenW
        
        //2,滚动到该位置
        collectionView.setContentOffset(CGPoint(x: offsetX, y: 0), animated: true)
    }
}
