
作者:裡脊串 授權本站轉載。
UICollectionView在reloadItems的時候,默認會附加一個隱式的fade動畫,有時候很討厭,尤其是當你的cell是復合cell的情況下(比如cell使用到了UIStackView)。
下面幾種方法都可以幫你去除這些動畫
//方法一
[UIView performWithoutAnimation:^{
[collectionView reloadItemsAtIndexPaths:@[[NSIndexPath indexPathForItem:index inSection:0]]];
}];
//方法二
[UIView animateWithDuration:0 animations:^{
[collectionView performBatchUpdates:^{
[collectionView reloadItemsAtIndexPaths:@[[NSIndexPath indexPathForItem:index inSection:0]]];
} completion:nil];
}];
//方法三
[UIView setAnimationsEnabled:NO];
[self.trackPanel performBatchUpdates:^{
[collectionView reloadItemsAtIndexPaths:@[[NSIndexPath indexPathForItem:index inSection:0]]];
} completion:^(BOOL finished) {
[UIView setAnimationsEnabled:YES];
}];如果你的APP只支持iOS7+ 推薦使用第一種方式performWithoutAnimation(感謝@sunnyxx的tip) 簡單方便
but
問題還沒有結束 上面介紹的方法只能解決UIView的Animation 如果你的cell中還包含有CALayer的動畫 比如這樣
- (void)layoutSubviews
{
[super layoutSubviews];
self.frameLayer.frame = self.frameView.bounds;
}上述情況多用於自定義控件使用了layer.mask的情況 如果有這種情況 上面提到的方法是無法取消CALayer的動畫的 但是解決辦法也很簡單
- (void)layoutSubviews
{
[super layoutSubviews];
[CATransaction begin];
[CATransaction setDisableActions:YES];
self.frameLayer.frame = self.frameView.bounds;
[CATransaction commit];
}done!