在按钮上切换图像单击iOS

时间:2014-07-16 06:57:28

标签: ios audio

我的应用程序有一个声音图像,无论何时单击它,声音都会关闭,图像会发生变化。但是,每当我再次按下它以将声音重新打开时,图像就会突出显示并保留旧图像。这是我的代码。帮助将不胜感激。

 -(IBAction)pauseSound
{
    if (![sound isPlaying]) {
        [sound play];
        [button setBackgroundImage:[UIImage imageNamed:@"Sound On iOS.png"] forState:UIControlStateNormal];
    }else {
        [sound pause];
        [button setBackgroundImage:[UIImage imageNamed:@"Sound Off iOS.png"] forState:UIControlStateReserved];

3 个答案:

答案 0 :(得分:1)

您可以look at the documentation获得UIControl的控件状态,对于您的查询,您可以使用UIControlStateNormal代替UIControlStateReserved

答案 1 :(得分:0)

尝试以下方法:

[button setBackgroundImage:[UIImage imageNamed:@"Sound Off iOS.png"] forState:**UIControlStateNormal**];

答案 2 :(得分:0)

您正在设置UIControlStateReserved的关闭图像,因此当按钮状态恢复正常(UIControlStateNormal)时,它将返回上一张图像。

要修复您需要更改:

[button setBackgroundImage:[UIImage imageNamed:@"Sound Off iOS.png"] forState:UIControlStateReserved];

为:

[button setBackgroundImage:[UIImage imageNamed:@"Sound Off iOS.png"] forState:UIControlStateNormal];