开发者

Add Image on Every click of same button

开发者 https://www.devze.com 2023-03-25 02:09 出处:网络
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.

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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号