iOS开发小技巧-如何去掉隐式动画

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

到此,问题还没有结束

上面介绍的方法只能解决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];
    
}

参考文献

iOS核心动画高级技巧之图层行为

图形性能与测试工具

推荐

一个封装简单使用简单的雷达视图

iOS酷炫切换动画实战分析

推荐阅读更多精彩内容

  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 10,732评论 4 57
  • 与他终究是到了尽头。 三月即将完结,我与他的纠葛,或许只这三个月。 一百天的日子里,相互的了解又有多少,既然选...
    影子_d50a阅读 50评论 0 1
  • 用转换角度的姿态,去行天涯。转换角度,是非常重要的。一个人因转换角度,而老成持重;一个企业因转换角度,而日...
    Orsen浅时光阅读 133评论 0 1