[英]UICollectionView : animation custom layout
我在UICollectionView中显示了很多图像单元格。 只需一个按钮,我就可以将所有细胞分组到第一个细胞上。
这很好用但是当我试图将动画过渡添加到我的重新组合动作时,没有任何反应。
这是我在自定义布局中使用的方法:
- (NSArray*)layoutAttributesForElementsInRect:(CGRect)rect
{
NSArray* allAttributesInRect = [super layoutAttributesForElementsInRect:rect];
if([allAttributesInRect count] > 0 && _isRegroup)
{
UICollectionViewLayoutAttributes *firstAttribute = [allAttributesInRect objectAtIndex:0];
CGRect frame = firstAttribute.frame;
for(UICollectionViewLayoutAttributes *attribute in allAttributesInRect)
[UIView animateWithDuration:0.3f animations:^{attribute.frame = frame;}];
}
return allAttributesInRect;
}
- (void)regroupCells:(BOOL)isRegroup // This method is called from my collection controller when my button is pressed
{
_isRegroup = isRegroup;
[self invalidateLayout];
}
任何想法 ? 谢谢 !
动画将无法在您调用它们的方法中起作用。
要更改布局并将动画更改为新布局,最简单的方法是在集合视图上调用performBatchUpdates
,每个块参数都使用nil
。 这会使您的布局和动画无效。
在执行此操作之前,您将告诉布局对象您希望新布局发生。 此外,在layoutAttributesForElementsInRect
,只需检查您的布尔变量并将分组的帧(可能是中心会更好)应用于您现在正在执行的所有属性,但没有动画。 您还需要在layoutAttributesForElementAtIndexPath
重现此代码。
所以,总结一下:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.