动画上UICollectionView自动约束布局变化 [英] Animating auto layout constraint change on UICollectionView

查看:801
本文介绍了动画上UICollectionView自动约束布局变化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从<一个继href=\"http://stackoverflow.com/questions/21014087/uiview-animated-changes-with-auto-layout-constraints\">$p$pvious SO质疑我问,我试图动画在 UICollectionView (这是 yellowBox 中的例子)。

Following on from a previous SO question which I asked, I am attempting to animate the change in height of a UICollectionView (which is yellowBox in the example). This change in height is being triggered by modifying the auto layout constraints on the collection view with the following code:

- (IBAction)expandYellowBox:(id)sender {

[UIView animateWithDuration:0.5
                 animations:^{
                     self.yellowBoxHeightConstraint.constant += 50;
                     [self.view layoutIfNeeded];
                 }];
}

然而,当我打电话 [self.view layoutIfNeeded] 这导致集合视图被重新加载,所以它明显闪烁给用户。不过,我不希望集合视图重载而只是以动画它的高度变化。有什么办法或避免 -layoutIfNeeded 重装集合视图,或者以另一种方式动画约束高度变化,这不叫有副作用的方法重装集合视图的?

However, when I call [self.view layoutIfNeeded] this results in the collection view being reloaded, so it flashes visibly to the user. However, I don't want the collection view to reload but instead just to animate its height change. Is there any way to either avoid -layoutIfNeeded reloading the collection view, or an alternative way to animate the constraint height change which doesn't call a method which has the side-effect of reloading the collection view?

推荐答案

在情况下,任何人都遇到这个问题,那是因为我其实是调用 [self.collectionView reloadData] 几行动画块以上(并没有注意到!)。如此看来,叫 -reloadData -layoutIfNeeded 将集合视图闪烁。卸下调用 -reloadData 解决该问题。

In case anyone else encounters this problem, it was because I was actually calling [self.collectionView reloadData] a few lines above the animation block (and didn't notice!). It seems that calling -reloadData and -layoutIfNeeded causes the collection view to flash. Removing the call to -reloadData resolves the problem.

这篇关于动画上UICollectionView自动约束布局变化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