看到集合视图单元格内的图像视图后,可以显示动画

时间:2016-12-19 22:08:42

标签: ios swift uicollectionview

我有collectionview全屏cells,它会自动逐个滚动。我想在我的单元格中向imageView添加动画。但我希望动画在自动滚动完成后启动。我尝试了下面的代码,但它只适用于第一个单元格。

我在此行cell.alpImage.getBiggerAnim()中调用动画功能。但正如我之前所说,它仅适用于第一个细胞。

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

    if (indexPath.item >= alpImageArray.count - 1) {
        cell.alpImage.isHidden = true
        cell.startAgain.isHidden = false
        cell.readyLabel.isHidden = true
        cell.startAgain.addTarget(self, action: #selector(startAgainPressed), for: .touchUpInside)
    } else if (indexPath.item == 0) {
        cell.readyLabel.isHidden = false
        cell.alpImage.isHidden = true
    } else {
        cell.startAgain.isHidden = true
        cell.readyLabel.isHidden = true
        cell.alpImage.isHidden = false
        cell.alpImage.image = UIImage(named: alpImageArray[indexPath.row] + ".png")
    }
    cell.alpImage.getBiggerAnim()
    return cell
}

这些是管理自动滚动的功能:

func scrollToNextCell(){
    let cellSize = CGSize(width: self.view.frame.width, height: self.view.frame.height)
    let contentOffset = myCollectionView.contentOffset
    width: cellSize.width, height: cellSize.height), animated: true)
    alphabetSoundPlay()
}
var myTimer: Timer?
func startTimer() {
    myTimer = Timer.scheduledTimer(timeInterval: 3.0,
                                   target: self,
                                   selector: #selector(scrollToNextCell),
                                   userInfo: nil,
                                   repeats: true)
}

这是我的动画扩展名:

extension UIView {
    func getBiggerAnim() {
        UIView.animate(withDuration: 0.6 , animations: {
            self.transform = CGAffineTransform(scaleX: 0.6, y: 0.6)
        },completion: { finish in
            UIView.animate(withDuration: 0.6){
                self.transform = CGAffineTransform.identity
            }
        })
    }
}

1 个答案:

答案 0 :(得分:0)

你应该使用这样的东西..

func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
   for cell: UICollectionViewCell in self.myCollectionView.visibleCells {
    var indexPath = self.myCollectionView.indexPath(for: cell)!
    let acell = myCollectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! AlphabetCollectionViewCell

    print("\(indexPath)")
    acell.alpImage.getBiggerAnim()
   }
}