工作的时候有好多自己以前没有做过的,经过自己的努力活着他人的提醒做出来的放在这里和大家一起探讨进步。以后就以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
好吧,确实不知道怎么上传运行视频的,对不住了,有兴趣的可以留言,我把源码发给你们