ios - OnTouchUp 和 OnTouchDown 上的 Swift UIButton 背景更改?

标签 ios swift background uibutton ontouch

我正在使用 swift,在我的应用程序中有一个 UIButton,我想在按下和按下时更改背景颜色和边框颜色。

最佳答案

您可以只创建两个函数并将它们连接到适当的事件。像这样:

@IBAction func up(){
    myButton.backgroundColor = UIColor.whiteColor()
}

@IBAction func down(){
    myButton.backgroundColor = UIColor.blackColor()
}

现在只需在界面生成器中连接它们。函数 up() 连接到 Touch Up Inside 和 Touch Up Outside。函数 down() 来触地。

但是有一个更好的方法,使用你的按钮并在代码中为状态设置他的背景图像。

//this will show when button is released
myButton.setBackgroundImage(UIImage(named:"background_image_normal"), forState:UIControlState.Normal)

//this will show when button is pressed
myButton.setBackgroundImage(UIImage(named:"background_image_highlighted"), forState:UIControlState.Highlighted)

关于ios - OnTouchUp 和 OnTouchDown 上的 Swift UIButton 背景更改?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28875055/

相关文章:

ios - Core Plot 建议 - 制作饼状条形图的最佳方式? (占总数的百分比)

php - iOS/PHP : communicating with database

ios - 局部变量的类属性闭包

ios - 以编程方式创建 UILabel 时出现的额外宽度和高度约束是什么?我怎样才能阻止他们的创造?

css - Susy 网格中部分的全尺寸背景

ios - Xcode Utilities rigth 面板在 Storyboard 中不再主宰信息

ios - 我的 m3u8 带宽值有问题

ios - 可拖动的 UITextView 没有响应

c++ - 位图作为 MDI mainfrm 的背景

css - 在 ul 列表中使用 sprite 图像?