Ios tableview cell.imageview未设置动画
我正在尝试设置默认情况下存在的Ios tableview cell.imageview未设置动画,ios,uitableview,imageview,tableview,Ios,Uitableview,Imageview,Tableview,我正在尝试设置默认情况下存在的tableview单元格的imageview的动画,我希望在单元格可见时仅对图像视图设置一次动画,为此我编写了以下代码 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *cellIdentifier = @"Cell"; UITableViewCell *c
tableview
单元格的imageview
的动画,我希望在单元格可见时仅对图像视图设置一次动画,为此我编写了以下代码
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
NSArray *myImageArray = [NSArray arrayWithObjects:
[UIImage imageNamed:[NSString stringWithFormat:@"wave6.png"]],
[UIImage imageNamed:[NSString stringWithFormat:@"wave7.png"]],
[UIImage imageNamed:[NSString stringWithFormat:@"wave8.png"]],
[UIImage imageNamed:[NSString stringWithFormat:@"wave9.png"]],
[UIImage imageNamed:[NSString stringWithFormat:@"wave10.png"]],nil];
NSArray *titles = @[@"John Appleseed", @"John Doe", @"Test User",@"John Appleseed", @"John Doe", @"Test User",@"John Appleseed", @"John Doe", @"Test User",
@"Test User",@"John Appleseed", @"John Doe", @"Test User",@"John Appleseed", @"John Doe", @"Test User",@"John Appleseed", @"John Doe", @"Test User",
@"Test User"];
[cell.imageView setAnimationImages:myImageArray];
cell.imageView.animationDuration = 4.20;
cell.imageView.animationRepeatCount = 1;
[cell.imageView startAnimating];
cell.textLabel.text = titles[indexPath.row];
return cell;
}
1。删除cellforatindexpath中的动画代码 2。当单元格显示时设置动画 3。隐藏单元格时停止动画 4。将提高应用程序的性能 5。节省更多内存
-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
NSArray *myImageArray = [NSArray arrayWithObjects:
[UIImage imageNamed:[NSString stringWithFormat:@"wave6.png"]],
[UIImage imageNamed:[NSString stringWithFormat:@"wave7.png"]],
[UIImage imageNamed:[NSString stringWithFormat:@"wave8.png"]],
[UIImage imageNamed:[NSString stringWithFormat:@"wave9.png"]],
[UIImage imageNamed:[NSString stringWithFormat:@"wave10.png"]],nil];
[cell.imageView setAnimationImages:myImageArray];
cell.imageView.animationDuration = 4.20;
cell.imageView.animationRepeatCount = 1;
[cell.imageView startAnimating];
}
-(void)tableView:(UITableView *)tableView didEndDisplayingCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
[cell.imageView stopAnimating];
[cell.layer removeAllAnimations];
}
而不是
[NSString stringWithFormat:@"wave6.png"]
您可以像@“wave6.png”一样指定。删除cellforatindexpath中的动画代码 2。当单元格显示时设置动画 3。隐藏单元格时停止动画 4。将提高应用程序的性能 5。节省更多内存
-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
NSArray *myImageArray = [NSArray arrayWithObjects:
[UIImage imageNamed:[NSString stringWithFormat:@"wave6.png"]],
[UIImage imageNamed:[NSString stringWithFormat:@"wave7.png"]],
[UIImage imageNamed:[NSString stringWithFormat:@"wave8.png"]],
[UIImage imageNamed:[NSString stringWithFormat:@"wave9.png"]],
[UIImage imageNamed:[NSString stringWithFormat:@"wave10.png"]],nil];
[cell.imageView setAnimationImages:myImageArray];
cell.imageView.animationDuration = 4.20;
cell.imageView.animationRepeatCount = 1;
[cell.imageView startAnimating];
}
-(void)tableView:(UITableView *)tableView didEndDisplayingCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
[cell.imageView stopAnimating];
[cell.layer removeAllAnimations];
}
而不是
[NSString stringWithFormat:@"wave6.png"]
您可以像@“wave6.png”一样指定UIImageView的创建对象和附加单元contentView
UIImageView *imageView = [[UIImageView alloc] initWithFrame:imageFrame];
imageView.animationImages = myImageArray;
imageView.animationDuration = 4.20;
imageView.animationRepeatCount = 1;
imageView.image = [UIImage imageNamed:@"wave5.png"]
; // you can put any image
[imageView startAnimating];
[cell.contentView addSubview:imageView];
创建UIImageView的对象和附加单元contentView
UIImageView *imageView = [[UIImageView alloc] initWithFrame:imageFrame];
imageView.animationImages = myImageArray;
imageView.animationDuration = 4.20;
imageView.animationRepeatCount = 1;
imageView.image = [UIImage imageNamed:@"wave5.png"]
; // you can put any image
[imageView startAnimating];
[cell.contentView addSubview:imageView];
可以在自定义单元类中使用animate方法 像这样
@interface CustomCell ()
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
isAnimating = NO;
[self startAni];
}
return self;
}
- (void)startAni{
if (!isAnimating){
isAnimating = YES;
}
//Animation Code here
}
- (void)stopAni{
if (isAnimating){
isAnimating = NO;
}
//Animation Code here
}
可以在自定义单元类中使用animate方法 像这样
@interface CustomCell ()
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
isAnimating = NO;
[self startAni];
}
return self;
}
- (void)startAni{
if (!isAnimating){
isAnimating = YES;
}
//Animation Code here
}
- (void)stopAni{
if (isAnimating){
isAnimating = NO;
}
//Animation Code here
}
这与您的问题无关,但出于最佳实践-
cellforrowatinexpth
不是您应该创建或初始化任何数组的地方。最好将初始化代码放在viewDidLoad
或其他位置。我尝试了您的解决方案。当我滚动下一行时,它对我很有效。你有没有检查过你是否给出了图片的正确名称??图像是否存在于资源中??首先将myimagearray与您的手机图像视图一起分配它与您的问题无关,但为了最佳实践-cellforrowatinexpth
不是您应该创建或初始化任何阵列的地方。最好将您的初始化代码放入viewDidLoad
或其他位置。我尝试了您的解决方案当我做下一行的滚动时,它对我很好。你有没有检查过你是否给出了图片的正确名称??图像是否存在于资源中??首先将myimagearray与您的细胞图像视图一起分配最初在可见细胞中图像必须存在,而向上滚动图像必须动画化,就像没有图像一样,并且图像必须缓慢出现。我们能做到这一点吗?@vijay。最初在可见细胞中图像必须存在,当向上滚动图像时,图像必须像没有图像一样动画化,并且图像必须缓慢地出现。我们能做到这一点吗?@vijay。