Iphone SDK:在UIIView的不同位置使用相同的UIImageView?

时间:2010-09-18 08:48:47

标签: iphone uiimageview subview

我会在我的视图的不同位置添加相同的UIImageView

所以我可以使用相同的图像制作十个UIImageViews,但使用其他名称。但我得到了ca. 50 UIImageViews

我用这段代码创建了我的UIImageView:

代码:

CGRect dragRect = CGRectMake(0.0f, 0.0f, 70.0, 70.0); 
dragRect.origin = CGPointMake(10, 300); 

UIImageView *image = [[UIImageView alloc] initWithFrame:dragRect];
[image setImage:[UIImage imageNamed:@"Apple.png"]];

[self.view addSubview:image;

现在,我想更改dracRect.origin点并使用其他名称制作新的UIImageView。我会在for循环中执行此操作。这可能吗?

代码:

int x;
for (x=0; x>=3; x=x+1) {

  CGRect dragRect = CGRectMake(0.0f, 0.0f, 70.0, 70.0); 
  //x koordinate wird immer 10 grösser 
  dragRect.origin = CGPointMake(x*10, 300);  

  //image1, image2, image3
  NSString *string1 = [[string1 alloc] initWithFormat:@"image%d",x]; 

  UIImageView *string1 = [[UIImageView alloc] initWithFrame:dragRect];
  [string1 setImage:[UIImage imageNamed:@"Apple.png"]];

  [self.view addSubview:string1;
}

更新

我使用image%iimage%d尝试了但是我收到了错误:

  

'string1'的冲突类型

1 个答案:

答案 0 :(得分:0)

你尝试过吗? 它应该工作。也许你必须在添加Subview后发布你的UIImageView,但我不确定。我会将原点计算也带到CGRectMake中,但不应该很重要。 你不必写:... initWithFormat:@“image%i”,x? 不确定,我是一个xcode noob ... :) 我在带有矩阵的非iphone项目中做了类似的事情。但是我不相信在iphone sdk中制作矩阵。