0

当用户按下它时,我想更改我的 UIButton 上的图像。图像具有透明效果(它是仅显示边缘的 .png)。问题是,当点击按钮时,一切正常(连接到它的其他功能),但应用程序会覆盖 2 个不同的图像一个超过一秒!按下按钮时,我想显示第二张图像,并删除第一张图像(这变得不必要)。PS:我不想使用图像数组。这里是按钮的代码:

UIButton *overlayButton = [UIButton buttonWithType:UIButtonTypeCustom];
[overlayButton setImage:[UIImage imageNamed:@"image1.png"] forState:UIControlStateNormal];
[overlayButton setFrame:CGRectMake(20, 382, 120, 60)];
[overlayButton addTarget:self action:@selector(scanButtonPressed) forControlEvents:UIControlEventTouchUpInside]; //just graphics
[overlayButton addTarget:self action:@selector(buttonshapeClicked:) forControlEvents:UIControlEventTouchUpInside]; //just to set a variable i++ for my cycle
[overlayButton addTarget:self action:@selector(changeshape:) forControlEvents:UIControlEventTouchUpInside];  //function that changes another shape
[[self view] addSubview:overlayButton];    

我的简单循环

- (void)buttonshapeClicked:(id)sender{
shape++;
if (shape == 4) {
    shape = 1;
        }}     

这里是允许更改屏幕上主要形状的主要功能

 -(void) changeshape:(id)sender {
if (shape==1 && people==2) {
    //bottone forma
    UIButton *overlayButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [overlayButton setImage:[UIImage imageNamed:@"passaquadrato.png"] forState:UIControlStateNormal];
    [overlayButton setFrame:CGRectMake(20, 382, 120, 60)];
    [overlayButton addTarget:self action:@selector(scanButtonPressed) forControlEvents:UIControlEventTouchUpInside];
    [overlayButton addTarget:self action:@selector(buttonshapeClicked:) forControlEvents:UIControlEventTouchUpInside];
    [overlayButton addTarget:self action:@selector(changeshape:) forControlEvents:UIControlEventTouchUpInside];
    [[self view] addSubview:overlayButton];

    //bottone people
    UIButton *overlayButtonp = [UIButton buttonWithType:UIButtonTypeCustom];
    [overlayButtonp setImage:[UIImage imageNamed:@"passatre.png"] forState:UIControlStateNormal];
    [overlayButtonp setFrame:CGRectMake(180, 382, 120, 60)];
    [overlayButtonp addTarget:self action:@selector(scanButtonPressed) forControlEvents:UIControlEventTouchUpInside];
    [[self view] addSubview:overlayButtonp];

    UIImageView *overlayImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"circle2.png"]];
    [overlayImageView setFrame:CGRectMake(20, 88, 280, 280)];
    [[self view] addSubview:overlayImageView];
}


else if (shape==2 && people==2) {
    //bottone forma
    UIButton *overlayButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [overlayButton setImage:[UIImage imageNamed:@"passarettangolo.png"] forState:UIControlStateNormal];
    [overlayButton setFrame:CGRectMake(20, 382, 120, 60)];
    [overlayButton addTarget:self action:@selector(scanButtonPressed) forControlEvents:UIControlEventTouchUpInside];
    [overlayButton addTarget:self action:@selector(buttonshapeClicked:) forControlEvents:UIControlEventTouchUpInside];
    [overlayButton addTarget:self action:@selector(changeshape:) forControlEvents:UIControlEventTouchUpInside];
    [[self view] addSubview:overlayButton];

    //bottone people
    UIButton *overlayButtonp = [UIButton buttonWithType:UIButtonTypeCustom];
    [overlayButtonp setImage:[UIImage imageNamed:@"passatre.png"] forState:UIControlStateNormal];
    [overlayButtonp setFrame:CGRectMake(180, 382, 120, 60)];
    [overlayButtonp addTarget:self action:@selector(scanButtonPressed) forControlEvents:UIControlEventTouchUpInside];
    [[self view] addSubview:overlayButtonp];

    UIImageView *overlayImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"square2.png"]];
    [overlayImageView setFrame:CGRectMake(20, 88, 280, 280)];
    [[self view] addSubview:overlayImageView];
}}

我想要相同的按钮,但要显示不同的图像

4

2 回答 2

1

看起来您从未删除过以前的按钮。

此外,您不必创建新按钮 - 您可以简单地存储对按钮的引用、更改图像并更改操作目标(通过在添加新按钮之前删除旧按钮)。

于 2012-06-18T21:57:45.773 回答
0

我已经解决了几个周期,从视图中删除所有内容:

for (int i = 0; i < [[self.view subviews] count]; i++ ) {
        [[[self.view subviews] objectAtIndex:i] removeFromSuperview];
    }

现在的问题是重新初始化循环,因为当我的变量变为 4 时,我希望它变为 1。我这样做:

- (void)buttonshapeClicked:(id)sender{shape++;
if (shape == 4) {
    shape = 1;
}}

但是在数字 4 之后,我遇到了和以前一样的概览问题!

于 2012-06-19T07:38:22.650 回答