解决collectionView刷新时闪屏的bug(隐式动画)

1,760 阅读1分钟

本文已参与「新人创作礼」活动,一起开启掘金创作之路。

大牛们有很多解决办法,但是我用过这个,是行之有效的,其他的没什么作用。

//方法一:
  [UIView performWithoutAnimation:^{
        //刷新界面
         [self.collectionView reloadData];
   }];

把刷新界面的事件放在这个BLock里就可以了!

其他的方法还有:

//方法二
[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];
    
}

\