当我使用我的自定义动画(使用我的自定义动画)scrollToItem函数动画时,UICollectionView单元格会消失

时间:2018-04-13 13:19:09

标签: ios swift uiscrollview uicollectionview uianimation

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

为什么会发生这种行为?为什么当我没有按照自己的方式有目的地制作动画,并且只使用animated = 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)

3 个答案:

答案 0 :(得分:0)

我认为你应该用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)

希望它能运作

答案 1 :(得分:0)

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

尝试使用以下方法重置您的手机内容: {/ 1}}在你手机的控制器内。

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

Here's the docs link to it

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

希望它有所帮助!

答案 2 :(得分:0)

我认为问题是由于collectionView太早回收单元格所致,因为您正在使用带有animation = 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和animation = true。

相关问题