【问题标题】:UICollectionView cell disappears when i animate(with my custom animation) scrollToItem functionUICollectionView 单元格在我动画(使用我的自定义动画)scrollToItem 函数时消失
【发布时间】:2018-09-23 20:58:35
【问题描述】:

当我以编程方式滚动到项目时,我只是想制作一个自定义动画。因此,当我不编写动画并使用默认单元格时不会消失,但是当我将 scrolltoItem func 放入 UIView.animate func 时,最后一个单元格首先消失,然后 scrollToItem 动画。 在最上面的collectionView的第二张图片中,位于独立游戏单元之前的位置首先消失,然后collectionView从独立游戏单元滚动到下一个

为什么会发生这种行为?为什么当我没有以我的方式故意为它设置动画,而只是用动画 = true func 调用 scrollToItem 时,什么都没有消除?如果有人知道细胞发生了什么,请给我一个线索。

  UIView.animate(withDuration: 10, delay: 0, options: .curveEaseInOut, animations: {
                 self.appsCollectionView.scrollToItem(at: IndexPath(item: self.actualNumberOfTheCell, section: 0), at: .centeredHorizontally, animated: false)
            }, completion: nil)

【问题讨论】:

  • 实际上在你的代码中你在动画中说我想滚动到第 4 个项目(你当前的位置是 0)所以动画认为你现在不需要第 0 个元素所以它只是删除它们并制作动画你的其他单元格相应地。您可以尝试在动画中使用setContentOffset,滑动方向的偏移增量为+1。不知道性能如何,这是一个尝试。只需检查它是否有效
  • 对此仍然没有答案?

标签: ios swift uiscrollview uicollectionview uianimation


【解决方案1】:

我认为你应该用self.view.layoutIfNeeded()来做这件事

self.appsCollectionView.scrollToItem(at: IndexPath(item: self.actualNumberOfTheCell, section: 0), at: .centeredHorizontally, animated: false)
UIView.animate(withDuration: 10, delay: 0, options: .curveEaseInOut, animations: {
          self.view.layoutIfNeeded()   
        }, completion: nil)

希望能成功

【讨论】:

  • 是的,但是会有一点小问题。只需检查您的单元格是否被重用(在单元格类中打印 prepareCellForReuse 内的内容。如果它正在重用它,那么这是一个解决方案(Y)
【解决方案2】:

由于突然滚动,您重复使用的单元格可能会重叠。最新的单元格可能没有正确出列,并且可能使用前一个单元格的内容进行初始化 -> 这很可能是一个在滚动期间未正确显示的空单元格。

尝试使用以下方法重置您的单元格内容: prepareForReuse() 在您单元的控制器中。

您应该在此处将您的 UI 重置为默认状态。

Here's the docs link to it

不要忘记为您的单元格调用 UI 显示/设置方法,该方法应该显示您的 UI 元素。

希望对你有帮助!

【讨论】:

    【解决方案3】:

    我认为问题是由于 collectionView 过早回收单元格造成的,因为您使用的是动画 = false 的 scrollToItem。

    我尝试将动画分成几个较小的步骤,它可以工作,但滚动不流畅。代码如下:

    // self is extended from UICollectionView
    scrollTo(x: 100, duration: 0.6, count: 4) { (finished) in
    
            }
    
     private func scrollTo(x:CGFloat, duration:TimeInterval, count:Int, completion:@escaping (Bool)->Void)
    {
        let xOffset = (x - contentOffset.x)/CGFloat(count)
        let durationPart = duration/TimeInterval(count)
    
        scrollToPart(xOffset: xOffset, duration: durationPart, count: count, completion: completion)
    }
    
    private func scrollToPart(xOffset:CGFloat, duration:TimeInterval, count:Int, completion:@escaping (Bool)->Void)
    {
        UIView.animate(withDuration: duration, animations: {
            self.contentOffset.x += xOffset
        }) { (finished) in
    
            if count <= 1
            {
                completion(finished)
            }
            else
            {
                self.scrollToPart(xOffset: xOffset, duration: duration, count: count-1, completion: completion)
            }
        }
    }
    

    我最后使用了动画=真的scrollToItem。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-04-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-04-04
      • 1970-01-01
      相关资源
      最近更新 更多