Iphone SDK: Use the same UIImageView at different places in a UIIView?
I would add the same UIImageView
at different places to my View.
So I could make ten UIImageViews
with the same image but with other names. But I got ca. 50 UIImageViews
.
I make my UIImageView with this code:
Code:
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:im开发者_JS百科age;
Now I would like to change my dracRect.origin
point and make a new UIImageView
with another name. I would do this in a for
loop. Is this possible?
Code:
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;
}
Update:
I tried it with image%i
and with image%d
but I get an error:
conflicting types of 'string1'
did you try it? it should work. perhaps you have to release your UIImageView after you added the Subview, but i am not sure. i would bring the origin-calculation also in the CGRectMake but shouldn't be important. and don't you have to write: ...initWithFormat:@"image%i",x ? not sure, i am a xcode noob... :) i did something similar in an non-iphone project with matrices. but it's not possible to make matrices in the iphone sdk i believe.
精彩评论