原生骨架屏 - 2.0.0重磅推出

最新版 2.0.3.2 历史版本文档

目录

关于 TABAnimated

TABAnimated起初是模仿简书的骨架屏效果。但是在2.0版本以上,为了优化底层原理,降低耦合度,删除了该特效,而且这种动态性,似乎不怎么受开发者欢迎。

实现原理

TABAnimated 需要一个控制视图,进行开关动画。 该控制视图下的所有subViews都将加入动画队列。

当然,你可以指定view移出动画队列。 TABAnimated通过subViews的位置创建TABLayer。 (TABLayer 是 CALayer的子类)

几乎所有的页面都可以通过 TABAnimated解决。

当使用约束进行布局时,当约束不足且没有数据时,致使subViews的位置信息不能体现出来,TABAnimated会进行数据预填充。

两种模式

为什么有两种模式?

在大多数情况下,如果TABAnimated按原视图创建动画,则动画并不漂亮。 但是你只需要做一点调整就可以让Tabanimated看起来不错。 所以,如果您不关心项目中的耦合,您可以使用普通模式。

如果您在意耦合度和阅读成本,可以使用模版模式。于此同时,您需要另创建一个新的模板cell文件。 虽然这种会增加工作负担,但是作者推崇这种模式。

优点

  • 集成迅速
  • 模版零耦合
  • 高性能
  • 适应性强
  • 完全自定制

安装

使用 CocoaPods
pod 'TABAnimated'
复制代码
手动导入

将TABAnimated文件夹拖入工程

效果图

卡片投影式模版模式 - 下拉演示普通模式
呼吸灯闪光灯分段视图演示

如何使用

快速看到效果

您只需要四步

  1. import 'TABAnimated.h'
  2. didFinishLaunchingWithOptions 中初始化 TABAimated
// init `TABAnimated`, and set the properties you need.
[[TABViewAnimated sharedAnimated] initWithOnlySkeleton];
// demo changes the pattern quietly.
// you can change the pattern in different views.
[TABViewAnimated sharedAnimated].isUseTemplate = YES;
// open log
[TABViewAnimated sharedAnimated].openLog = YES;
// set gobal cornerRadius
[TABViewAnimated sharedAnimated].animatedCornerRadius = 3.f;
复制代码
  1. [self.rootControlView tab_startAnimation]; // 开启动画
  2. [self.rootControlView tab_endAnimation]; // 关闭动画

当然,这只是最简单的使用,并不能很完美的适配

模版模式教程

只有UITableView和UICollectionView支持模板模式。

普通视图使用模板模式其实是多余的。

1.设置 TABAnimatedObject 相关属性

_collectionView.tabAnimated = [TABAnimatedObject animatedWithTemplateClass:[TABTemplateCollectionViewCell class] animatedCount:4];
复制代码

TABAnimatedObject 初始化方法

/**
 单section表格组件初始化方式
 
 @param templateClass 模版cell, animatedCount 为默认值,默认值为填充屏幕
 @return object
 */
+ (instancetype)animatedWithTemplateClass:(Class)templateClass;

/**
 单section表格组件初始化方式

 @param templateClass 模版cell
 @param animatedCount 动画时row值
 @return object
 */
+ (instancetype)animatedWithTemplateClass:(Class)templateClass
                            animatedCount:(NSInteger)animatedCount;

/**
 多section表格组件初始化方式

 @param templateClassArray 模版cell数组
 @param animatedCountArray 动画时row值的集合
 @return object
 */
+ (instancetype)animatedWithTemplateClassArray:(NSArray <Class> *)templateClassArray
                            animatedCountArray:(NSArray <NSNumber *> *)animatedCountArray;
复制代码

数组安全处理:

当animatedCountArray.Count>section.Count时,animatedCountArray上的多余部分无效。

当animatedCountArray.count<section.count时,多余部分将按animatedCountArray.lastObject加载。

  1. 创建模版

UITableViewCell的

  • 新建cell,继承自TABBaseTableViewCell
  • 重写+ (NSNumber *)cellHeight 声明模版cell高度
  • 初始化模版对应组件,设置对应frame,支持自动布局

