如何在UICollectionView中正确地为单元格内的UILabel设置动画?

时间:2014-10-11 15:29:42

标签: swift ios8 uicollectionviewcell uiviewanimation

嘿伙计们,我有一段淡出的代码,并且在UILabel中,它位于UICollectioView的单元格中。在App运行时,标签会正确显示,问题是:动画不会发生。

我有相同的代码,但使用UITableView,它工作正常,但没有UICollectionView。这是代码:

override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
    let cell : Cells = collectionView.dequeueReusableCellWithReuseIdentifier("cell", forIndexPath: indexPath) as Cells

    cell.prodImg.image = UIImage(named: prodImgs[indexPath.row])
    cell.prodName.text = prodNomes[indexPath.row]
    cell.prodName.alpha = 1.0
    cell.prodName.hidden = false

    cellsArray = [collectionView.visibleCells()]
            return cell
}


override func scrollViewDidScroll(scrollView: UIScrollView) {
    for cell in cellsArray {
        println(cell)
        UIView.animateWithDuration(0.5, animations: {
            cell.prodName?.alpha = 0.0
            return
        })
    }
}


override func scrollViewDidEndDecelerating(scrollView: UIScrollView) {
    for cell in cellsArray {
        println(cell)
        UIView.animateWithDuration(0.5, animations: {
            cell.prodName?.alpha = 1.0
            return
        })
    }

}

所以任何人都有想法解决这个问题?

1 个答案:

答案 0 :(得分:0)

嘿伙计们,我设法让它发挥作用。以下是我如何使用它:

override func scrollViewDidScroll(scrollView: UIScrollView) {
for cell in self.collectionView?.visibleCells() as [Cells] {
    println(cell)
    UIView.animateWithDuration(0.5, animations: {
        cell.prodName?.alpha = 0.0
        return
    })
}
}


override func scrollViewDidEndDecelerating(scrollView: UIScrollView) {
for cell in self.collectionView?.visibleCells() as [Cells] {
    println(cell)
    UIView.animateWithDuration(0.5, animations: {
        cell.prodName?.alpha = 1.0
        return
    })
}

}