【发布时间】:2011-08-03 08:11:33
【问题描述】:
我的应用程序中有一个图像和一个按钮。我想在每次单击同一个按钮时一次又一次地添加相同的图像。
但是我不知道怎么做,请帮忙!!!
【问题讨论】:
-
我认为 Terente Ionut Alexandru 的回答非常好。因此,如果这对您有用,请尝试并接受它。
标签: iphone objective-c uiimageview uibutton uiimage
我的应用程序中有一个图像和一个按钮。我想在每次单击同一个按钮时一次又一次地添加相同的图像。
但是我不知道怎么做,请帮忙!!!
【问题讨论】:
标签: iphone objective-c uiimageview uibutton uiimage
声明一个新的 UIImageView 并将其作为子视图添加到您的 UIButton。
【讨论】:
在视图中添加一个按钮,删除标题并将类型设置为切换(在检查器的属性选项卡中)。这里还设置按钮的图像和备用图像,如下所示:
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];
【讨论】:
如果我理解您的问题,您希望通过点击同一个按钮将图像添加到不同的位置???如果是,
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];
这将时间用作种子,因此如果您非常快速地点击,可能会生成相同的值,并且似乎没有添加任何内容,因为新的图像视图与其他一些图像视图重叠
【讨论】: