如何在滚动时为UICollectionview单元设置动画

Sha*_*ndo 6 uiview ios uicollectionview uicollectionviewcell swift

我如何设置水平Collectionview动画,因为它滚动我在单元格cellForItemAt中将alpha 更改为0,并且我将alpha动画设置为1,但只有在第一次滚动Collectionview时才会发生这种情况我尝试过的代码

UIView.animate(withDuration: 0.8) {
        cell.imageView.alpha = 1
        cell.onboardLabel.alpha = 1
 }
Run Code Online (Sandbox Code Playgroud)

我也试过这样做,scrollViewDidEndDecelerating但仍然没有工作

 let index = Int(scrollView.contentOffset.x) / Int(scrollView.frame.width)
 let indexPath = IndexPath(item: index, section: 0)
 let cell = collectionView.cellForItem(at: indexPath) as? OnboardingCell

    UIView.animate(withDuration: 0.8) {
        cell?.imageView.alpha = 1
        cell?.onboardLabel.alpha = 1
    }
Run Code Online (Sandbox Code Playgroud)

Séb*_*EMY 14

斯威夫特4:

从UICollectionViewDelegate使用此函数:

 override func collectionView(_ collectionView: UICollectionView,
                             willDisplay cell: UICollectionViewCell,
                             forItemAt indexPath: IndexPath) {

    cell.alpha = 0
    UIView.animate(withDuration: 0.8) {
        cell.alpha = 1
    }
}
Run Code Online (Sandbox Code Playgroud)