每次单击同一按钮时添加图像

发布于 2024-11-27 12:49:38 字数 73 浏览 1 评论 0原文

我的应用程序中有一张图像和一个按钮。我想在每次单击同一按钮时一次又一次地添加相同的图像。

但我不知道怎么办,请帮忙!

I have 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 !!!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

紫﹏色ふ单纯 2024-12-04 12:49:38

声明一个新的 UIImageView 并将其作为子视图添加到 UIButton 中。

Declare an new UIImageView and add it as a subview to your UIButton.

对你再特殊 2024-12-04 12:49:38

在视图中添加一个按钮,删除标题并将类型设置为切换(在检查器的属性选项卡中)。在这里还设置按钮的图像和备用图像,如下所示:

属性检查器http://img340.imageshack.us/img340/2310/bildschirmfoto20090928u.png

应该可以。

如果您想使用自定义图像,则必须以编程方式执行此操作,如下

NSString* path  = [[NSBundle mainBundle] pathForResource:@"myImage" 
                              ofType:@"png"];
NSURL* url      = [NSURL fileURLWithPath:path];
NSImage *image  = [[NSImage alloc] initWithContentsOfURL: url];

[myButton setImage: image];

所示:对于备用图像分别:

[myButton setAlternateImage: image2];

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];
你对谁都笑 2024-12-04 12:49:38

如果我理解你的问题,你希望通过点击同一个按钮在不同的地方添加图像???如果是,

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];

这使用时间作为种子,因此如果您点击得非常快,可能会生成相同的值,并且看起来好像没有添加任何内容,因为新的图像视图与其他一些图像视图重叠

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文