ectionView时防止UICollectionViewCel

ectionView时防止UICollectionViewCel

本文介绍了在呈现UICollectionView时防止UICollectionViewCell动画外观的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当用户执行某些操作时,我需要从底部向上拉一个 UICollectionView 到一定高度。由于新状态完全是可选的,因此集合视图就在以这种方式呈现之前创建。使用对NSLayoutConstraint的常量属性的更改以及对某些 [view layoutIfNeeded] 的调用来执行从下到上的动画动画块。

When a user does some action, I need to pull a UICollectionView from the bottom up to a certain height. Since that new state is totally optional, the collection view is created just before being presented that way. The animation from bottom to top is performed using changes to NSLayoutConstraint's constant property and a call to [view layoutIfNeeded] in some animation block.

问题在于,以这种方式做事会使单元格以不希望的方式出现:它们从左上角扩展到指定的大小。我希望集合视图出现并且所有单元格已经按照它们的最终尺寸和外观进行布局。

The problem is that, doing things that way makes the cells appear in an undesired way: they expand from their top-left corner to their specified size. I would like the collection view to appear and have all of its cells already laid out in their final size and appearance.

我知道像UIView的 setAnimationEnabled:这样的方法,但我似乎无法找到如何和在哪里我应该使用它(如果这是要走的路)。

I'm aware of things like UIView's setAnimationEnabled: method, but I can't seem to find how and where I should use that (if that's the way to go).

我想问题是由于集合视图单元格被添加到动画之前的视图层次结构中包含对 [superview layoutIfNeeded] 的调用的块。这可能导致UIKit认为它还应该动画对布局的那些更改。如果是这种情况,那么解决方案可能就是从动画中排除,视图层次结构的特定更改。

I guess the problem is due to the collection view cells being added to the view hierarchy just before the animation block that contains the call to [superview layoutIfNeeded]. This probably leads UIKit to think that it should also animate those changes to the layout. If that's the case the solution would probably be something along the way of excluding from the animation, specific changes to the view hierarchy.

推荐答案

从左上角展开通常是在原始视图从未布局时在动画块中调用 layoutIfNeeded 的症状。您基本上是动画初始布局通道,其中所有子视图都是从CGRectZero开始的。

Expanding from the top left corner is typically a symptom of calling layoutIfNeeded in an animation block when the original view has never been laid out. You're basically animating the initial layout pass, where all subviews have started at CGRectZero.

要解决这个问题,你需要做两件事:

To solve it you need two things:


  • 确保约束你''重新编辑与集合视图的位置相关,而不是大小。我的意思是你没有通过将其高度从零改为最终值来呈现你的集合视图。

  • 调用 layoutIfNeeded 在编辑约束之前在视图上,然后在进行更改后再在动画块中再次调用它。这样,您只需将您指定的更改设置为约束,而不是整个布局。

  • Ensure the constraint you're editing is related to the position of the collection view, not the size. By this I mean that you're not presenting your collection view by changing its height from zero to the final value.
  • Call layoutIfNeeded on your view before you edit the constraint, then call it again in the animation block after you've made the change. This way you're only animating the change you've specified to the constraint, rather than the entire layout.

这篇关于在呈现UICollectionView时防止UICollectionViewCell动画外观的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-19 21:27