在做相冊的時候遇到了一個問題,就是UICollectionView刷新的時候會閃屏,網上搜了搜,解決的方法也是挺多,并沒有一一嘗試,只是存下來做個筆記,來看看遇到的幾種方法。
方法一:
1
2
3
4
|
[UIView performWithoutAnimation:^{ //刷新界面 [self.collectionView reloadData]; }]; |
把刷新界面的事件放在這個BLock里就可以了!
方法二
1
2
3
4
5
|
[UIView animateWithDuration:0 animations:^{ [collectionView performBatchUpdates:^{ [collectionView reloadItemsAtIndexPaths:@[[NSIndexPath indexPathForItem:index inSection:0]]]; } completion:nil]; }]; |
方法三
1
2
3
4
5
6
|
[UIView setAnimationsEnabled:NO]; [self.trackPanel performBatchUpdates:^{ [collectionView reloadItemsAtIndexPaths:@[[NSIndexPath indexPathForItem:index inSection:0]]]; } completion:^( BOOL finished) { [UIView setAnimationsEnabled:YES]; }]; |
如果你的APP只支持iOS7+,推薦使用第一種方式performWithoutAnimation簡單方便。
上面說的方法只能解決UIView的Animation,但是如果你的cell中還包含有CALayer的動畫,比如這樣:
1
2
3
4
5
|
- ( void )layoutSubviews{ [super layoutSubviews]; self.frameLayer.frame = self.frameView.bounds; } |
上述情況多用于自定義控件使用了layer.mask的情況,如果有這種情況,上面提到的方法是無法取消CALayer的動畫的,但是解決辦法也很簡單:
1
2
3
4
5
6
7
8
9
10
|
- ( void )layoutSubviews{ [super layoutSubviews]; [CATransaction begin]; [CATransaction setDisableActions:YES]; self.frameLayer.frame = self.frameView.bounds; [CATransaction commit]; } |
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。
原文鏈接:https://blog.duicode.com/2583.html