2014-02-06 68 views
-1
NSString *butterfly = [NSString stringWithFormat:@"brush_%d.png", i] 

我想在單獨的該字符串這樣如何添加字符串和數組

  1. brush_
  2. %d.png

,因爲我有很多的動畫功能都在那裏我的項目(0至39)。

if(counter == 0) { 
    NSMutableArray *dashBoy = [NSMutableArray array]; 
    for (i = 1; i<= 13; i++) { 
     butterfly = [NSString stringWithFormat:@"brush_%d.png", i]; 
     if ((image = [UIImage imageNamed:butterfly])) 
      [dashBoy addObject:image]; 
    } 

    [stgImageView setAnimationImages:dashBoy]; 
    [stgImageView setAnimationDuration:2.0f]; 
    [stgImageView startAnimating]; 
} 

. 
. 

if(counter == 39) { 
    NSMutableArray *dashBoy1 = [NSMutableArray array]; 
    for (i = 1; i <= 30; i++) { 
     butterfly = [NSString stringWithFormat:@"catch_%d.png", i]; 
     if ((image = [UIImage imageNamed:butterfly])) 
      [dashBoy1 addObject:image]; 
    } 

    [stgImageView setAnimationImages:dashBoy1]; 
    [stgImageView setAnimationDuration:7.20f]; 
    [stgImageView startAnimating]; 
} 

我試過,但它是不正確

NSArray *c = [NSArray arrayWithObjects:@"brush", @"catch", @"clap",@"dog", nil]; 

//NSString *str = @"brush_"; 
NSString *str = [c componentsJoinedByString:@""]; 
for (int i=0; i<=3;i++) 
    str = [str stringByAppendingFormat:@"_%i.png ",i]; 
    NSLog(@"%@",str); //Output is brushcatchclapdog_0.png _1.png _2.png _3.png 

我想

brush_0.png, catch_1.png, clap_2.png in array 

在這裏,我想用一個函數結合的循環以上category.It有可能獲得一個功能結合上述39動畫功能。

回答

0
[self animateWithCategory:@"brush_" WithCount:13 duration:0.2f] 

-(void)animateWithCategory:(NSString *)strCategory WithCount:(CGFloat)count duration:(NSInteger)duration{ 
     NSMutableArray *dashBoy = [NSMutableArray array]; 
     for (int i = 1; i<= count; i++) { 
      NSString* butterfly = [NSString stringWithFormat:@"%@%d.png",strCategory ,i]; 
      UIImage *image=[UIImage imageNamed:butterfly]; 
      [dashBoy addObject:image]; 
     } 

     [stgImageView setAnimationImages:dashBoy]; 
     [stgImageView setAnimationDuration:duration]; 
     [stgImageView setAnimationRepeatCount:1]; 
     [stgImageView startAnimating]; 
    } 
+0

謝謝Sunny。它的工作 – user3069029

+0

動畫重複是無限的。但我想重複一次 – user3069029

+0

使用setAnimationRepeatCount –

0

根據您所需的輸出:

我想

brush_0.png,catch_1.png,在陣列clap_2.png

你可以嘗試這樣的:

NSArray *c = [NSArray arrayWithObjects:@"brush", @"catch", @"clap",@"dog", nil]; 
NSMutableArray *o = [[NSMutableArray alloc] initWithCapacity:[c count]]; 
//NSString *str = @"brush_"; 
//NSString *str = [c componentsJoinedByString:@""]; 
for (int i=0; i<=3;i++) 
{ 
    NSString *str = [[c objectAtIndex:i] stringByAppendingFormat:@"_%i.png ",i]; 
    NSLog(@"%@",str); //Output is brushcatchclapdog_0.png _1.png _2.png _3.png 

    [o addObject:str]; 
}//brush_0.png, catch_1.png, clap_2.png in array 
NSLog(@"Output array : %@", o); 

登錄輸出代碼:

2014-02-07 00:31:29.020 DesignMantic[807:70b] brush_0.png 
2014-02-07 00:31:29.023 DesignMantic[807:70b] catch_1.png 
2014-02-07 00:31:29.024 DesignMantic[807:70b] clap_2.png 
2014-02-07 00:31:29.025 DesignMantic[807:70b] dog_3.png 
2014-02-07 00:31:29.026 DesignMantic[807:70b] Output array : (
    "brush_0.png ", 
    "catch_1.png ", 
    "clap_2.png ", 
    "dog_3.png " 
)