背景UIButton的图像有时不会随着touchUpInside事件而改变

时间:2012-09-27 08:40:06

标签: iphone ios uibutton touch-up-inside

UIButton设置如下代码:

UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 27, 27)];
[btn setBackgroundImage:[UIImage imageNamed:@"imgUp.png"] forState:UIControlStateNormal];
[btn setBackgroundImage:[UIImage imageNamed:@"imgDown.png"] forState:UIControlStateHighlighted];
[btn addTarget:self action:@selector(btnPressed:) forControlEvents:UIControlEventTouchUpInside];

如果我快速触摸btw,则不会显示imgDown.png,但会触发操作btnPressed:。怎么可能修好?任何帮助表示赞赏:)

4 个答案:

答案 0 :(得分:1)

在您的代码中添加以下行:

[btn setBackgroundImage:[UIImage imageNamed:@"imgDown.png"] forState:UIControlStateSelected];

答案 1 :(得分:0)

你应该为btnPressed:function中的按钮(btn)调用setHiglighted:方法。

答案 2 :(得分:0)

以下代码可以解决问题。

-(void)btnPressedDelay{

    //original code in btnPressed: method
}

- (void) btnPressed:(id)sender
{    

    [self performSelector:@selector(btnPressedDelay) withObject:nil afterDelay:0];
// 0 did the magic here

}

答案 3 :(得分:0)

它的工作正常我会测试它请检查你的图像并试一试:

UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 27, 27)];
[button setBackgroundImage:[UIImage imageNamed:@"rainy.jpg"] forState:UIControlStateNormal];
[button setBackgroundImage:[UIImage imageNamed:@"Icon.png"] forState:UIControlStateHighlighted];
[button addTarget:self action:@selector(btnPressed:) forControlEvents:UIControlEventTouchUpInside];
[self.view button];