按钮改变iPhone上的背景图像

时间:2011-12-15 18:03:24

标签: iphone ios

我有一个小错误。每个按钮我有两个代码片段。一个是这样的:

-(IBAction)clickoneButton:(id)sender
{
    [sender setBackgroundImage:[UIImage imageNamed:@"cnumber_1.jpg"] forState:UIControlStateNormal];
}

另一个是这个:

-(IBAction)oneButton:(id)sender
{
    self.enteredPhoneNumberString = [self.enteredPhoneNumberString stringByAppendingString:[NSString stringWithFormat:@"%d", 1]];
    [self updateFormattedPhoneNumberLabel];

    [sender setBackgroundImage:[UIImage imageNamed:@"Number_1.jpg"] forState:UIControlStateNormal];
}

第一个设置为Touch Down。另一个是Touch Up Inside。该应用程序看起来像普通拨号电话。所以第一个图像是浅蓝色按钮,第二个图像是深蓝色按钮,就像iPhone拨号器一样。问题是,当我按下按钮时,几乎看起来暗图像仍然在Touch Down浅蓝色图像后面。它正在变为浅色图像,但阴影几乎就像黑暗图像通过它或其他东西透明。不知道怎么解释它。如果你有任何想法,为什么淡蓝色的一些变暗,当Touch Down正在发生时,我将不胜感激任何帮助。感谢

Button before touch and after release 触摸前和释放后按钮(正确)

Button on Touch Down 触摸按下按钮(不能正常工作)

What the Touch Down button should look like 触摸按钮应该是什么样子

1 个答案:

答案 0 :(得分:3)

根据您提供的两段代码,您不会在按下按钮时设置图像。在加载视图时尝试更新代码以使用以下内容:

// sets the background image for when the button is not pressed
[sender setBackgroundImage:[UIImage imageNamed:@"cnumber_1.jpg"] forState:UIControlStateNormal];

// sets the background image for when the button is pressed
[sender setBackgroundImage:[UIImage imageNamed:@"Number_1.jpg"] forState:UIControlStateHighlighted];
相关问题