ios - 如何在我滚动时为 UICollectionview 单元格设置动画

标签 ios swift uiview uicollectionview uicollectionviewcell

如何在滚动时为水平 Collectionview 设置动画 我在单元格中将 alpha 更改为 0,在 cellForItemAt 中我将 alpha 设置为 1 的动画,但这仅在 Collectionview 滚动到第一个时发生时间是我试过的代码

UIView.animate(withDuration: 0.8) {
        cell.imageView.alpha = 1
        cell.onboardLabel.alpha = 1
 }

我也尝试在 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
    }

最佳答案

swift 4:

从 UICollectionViewDelegate 使用这个函数:

 override func collectionView(_ collectionView: UICollectionView,
                             willDisplay cell: UICollectionViewCell,
                             forItemAt indexPath: IndexPath) {
    
    cell.alpha = 0
    UIView.animate(withDuration: 0.8) {
        cell.alpha = 1
    }
}

关于ios - 如何在我滚动时为 UICollectionview 单元格设置动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46874079/

相关文章:

jquery - 无法使用类设置 div 的高度

c# - 如何导入/包含包含对 Xamarin Studio/MonoTouch 静态内容文件的引用的文件

swift - 通过 optional 链接访问NSNumber var中的boolValue(在Swift中)

ios - 将应用程序启动到运行时关闭的最后一个 View

swift - 如何在表格 View 的单元格中添加更多行?

ios - 如何在 UIButton 之外编辑 UIButton 属性

ios - Realm 默认值属性崩溃

ios - 在 UIView 下面添加 CALayer

iOS 导航栏 VS UIToolBar

ios - UIView工厂创建过程