原生骨架库模版功能上线,零耦合。

前言

前文章地址

首先,原有的骨架库实现的大概思路: 如果你开启了动画, 框架会根据view内的所有subViews的位置,映射出一组一模一样的CALayer动画,并进行管理。

目录

  • 技术瓶颈
  • 模版功能 - 展示
  • 模版功能 - 使用方式
  • 模版功能 - 其他细节

技术瓶颈

  1. 如果使用约束进行布局,例如知名的第三方库Masonry布局,大部分只需要2个约束就可以很好地布局, 但是, 2个约束就可以很好地布局是在数据已经填充的情况下,如果没有数据,则位置信息是完全不对

  2. 本框架采用的是AOP编程,最初地思想是开发者尽量不需要动自己原有的代码,就可以完成动画的设置。 但是,当你使用后会发现,会于原代码产生一定耦合,不会利于他人阅读和维护。

  3. 我们将骨架展示给用户时,大部分情况是这样的:

  • 可能并不需要很复杂的view,完全展示给用户
  • 可能是很个性化的view(因为映射出的动画,并不能保证好看,又需要调试)
  • 可能是通用的view,很多地方共用一个就行了

那么模版功能特别适合你。

模版功能 - 展示

template.gif

交流群

TABAniamted交流群:304543771 提出你的意见

模版功能 - 使用方式

模版功能是库的一个新功能,并不是一个新的库。

  1. 模版功能只针对常用的表格组件。
  2. 开启和结束动画的方式不变
  3. 唯一的改变就是在表格初始化的时候,注册模版,如下
- (UICollectionView *)collectionView {
    if (!_collectionView) {
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, kNavigationHeight, kScreenWidth, kScreenHeight-kNavigationHeight)            collectionViewLayout:layout];
        _collectionView.backgroundColor = [UIColor whiteColor];
        _collectionView.dataSource = self;
        _collectionView.delegate = self;
        _collectionView.animatedDelegate = self;             
        _collectionView.showsHorizontalScrollIndicator = NO;
        _collectionView.showsVerticalScrollIndicator = NO;
        
        // 注册模版
        [_collectionView registerTemplateClassArray:@[[TemplateCollectionViewCell class],
                                                      [TemplateSecondCollectionViewCell class]]];

    }
    return _collectionView;
}

复制代码

模版功能 - 其他细节

  1. cell模版需要自己写,布局写死,想什么样就什么样 但需要继承自TABBaseCollectionViewCellTABBaseTableViewCell
  2. 以table举例,TABBaseTableViewCell中的cellHeight方法, 需要你在子类重写,并指定数值, 这个返回值就是改模版在动画是展示的高度
+ (NSNumber *)cellHeight {
    return [NSNumber numberWithFloat:10+headImgWidth+5+80+10+imgWidth];
}
复制代码
  1. 模版功能依旧根据animationType设置动画类型
  2. 使用isUseTemplate属性切换为模版模式, 可以在动画开启前随意切换。
  3. 模版中的组件,使用经典类型的动画,依旧需要指定动画类型
  4. 提供两种方式注册模版,一个section和多section, 多个section是以一个class数组形式储存。 言外之意,数组中的模版类和section一一对应。
- (void)registerTemplateClassArray:(NSArray <Class> *)classArray;
复制代码

最后

  • 如有问题,加入交流群:304543771
  • github地址:https://github.com/tigerAndBull/LoadAnimatedDemo-ios

猜你喜欢

转载自juejin.im/post/5c84d309f265da2ddc3cae72