当用户按下该按钮删除上一张图像时,更改UIButton上的图像

时间:2012-06-18 17:23:34

标签: iphone objective-c xcode image uibutton

当用户按下UIButton时,我想更改我的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];
}}

1 个答案:

答案 0 :(得分:1)

试试这样:

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(setRightActionToButton:) forControlEvents:UIControlEventTouchUpInside];
[[self view] addSubview:overlayButton];

-(void)setRightActionToButton:(id)sender {
    UIButton *button = (UIButton *)sender;

    if (shape==1 && people==2) {
        [button setImage:[UIImage imageNamed:@"image2.png"] forState:UIControlStateNormal];
        [self scanButtonPressed:button]
    }

    else if (shape==2 && people==2) {
        [button setImage:[UIImage imageNamed:@"image3.png"] forState:UIControlStateNormal];
        [self buttonshapeClicked:button]
    }

    else if (shape==3 && people==2) {
        [button setImage:[UIImage imageNamed:@"image4.png"] forState:UIControlStateNormal];
        [self changeshape:button];
    }
}