我正在尝试为默认存在的 tableview
单元格的 imageview
设置动画,我希望 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"那样指定
关于ios - tableview cell.imageview 没有动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23558096/