iOS 心得一 单元格的动画加载(非autolayout适配模式下)

工作的时候有好多自己以前没有做过的,经过自己的努力活着他人的提醒做出来的放在这里和大家一起探讨进步。以后就以iOS心得开头记录了,也是为了激励自己一值写下去。进入正题:


大家都知道表的加载,这是作为ios程序员最最经常使用的一个类,但是单元格的动画加载可能有一些人不知道或者没有用过。我就贴上一下我自己理解的单元格动画加载。

首先单元格有一个方法是专门用来显示单元格显示之前做的最后操作:

-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath

这个方法一看名字你也知道他的作用,就是将要显示。我刚开始使用核心动画做了一些操作,比如上移,旋转之类的,但是我重点讲的不是这个,因为这个方法虽然可以动画显示单元格,但是却是单元格里面所有的子视图和他一起做同一个动画然后回到动画之前的位置。我们经理给我的要求是单元格里面的视图做一个上移的动画之后,不在返回,就放在那个动画之后的地方。所以我又在自定义单元格里面做内容。首先我写了一个简单的表:


#import <UIKit/UIKit.h>


@interface ViewController : UIViewController<UITableViewDataSource,UITableViewDelegate>

//

@property(nonatomic,retain)UITableView *tableView;

//数组

@property(nonatomic,retain)NSArray *array;



@end



#import "ViewController.h"


#import "MyCell.h"



@interface ViewController ()


@end


@implementation ViewController


- (void)viewDidLoad {

    [super viewDidLoad];

    

    self.array = [UIFont familyNames];

    //表的设置

    self.tableView = [[UITableView alloc]initWithFrame:self.view.bounds];

    self.tableView.delegate = self;

    self.tableView.dataSource = self;

    self.tableView.rowHeight = 400;

//    self.tableView.estimatedRowHeight = 400;

//    self.tableView.rowHeight = UITableViewAutomaticDimension;

    self.tableView.showsVerticalScrollIndicator = NO;

    [self.view addSubview:self.tableView];

    

}


@end



然后我去自定义单元格,我用的是xib,单元格里面放一个普通的view



然后我在单元格类的一个方法

- (void)awakeFromNib 里面去写动画加载的方法,代码如下

#import "MyCell.h"


@implementation MyCell


+(id)createCell

{

    return [[[NSBundle mainBundle]loadNibNamed:@"MyCell" owner:nil options:nil] lastObject];

}


- (void)awakeFromNib {

    //平移动画

    CABasicAnimation *a1 = [CABasicAnimation animation];

    a1.keyPath = @"transform.translation.y";

    a1.toValue = @(-30);

    

        //透明度

        CABasicAnimation *an1;

        an1 = [CABasicAnimation animationWithKeyPath:@"opacity"];

        an1.autoreverses = YES;

        an1.fromValue = [NSNumber numberWithFloat:0.1];

        an1.toValue = [NSNumber numberWithFloat:1.0];

    CAAnimationGroup *group = [CAAnimationGroup animation];

    group.animations = @[a1,an1];

    //设置组动画时间

    group.duration = 0.8;

    group.fillMode = kCAFillModeForwards;

    group.removedOnCompletion = NO;

    

    [self.bgView.layer addAnimation:group forKey:nil];


}


@end


单元格动画写完之后我再回到表里面去加载单元格

#import "ViewController.h"


#import "MyCell.h"


@interface ViewController ()


@end


@implementation ViewController


- (void)viewDidLoad {

    [super viewDidLoad];

    

    self.array = [UIFont familyNames];

    //表的设置

    self.tableView = [[UITableView alloc]initWithFrame:self.view.bounds];

    self.tableView.delegate = self;

    self.tableView.dataSource = self;

    self.tableView.rowHeight = 400;

//    self.tableView.estimatedRowHeight = 400;

//    self.tableView.rowHeight = UITableViewAutomaticDimension;

    self.tableView.showsVerticalScrollIndicator = NO;

    [self.view addSubview:self.tableView];

    

}


#pragma mark-----

#pragma mark----表的代理

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

{

    return self.array.count;

}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

{

    static NSString *ID = @"cell";

    MyCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];

    if (cell == nil) {

        cell = [MyCell createCell];

    }

    cell.selectionStyle = UITableViewCellSelectionStyleNone;

    return cell;

}

@end



运行之后既可以看到单元格内容的动态加载



好吧,确实不知道怎么上传运行视频的,对不住了,有兴趣的可以留言,我把源码发给你们

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值