UICollectionViewCell的

  • 新建cell,继承自TABBaseCollectionViewCell
  • 重写+ (NSValue *)cellSize 声明模版cell高度
  • 初始化模版对应组件,设置对应frame,支持自动布局
tips.
  1. TABAnimatedObject的其他属性,请自己查看demo注释
  2. 模版可以用你项目中的cell,但是会产生耦合,请开发者自行抉择
普通模式教程
  1. 设置tabAnimated相关属性
// 可以不进行手动初始化,将使用默认属性
TABAnimatedObject *tabAnimated = TABAnimatedObject.new;
tabAnimated.animatedCount = 3;
_tableView.tabAnimated = tabAnimated;
复制代码
  1. 默认会将所有subViews加入动画队列, 可以使用loadStyleTABViewLoadAnimationRemove将指定view移出(模版同理)
{
        UILabel *lab = [[UILabel alloc]init];
        [lab setFont:tab_kFont(15)];
        lab.loadStyle = TABViewLoadAnimationRemove;   // 移除动画队列
        [lab setTextColor:[UIColor blackColor]];
        titleLab = lab;
        [self.contentView addSubview:lab];
 }
复制代码

Tips

表格使用细节

以下均针对UITableView组件和UICollectionView组件

  1. 在加载动画的时候,即未获得数据时,不要设置对应的数值 当然这样的话耦合度高,下面说明当前解决方案。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *str = @"TestTableViewCell";
    TestTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:str];
    if (!cell) {
        cell = [[TestTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:str];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    
    // 较老版本有变动
    // 在加载动画的时候,即未获得数据时,不要走加载控件数据的方法
    if (!_mainTV.tabAnimated.isAnimating) {
        [cell initWithData:dataArray[indexPath.row]];
    }

    return cell;
}
复制代码
  1. 一般情况下刷新数据源的时候,大家都用cellForRowAtIndexPath代理方法, 解决方案就是加载视图使用cellForRowAtIndexPath代理方法, 刷新数据源使用willDisplayCell代理方法,如下:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *str = @"TestTableViewCell";
    TestTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:str];
    if (!cell) {
        cell = [[TestTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:str];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    return cell;
}

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
    TestTableViewCell *myCell = (TestTableViewCell *)cell;
    [myCell initWithData:dataArray[indexPath.row]];
}
复制代码
  1. 多section通过新增表格代理方法解决,模版也可以用 UITableViewAnimatedDelegateUICollectionViewAnimatedDelegate
_mainTV.animatedDelegate = self;
复制代码
- (NSInteger)tableView:(UITableView *)tableView numberOfAnimatedRowsInSection:(NSInteger)section {
    if (section == 0) {
        return 5;
    }
    return 3;
}
复制代码
  1. 对于嵌套表格组件,需要被嵌套在内的表格组件的isNest属性设为YES 嵌套表格比较特殊,具体看demo
_collectionView.tabAnimated = [[TABAnimatedObject alloc] init];
_collectionView.tabAnimated.isNest = YES;
_collectionView.tabAnimated.animatedCount = 3;
复制代码

其他属性

初始化方法名称
initWithOnlySkeleton骨架屏
initWithBinAnimation呼吸灯动画
initWithShimmerAnimated闪光灯动画

如果控制视图的superAnimationType做了设置,那么将以superAnimationType声明的动画类型加载

选择设置其他TABAnimated的属性:

属性名称适用模式适用动画含义默认值
animatedColor通用通用动画颜色0xEEEEEE
animatedBackgroundColor通用通用动画背景颜色UIColor.white
animatedDurationShimmer通用闪光灯移动时长1.5
animatedHeightCoefficient通用通用高度系数0.75
animatedCornerRadius通用通用全局圆角0.
templateTableViewCell模版通用通用模版\
templateCollectionViewCell模版通用通用模版\
openLog通用通用开启日志NO

强调:

  1. demo也只是简单的引导作用, 你可以订制更精美的效果,解决大部分的视图骨架
  2. 遇到问题先去demo上看看有没有使用示例

最后:

  • 感谢使用,如果您觉得不错可以在github上点个star
  • 如有问题,优化建议,加入交流群更快解决:304543771
  • github地址:github.com/tigerAndBul…

转载于:https://juejin.im/post/5ca8440fe51d457a8a7a01ea

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值