美文网首页自鉴iOS进阶指南iOS 开发每天分享优质文章
ios - 仿简书网页,网络加载过渡动画的封装(持续更新)

ios - 仿简书网页,网络加载过渡动画的封装(持续更新)

作者: tigerAndBull | 来源:发表于2018-09-15 18:31 被阅读963次

2018 - 09 - 22 重大更新!你一定会爱上这个库!!!
你或许只需要干2件事,就可以让所有组件在网络卡顿时动起来!!!

  1. 注册需要动画的组件
  2. 父视图控制动画的开关

2018 - 09 - 20晚 更新一次
更新内容:

  1. 动画支持所有继承自UIView的组件,以前仅支持UITableView
  2. 需要动的组件仍需注册一个属性
  3. 可以全局定义动画时长,动画背景色,由类方法改为单例模式

先上效果图

效果图.gif

说明

  1. 本文主要讲解如何将demo集成到你的项目中,并使用,同时也算是抛砖引玉了,大佬们要是有更好的封装方法,求之不得。
  2. 均为个人思考,转载请注明出处,谢谢🙏

主要使用的技术

AOP,即IOS的Runtime运行机制的黑魔法

使用流程

第一步:将demo的文件夹引入到你的项目中,并在合适位置
导入头文件TABAnimated.h,建议在.pch文件下全局引用

库内文件.png

第二步(可选):可以选择在appDelegate的didFinishLaunchingWithOptions方法全局设置动画属性,有默认属性,为上面示例图的样子

//设置TABAnimated相关属性
[[TABViewAnimated sharedAnimated]initWithAnimatedDuration:0.3 withColor:tab_kBackColor];

第三步:在需要动画的view上,将属性animatedStyle设置为TABTableViewAnimationStart,不需要动画的view不用做额外的操作

//UIView枚举
typedef NS_ENUM(NSInteger,TABTableViewAnimationStyle) {
    TABTableViewAnimationDefault = 0,     //没有动画,默认
    TABTableViewAnimationStart,           //开始动画
    TABTableViewAnimationEnd              //结束动画
};

//UITableView枚举
typedef NS_ENUM(NSInteger,TABViewAnimationStyle) {
    TABViewAnimationDefault = 0,    //没有动画,默认
    TABViewAnimationStart,          //开始动画
    TABViewAnimationEnd             //结束动画
};

//UITableView例子
- (UITableView *)mainTV {
    if (!_mainTV) {
        _mainTV = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight)];
        _mainTV.animatedStyle = TABTableViewAnimationStart;  //开启动画
        _mainTV.delegate = self;
        _mainTV.dataSource = self;
        _mainTV.rowHeight = 100;
        _mainTV.backgroundColor = [UIColor whiteColor];
        _mainTV.estimatedRowHeight = 0;
        _mainTV.estimatedSectionFooterHeight = 0;
        _mainTV.estimatedSectionHeaderHeight = 0;
        _mainTV.separatorStyle = UITableViewCellSeparatorStyleNone;
    }
    return _mainTV;
}

//UIView例子
- (TestHeadView *)headView {
    if (!_headView) {
        _headView = [[TestHeadView alloc]initWithFrame:CGRectMake(0, 0, tab_kScreenWidth, 90)];
        _headView.animatedStyle = TABViewAnimationStart;  //开启动画
    }
    return _headView;
}

第四步

  1. 需要动的组件的属性loadStyle,设置为需要的类型(不需要动的组件不用做额外的操作)
  2. (可选)新增属性tabViewWidth,其为动画开启时该组件的宽度,有较为合理默认值
typedef enum {
    TABViewLoadAnimationDefault = 0, //默认没有动画
    TABViewLoadAnimationShort,       //动画先变短再变长
    TABViewLoadAnimationLong         //动画先变长再变短
}TABViewLoadAnimationStyle;          //view动画类型枚举
{
        UILabel *lab = [[UILabel alloc]init];
        [lab setFont:tab_kFont(15)];
        lab.loadStyle = TABViewLoadAnimationLong;
        lab.tabViewWidth = 100;
        [lab setTextColor:[UIColor blackColor]];
        [lab setText:@""];
        
        titleLab = lab;
        [self.contentView addSubview:lab];
    }

