【问题标题】:Add Image on Every click of same button每次单击相同按钮时添加图像
【发布时间】:2011-08-03 08:11:33
【问题描述】:

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

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

【问题讨论】:

  • 我认为 Terente Ionut Alexandru 的回答非常好。因此,如果这对您有用,请尝试并接受它。

标签: iphone objective-c uiimageview uibutton uiimage


【解决方案1】:

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

【讨论】:

  • 但我想在每次点击时不断添加该图像,因为您的答案图像只会出现一次?请建议一些代码或示例来支持您的答案......
  • 粘贴用于创建按钮的代码及其选择器方法。
  • 通过向按钮添加标签并在每次计数时增加标签来完成。
【解决方案2】:

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

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

【讨论】:

【解决方案3】:

如果我理解您的问题,您希望通过点击同一个按钮将图像添加到不同的位置???如果是,

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-31
    • 2019-07-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多