在UICollectionView上动画自动布局约束更改

时间:2014-01-09 23:11:12

标签: ios cocoa-touch uicollectionview autolayout

从我问过的previous SO question开始,我试图动画UICollectionView(示例中为yellowBox)的高度变化。通过使用以下代码修改集合视图上的自动布局约束来触发此高度更改:

- (IBAction)expandYellowBox:(id)sender {

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

然而,当我调用[self.view layoutIfNeeded]时,这会导致重新加载集合视图,因此它会向用户显示闪烁。但是,我不希望重新加载集合视图,而是仅仅为其高度变化设置动画。是否有任何方法可以避免-layoutIfNeeded重新加载集合视图,或者为约束高度更改设置动画的替代方法,该约束高度更改不会调用具有重新加载集合视图的副作用的方法?

1 个答案:

答案 0 :(得分:3)

如果其他人遇到这个问题,那是因为我实际上是在动画块上面几行调用[self.collectionView reloadData](并没有注意到!)。似乎调用-reloadData-layoutIfNeeded会导致集合视图闪烁。删除对-reloadData的调用可以解决问题。