简体   繁体   English

按下按钮时更改UIButton图像

[英]Change UIButton image when button pressed

I'm trying to change the image button just in the moment that the button is pressed. 我正在尝试在按下按钮的瞬间更改图像按钮。 When released, i want go back to the original. 发布后,我想回到原版。

I read some posts here, but it's not working. 我在这里看了一些帖子,但它没有用。

Is it possible do it in the GUI without programming? 是否可以在没有编程的GUI中进行?

I tried this: 我试过这个:

-(IBAction) toggleUIButtonImage:(id)sender{
     if ([sender isSelected]) {
         [sender setImage:unselectedImage forState:UIControlStateNormal];
         [sender setSelected:NO];
     }else {
         [sender setImage:selectedImage forState:UIControlStateSelected];
         [sender setSelected:YES];
      }
 } 

But what kind of sender events should i associated the function toggleUIButtonImage ? 但是我应该将函数toggleUIButtonImage关联到什么样的发件人事件?

Thanks 谢谢

You can do it in interface builder by giving different settings to each button state config: 您可以通过为每个按钮状态配置提供不同的设置在界面构建器中执行此操作:

可用状态包括:默认,选中和禁用

Or you can also change the 'settings' of the button in code by using 'UIButton' setImage:forState method. 或者您也可以使用'UIButton'setImage:forState方法更改代码中按钮的“设置”。

    [myButton setImage:highlightedImage forState:UIControlStateHighlighted];

Usually, you should do it by 'configuring' the button as described above and not by changing the sub views of the button manually as a response to user interaction. 通常,您应该通过如上所述“配置”按钮而不是通过手动更改按钮的子视图作为对用户交互的响应来执行此操作。 The reason it doesn't work for you is the inherent hidden behaviour of UIButton. 它不适合你的原因是UIButton固有的隐藏行为。 You manually make the change of a subview of the button as a response to user interaction, but your change is changed back to what was defined in the settings of your button (according to the button's state). 您手动更改按钮的子视图作为对用户交互的响应,但您的更改将更改回按钮设置中定义的更改(根据按钮的状态)。 So it seems like your code doesn't do anything at all. 所以看起来你的代码根本不做任何事情。

in the ViewDidLoad Method ViewDidLoad方法中

set the property of 设置属性

[[button setImage:[UIImage ImageNamed:@"unselectedImage.png"] forState: UIControlStateNormal]; 
[[button setImage:[UIImage ImageNamed:@"selectedImage.png"] forState: UIControlStateHighlighted ];

This can be done with Interface Builder, no code necessary. 这可以通过Interface Builder完成,无需代码。

Select your button in IB, go to the right sidebar, change the Type to Custom, make sure State Config is Default, and type in the name of your "regular image" reference in the Background Image text field. 在IB中选择您的按钮,转到右侧边栏,将“类型”更改为“自定义”,确保“状态配置”为“默认”,然后在“背景图像”文本字段中键入“常规图像”参考的名称。

Then change State Config to Highlighted, and type in the name of your "pressed image" reference in the Background Image text field. 然后将状态配置更改为突出显示,并在背景图像文本字段中键入“按下的图像”参考的名称。

In IB, you can set the image for the Highlighted (not Selected) state of the button. 在IB中,您可以为按钮的突出显示(未选定)状态设置图像。 That should do it. 应该这样做。

Look in the Button section of the Utilities pane, and select Highlighted from the State Config drop-down. 查看“实用工具”窗格的“按钮”部分,然后从“状态配置”下拉列表中选择“突出显示”。

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

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