【问题标题】:How to animate UICollectionview cells as i scroll滚动时如何为 UICollectionview 单元格设置动画
【发布时间】:2017-10-22 12:30:36
【问题描述】:

我如何在水平 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
    }

【问题讨论】:

    标签: ios swift uiview uicollectionview uicollectionviewcell


    【解决方案1】:

    斯威夫特 4:

    从 UICollectionViewDelegate 使用这个函数:

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

    【讨论】:

      【解决方案2】:

      首先你需要知道哪些单元格是可见的,所以在文件的顶部设置这个变量。

      var visibleIndexPath: IndexPath? = nil
      

      在 scrollViewDidEndDecelerating 中使用此代码设置 visibleIndexPath:

      func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
          var visibleRect = CGRect()
      
          visibleRect.origin = collectionView.contentOffset
          visibleRect.size = collectionView.bounds.size
      
          let visiblePoint = CGPoint(x: visibleRect.midX, y: visibleRect.midY)
      
          if let visibleIndexPath = collectionView.indexPathForItem(at: visiblePoint) {
              self.visibleIndexPath = visibleIndexPath
          }
      }
      

      现在您有了 visibleIndexPath,您可以在 willDisplay 单元格函数中为单元格设置动画。

       func collectionView(_ collectionView: UICollectionView, willDisplay cell: UICollectionViewCell, forItemAt indexPath: IndexPath) {
      
              if let visibleIndexPath = self.visibleIndexPath {
      
                  // This conditional makes sure you only animate cells from the bottom and not the top, your choice to remove.
                  if indexPath.row > visibleIndexPath.row {
      
                      cell.contentView.alpha = 0.3
      
                      cell.layer.transform = CATransform3DMakeScale(0.5, 0.5, 0.5)
      
                      // Simple Animation 
                      UIView.animate(withDuration: 0.5) {
                          cell.contentView.alpha = 1
                          cell.layer.transform = CATransform3DScale(CATransform3DIdentity, 1, 1, 1)
                      }
                  }
              }
      }
      

      【讨论】:

      • 你能给我指出学习CoreAnimation的正确方向吗?我一直在寻找好的资源
      • @Shagsmando 如果这是正确答案,为什么不标记为答案?
      • 因为我刚刚标记的是一种更好更清洁的方式
      猜你喜欢
      • 2021-07-04
      • 1970-01-01
      • 1970-01-01
      • 2018-08-22
      • 2013-07-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多