Tips:取消UICollectionView的隐式动画

http://www.cocoachina.com/ios/20151204/14211.htmlhtml

 

UICollectionView在reloadItems的时候,默认会附加一个隐式的fade动画,有时候很讨厌,尤为是当你的cell是复合cell的状况下(好比cell使用到了UIStackView)。ios

下面几种方法均可以帮你去除这些动画动画

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
//方法一
[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) 简单方便spa

butcode

问题尚未结束 上面介绍的方法只能解决UIView的Animation 若是你的cell中还包含有CALayer的动画 好比这样orm

1
2
3
4
5
6
- (void)layoutSubviews
{
     [ super  layoutSubviews];
     
     self.frameLayer.frame = self.frameView.bounds;
}

上述状况多用于自定义控件使用了layer.mask的状况 若是有这种状况 上面提到的方法是没法取消CALayer的动画的 可是解决办法也很简单htm

1
2
3
4
5
6
7
8
9
10
11
12
- (void)layoutSubviews
{
     [ super  layoutSubviews];
     
     [CATransaction begin];
     [CATransaction setDisableActions:YES];
     
     self.frameLayer.frame = self.frameView.bounds;
     
     [CATransaction commit];
     
}

done!ip

相关文章
相关标签/搜索