简体   繁体   English

按下按钮时如何更改图像?

[英]How do I change an Image when a button is pressed?

Now I know this question may sound like a duplicate, but I doubt it seeing as I've researched and none of the possible duplicates answer my question. 现在我知道这个问题可能听起来像是重复的,但是我怀疑它是在我研究过的时候看到的,并且没有一个可能的副本回答我的问题。 If it is a duplicate and it's been answered by all means show me the way! 如果它是重复的,它已经通过各种方式回答给我指路!

But my question doesn't deal so much with a button changing an image, more so the button is the image. 但是我的问题并没有处理更改图像的按钮,更多的是按钮是图像。

So my button is an image and when pressed a sound plays and then the image changes until the sound is done. 所以我的按钮是一个图像,按下时会播放声音,然后图像会改变,直到声音完成。 My problem is I can't get the image to change. 我的问题是我无法改变图像。

Now I have tried this method thinking that I could press my button and my new image would cover up my button until the sound was done: 现在我尝试了这种方法,认为我可以按下我的按钮,我的新图像将覆盖我的按钮,直到声音完成:

UIImage *dolDerp = [UIImage imageNamed:@"dolphin2.png"];

[imageview setImage:dolDerp];

I have an outlet set up and it's connected to an image view object and then the action is connected to the button, so in theory when the button is pressed the new image should take over the screen. 我有一个插座设置,它连接到图像视图对象,然后动作连接到按钮,所以理论上按下按钮时,新图像应该接管屏幕。 Now obviously the code needs to be tweaked so the button would go away after a few seconds when the sound is played, but my problem is I can't even get the button to display. 现在很明显代码需要调整,以便在播放声音几秒后按钮会消失,但我的问题是我甚至无法显示按钮。 I also would prefer to just have the button objects image change if possible? 如果可能的话,我也更愿意让按钮对象图像改变? If anyone could offer some help it's much appreciated! 如果有人能提供一些帮助,我们非常感谢!

the best thing is in Interface Builder assign the two images to the button Normal/Selected states. 最好的事情是在Interface Builder中将两个图像分配给按钮Normal / Selected状态。

in code just use this line: 在代码中只使用这一行:

myButton.selected = YES;
//or 
myButton.selected = NO;

What @Mahmoud Fayez suggested is good. @Mahmoud Fayez建议的是好的。 In code, the proper way to set a buttons image/background image is [button setImage:image forState:UIControlStateNormal] or the similar method for setBackgroundImage 在代码中,设置按钮图像/背景图像的正确方法是[button setImage:image forState:UIControlStateNormal]或setBackgroundImage的类似方法

If you are using IB though, it is indeed best to set the different images for different states in IB, then change the buttons state (which will then change the buttons image) in code. 如果你正在使用IB,那么确实最好在IB中为不同的状态设置不同的图像,然后在代码中更改按钮状态(然后将更改按钮图像)。

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

相关问题 如何在按下按钮时更改按钮的颜色,并在按下其他按钮时重置为原始颜色? - How do I change a button's color when pressed and reset to original color when a different button is pressed? 按下按钮时更改图像 - Change image when button is pressed 迅捷的…如何在按下按钮并将其恢复为正常状态时更改图像? - Swift… how can I make an image change when a button its pressed and return it back to normal? 按下按钮时如何更改自定义按钮图像 - how to change a custom buttons image when the button is pressed 我如何设置标签,以便在按下按钮时它将更改标签上显示的值 - How do i set up a label so that when a button is pressed then it will change that value displayed on the label 未按下按钮时如何调用按钮功能 - How do I call a button function when the button is not being pressed 如何点击时更改按钮的图像 - Swift - How do I change the image of a button when tapped - Swift 当按下TableView单元格中的按钮时,我怎么知道按下了哪一行单元格的按钮? - When the button in TableView cell pressed, how do I know the button of which row of cell pressed? 按下后退按钮时如何执行操作 - How do I perform an operation when the back button is pressed 按下按钮时如何返回下一个数组? - How do I return the next array when a button is pressed?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM