简体   繁体   English

单击时如何更改UIButton的背景图像?

[英]How to change UIButton's background image while clicking it?

I have a UIButton in each of my tableview's cells. 我在每个tableview的单元格中都有一个UIButton。 I want to glow the uibutton while click action happends. 我想在单击操作发生时发出uibutton。 I set an image as its background image using the code 我使用代码将图像设置为其背景图像

UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];

button.frame = CGRectMake(270,10,30,30);

[button setImage:[UIImage imageNamed:@"image.png"] forState:UIControlStateNormal];

[button addTarget:self action:@selector(action:) forControlEvents:UIControlEventTouchUpInside];

button.backgroundColor = [UIColor clearColor];

The image.png is little bit darker image. image.png有点暗。 Because i want to show a click effect by showing a glowed image. 因为我想通过显示发光的图像来显示点击效果。

Now shall i glow the image.png by code while clicking happends? 现在我应该在单击事件时通过代码发光image.png吗? Or should i create a new glowing image(say image_glow.png) with the same dimension and replace the previous one? 或者我应该创建一个具有相同尺寸的新发光图像(例如image_glow.png)并替换上一个图像?

Whatever, please explain how to implement this.... 无论如何,请解释如何实现此目标。

The best way is to use multiple images. 最好的方法是使用多个图像。

You can set different image to different state of a UIButton by using setImage method. 您可以使用setImage方法将不同的图像设置为UIButton的不同状态。

As i see, you have set image only for UIControlStateNormal state, which makes all other states of a button to use the same image. 如我所见,您只为UIControlStateNormal状态设置了图像,这使按钮的所有其他状态都可以使用同一图像。

reference : UIButton States 参考: UIButton状态

Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM