0

这会为 Impactdrawarray 中的 UIImageView 设置动画:

if ( ((impact *) [impactarray objectAtIndex:iv]).animframe == 70){
   ((UIImageView *) [impactdrawarray objectAtIndex:iv]).image =
          [UIImage imageWithContentsOfFile:
                         [[NSBundle mainBundle] pathForResource:@"explosionA71"
                                                         ofType:@"png"]];
}
if ( ((impact *) [impactarray objectAtIndex:iv]).animframe == 71){
       ((UIImageView *) [impactdrawarray objectAtIndex:iv]).image =
                       [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] 
                                        pathForResource:@"explosionA72"
                                                 ofType:@"png"]];
}

什么都没有出现:

NSString *myString2 =
           [NSString stringWithFormat:@"A%d",
                     ((impact *) [impactarray objectAtIndex:iv]).animframe;

((UIImageView *) [impactdrawarray objectAtIndex:iv]).image =
                    [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] 
                                     pathForResource:(@"explosion%d", myString2)
                                              ofType:@"png"]];

我使用 NSString 的参考(示例)

NSString *myString23 = [NSString stringWithFormat:@"$%d", money];
moneylabel.text = myString23;

如何在文件名中使用变量?有没有办法通过索引在资源文件夹中加载图像?像imageByIndex:currentanimationframe什么?我在 UIImage 文档中没有看到任何内容。

回复评论:

   NSString *myString2 = [NSString stringWithFormat:@"A%d", ((impact *) [impactarray objectAtIndex:iv]).animframe];
  ((UIImageView *) [impactdrawarray objectAtIndex:iv]).image = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"explosion%d", myString2] ofType:@"png"]];

                NSString *imagename = [NSString stringWithFormat:@"A%d", ((impact *) [impactarray objectAtIndex:iv]).animframe];
                UIImage *myImage = [UIImage imageNamed:imagename];

                ((UIImageView *) [impactdrawarray objectAtIndex:iv]).image = myImage;

都编译和运行,但图像不显示。

4

2 回答 2

2

使用+stringWithFormat:,就像在您的示例中一样:

[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"explosion%d", myString2] ofType:@"png"];
于 2009-06-24T23:46:59.060 回答
0
NSString* imageName = [self dynamicallyCalculateImageNameUsingCrazyMath];
UIImage* myImage = [UIImage imageNamed:imageName];
于 2009-06-24T23:41:48.947 回答