第五步:在获取到数据后,停止动画,如下:

//停止动画,并刷新数据
_mainTV.animatedStyle = TABTableViewAnimationEnd;
[_mainTV reloadData];
    
_headView.animatedStyle = TABViewAnimationEnd;
[_headView initWithData:headGame];
[_headView layoutSubviews];

注意点

  1. 对于table组件,在加载动画的时候,即未获得数据时,不要设置对应的数值
- (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.animatedStyle != TABTableViewAnimationStart) {
        [cell initWithData:dataArray[indexPath.row]];
    }

    return cell;
}
  1. 没有默认高度,如果高度为0,则没有动画

最后:

相关文章

网友评论

  • Joey_cjj:发现分区会显示会出现问题:smiley: 望能尽快修复 期待!!!
    tigerAndBull:@Joey_cjj 请用最新代码哦,已经修复了
    Joey_cjj:@tigerAndBull 是这样的 在tableView设置多分区的时候会不起效。 问题定位在 -(NSInteger)tab_numberOfSectionsInTableView:(UITableView *)tableView {

    // get the count of sections safely.
    NSNumber *value = objc_getAssociatedObject(self, @selector(numberOfSections));

    if (tableView.animatedStyle == TABTableViewAnimationStart) {
    return ([value integerValue] > 0)?[value integerValue]:1;
    }
    tigerAndBull:@Joey_cjj 具体什么问题,需要你详细描述
  • a3f72f818c4f:用这个后所有的tableView都必须写 numberOfSectionsInTableView 组的代理方法?
    a3f72f818c4f:@tigerAndBull 好的,等待大佬提交:pray:
    tigerAndBull:午休有空的时候,我整理下,再做更新
    tigerAndBull:最新版已经不需要了,我还没来得及提交
  • 大牛在郑州:请问作者,记得网上有个swift做的,是哪个库?知道么,之前看到过忘了名字
    tigerAndBull:在写,很快就上了
    大牛在郑州:@tigerAndBull CollectionView支持下:kissing_heart:
    tigerAndBull:https://github.com/fahidattique55/FAShimmerViews
    我只知道这个,他的模版是死的,我有增加swift版本的打算
  • 洁简:问一下这种动画方式叫什么名字
    tigerAndBull:@苦笑男神 +1
    苦笑男神:Skeleton 可以叫骨架屏,骨架动画,之类的,,最初是Facebook先做的,GitHub上有很多类似的库。
  • fa18ead61094:加不了你qq:1429299849
    tigerAndBull:ax:18305177052
  • 8bb113bd53e7:多个TabellaView要注释掉以下代码,才可以有效果
    //保证交换方法只执行一次
    static dispatch_once_t onceToken;

    dispatch_once(&onceToken, ^{
    tigerAndBull:已经进行了更新,只需要把交换代理方法的限制去掉就可以了
    tigerAndBull:@Ricky2018 不好意思,没有考虑到多个的问题,后面会修的
  • destiny_12:我建议把启动方法直接放在分类上,这样项目不需任何操作,只要引入头文件直接就起作用了。
    tigerAndBull:@destiny_12 感谢你的建议
    tigerAndBull:@destiny_12 现在不需要额外操作,只要设置开启动画属性,table加载好就会运行。
  • Corbin___:AOP不难用,除非我是项目的大佬,不然小弟都不敢随便引进去的
    程序员不务正业:就应该大胆的用,出问题处理呗
    tigerAndBull:@陈泽槟_Corbin 没事的,知道怎么运行的就不会出问题的
    Corbin___:AOP不敢用

本文标题:ios - 仿简书网页,网络加载过渡动画的封装(持续更新)

本文链接:https://www.haomeiwen.com/subject/ulqtnftx.html