UICollectionView水平自动滚动单元

时间:2018-07-30 22:35:46

标签: ios swift xcode uicollectionview

我有5个要自动滚动的集合视图单元格,我尝试过的所有代码似乎都不适用于我的项目。我究竟做错了什么?

var scrollingTimer = Timer()

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return DataServices.instance.getClView().count
}

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    if let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "InsideCollectionViewCell", for: indexPath) as? InsideCollectionViewCell{

    let promo = DataServices.instance.getClView()[indexPath.row]


        cell.updateViews(clView: promo)
        return cell
    }

     var rowIndex = indexPath.row

    let numberOfRecords: Int = DataServices.instance.getClView().count - 1
    if (rowIndex < numberOfRecords){
        rowIndex = (rowIndex + 1)
    }else{
        rowIndex = 0
    }

    scrollingTimer = Timer.scheduledTimer(timeInterval: 2.0, target: self, selector: #selector(MainTableViewCell.startTimer(theTimer:)), userInfo: rowIndex, repeats: true)

    return InsideCollectionViewCell()
}

@objc func startTimer(theTimer:Timer) {

    UIView.animate(withDuration: 1.0, delay: 0, options: .curveEaseIn, animations: {

        self.clCollectionView.scrollToItem(at: IndexPath(row: theTimer.userInfo! as! Int, section: 0), at: .centeredHorizontally, animated: false)

    }, completion: nil)

}

0 个答案:

没有答案