我试图在"表视图单元格"中动画UIImageview
,但图像从未显示。这是我要使用的代码。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
if (cell == nil)
{
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"];
cell.textLabel.font = [UIFont systemFontOfSize:16.0];
}
if (cell)
{
cell.textLabel.text = [titleArray objectAtIndex:indexPath.row];
cell.textLabel.textColor = [UIColor darkGrayColor];
UIImage *musicOne = [UIImage imageNamed:@"music1.png"];
UIImage *musicTwo = [UIImage imageNamed:@"music2.png"];
UIImage *musicThree = [UIImage imageNamed:@"music3.png"];
NSArray *imagesArray = [NSArray arrayWithObjects:musicOne, musicTwo, musicThree, nil];
if (indexPath.row == 0)
{
cell.imageView.animationImages = imagesArray;
cell.imageView.animationDuration = 1.0f;
cell.imageView.animationRepeatCount = 0;
[cell.imageView startAnimating];
}
}
return cell;
}
如果我不尝试对多个图像进行动画处理,并且在图像视图中只使用一个图像,则图像将显示在单元格中。
cell.imageView.image = [imagesArray objectAtIndex:0];
必须将cell.imageView
的image
属性与animationImages
一起设置,否则UITableViewCell
将无显示。
为了提高应用程序的性能并节省内存,请执行以下操作:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
cell.imageView.image = [UIImage imageNamed:@"music1.png"];
return cell;
}
-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
UIImage *musicOne = [UIImage imageNamed:@"music1.png"];
UIImage *musicTwo = [UIImage imageNamed:@"music2.png"];
UIImage *musicThree = [UIImage imageNamed:@"music3.png"];
NSArray *imagesArray = @[musicOne, musicTwo, musicThree];
[cell.imageView setAnimationImages:imagesArray];
cell.imageView.animationDuration = 1.0f;
[cell.imageView startAnimating];
}
-(void)tableView:(UITableView *)tableView didEndDisplayingCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
[cell.imageView stopAnimating];
[cell.layer removeAllAnimations];
}
尝试给imageView一个标签(例如1),然后得到这样的图像视图:
UIImage *musicOne = [UIImage imageNamed:@"music1.png"];
UIImage *musicTwo = [UIImage imageNamed:@"music2.png"];
UIImage *musicThree = [UIImage imageNamed:@"music3.png"];
NSArray *imagesArray = [NSArray arrayWithObjects:musicOne, musicTwo, musicThree, nil];
if (indexPath.row == 0)
{
UIImageView *imageView = (UIImageView *)[cell viewWithTag:1];
imageView.animationImages = imagesArray;
imageView.animationDuration = 1.0f;
imageView.animationRepeatCount = 0;
[imageView startAnimating];
}
同样的代码也适用于我。我知道在UIScrollView
动画会停止,当视图滚动,因为这个原因,你应该使用CADisplayLink
运行你的动画在NSRunLoopCommonModes
而不是在NSDefaultRunLoopMode
,但我已经测试了你的代码,它的工作。