Add Image on Every click of same button
I ha开发者_JAVA技巧ve one image and one button in my application. i want to add the same image again and again on every click of same button multiple times.
But i dont know how, Please help !!!
Declare an new UIImageView and add it as a subview to your UIButton.
Add a button to your view, remove the title and set the type to toggle(in the attributes tab of the inspector). Here also set the image and alternate image of your button like this:
attributes inspector http://img340.imageshack.us/img340/2310/bildschirmfoto20090928u.png
That should do it.
If you want to use a custom image you will have to do it programatically like this:
NSString* path = [[NSBundle mainBundle] pathForResource:@"myImage"
ofType:@"png"];
NSURL* url = [NSURL fileURLWithPath:path];
NSImage *image = [[NSImage alloc] initWithContentsOfURL: url];
[myButton setImage: image];
and respectively for the alternate image:
[myButton setAlternateImage: image2];
If I understand your question, you want an image to be added at different places with tap on same button??? If yes,
UIImage *image = [UIImage imageNamed:@"Icon-72.png"];
int width = image.size.width;
int height = image.size.height;
srand(time(0));
int x = (rand() % 320);
int y = (rand() % 480);
if (x+width > 320) {
x = x- width;
}
if (y+height > 4800) {
x = x- height;
}
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(x, y, width, height)];
imageView.image = image;
[self.view addSubview:imageView];
[imageView release];
[self.view bringSubviewToFront:sender];
This uses time as seed so if you tap very quickly, same values may be generated and it will seem like nothing is added because the new imageview is overlapping some other imageview
精彩评论