2011-08-03 157 views
0

我在应用程序中有一个图像和一个按钮。我想多次添加相同的图像,每次点击相同的按钮。每次点击同一个按钮时添加图片

但我不知道如何,请帮助!!!

+0

我觉得Terente Ionut Alexandru的回答非常好。所以试试我,并接受它,如果这对你有用。 –

回答

2

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

+1

+ 1为快速和有效的答案:) –

+0

但我想连续添加每次点击图像,因为您的答案图像将只出现一次?请提供一些支持您答案的代码或示例...... –

+0

粘贴您用于创建按钮的代码及其选择器方法。 –

0

向视图中添加一个按钮,删除标题并设置切换类型(在检查器的属性选项卡中)。这里还设置了形象和按钮的备用图像是这样的:

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

http://stackoverflow.com/questions/1486291/i-want-to-switch-button-image-on-click – ram

+0

我不想切换图像,我想显示3个相同的图像点击3次或数量时间用户点击那个按钮。 –

0

如果我明白你的问题,你想要在不同的地方添加图像,并点击相同的按钮?如果是的话,

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

这将使用时间作为种子,所以如果你点击速度非常快,可以产生相同的值,它会看起来像什么都不添加,因为新的ImageView的是重叠的其他一些imageview的

相关问题