Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/112.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/tfs/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 设置contentOffset时UICollectionViewCell将消失_Ios_Swift - Fatal编程技术网

Ios 设置contentOffset时UICollectionViewCell将消失

Ios 设置contentOffset时UICollectionViewCell将消失,ios,swift,Ios,Swift,我已经创建了一个UICollectionView,它根据某个触发器滑动其项目。 我正在尝试通过以下方式设置细胞转换的动画: UIView.animate(withDuration: 0.4, delay: 0.5, animations: { self.headingsCollectionView.contentOffset = CGPoint(x: x, y:

我已经创建了一个
UICollectionView
,它根据某个触发器滑动其项目。 我正在尝试通过以下方式设置细胞转换的动画:

            UIView.animate(withDuration: 0.4,
                           delay: 0.5, animations: {

                            self.headingsCollectionView.contentOffset = CGPoint(x: x, y: 0)
                            self.headingsCollectionView.reloadItems(at: [indexPath])
            })
动画成功,问题是当我更改CollectionView内容偏移量时,当前项目将消失,而不是滚动关闭

  • 我尝试重新加载当前项目(如建议的),以便 将保持可见,但仍不工作

  • 我尝试了
    scrollToItemAtIndexPath:atScrollPosition:animated
    但是 它不会设置项目转换的动画
  • setContentOffset:animated尝试了它,但它也没有设置动画

因为您使用的是UIView.animate方法,所以必须在以编程方式设置contentOffset后放入self.view.layoutifNeed()

 UIView.animate(withDuration: 0.4,
                   delay: 0.5, animations: {

                    self.headingsCollectionView.contentOffset = CGPoint(x: x, y: 0)
                    self.headingsCollectionView.reloadItems(at: [indexPath])
                    self.view.layoutIfNeeded()
    })

因为您使用的是UIView.animate方法,所以必须在以编程方式设置contentOffset后放置self.view.layoutIfNeeded()

 UIView.animate(withDuration: 0.4,
                   delay: 0.5, animations: {

                    self.headingsCollectionView.contentOffset = CGPoint(x: x, y: 0)
                    self.headingsCollectionView.reloadItems(at: [indexPath])
                    self.view.layoutIfNeeded()
    })

删除self.HeadingCollectionView.reloadItems(位于:[indexPath])@Sandeppebhandari我尝试不使用它,但它仍在消失。
scrollToItemAtIndexPath:atScrollPosition:animated
如果您已经实现了它,应该可以做到这一点correctly@RijoPayyappilly请详细说明它的正确实现方式好吗?删除self.headingscolectionview.reloadItems(位于:[indexPath])@Sandeppebhandari我尝试不使用它,但它仍在消失。
scrollToItemAtIndexPath:atScrollPosition:animated
如果您已经实现了它,应该可以做到这一点correctly@RijoPayyappilly你能详细说明一下它的正确实现方式吗?我还以为它是某种黑魔法呢。谢谢你把这件事弄清楚!我以为这是某种黑魔法。谢谢你把这件事弄清楚!