嗨,我正在尝试实现以下内容
点击 uibutton 以选择并突出显示背景,然后再次点击同一按钮以取消选择 uibutton 背景为原始状态或其他颜色
我的代码如下:
@IBOutlet weak var case4Btn: UIButton!
@IBAction func case4BtnClicked(sender: AnyObject) { //touch up inside
case4Btn.backgroundColor = UIColor.cyanColor()
}
@IBAction func case4BtnCancel(sender: AnyObject) {
case4Btn.backgroundColor = UIColor.lightGrayColor()//touch down
}
使用以下代码,当我点击一次时,它会选择并突出显示 UIButton 按钮,当我再次点击时,它会更改颜色按钮不会取消选择,为了让我取消选择,我必须点击、按住并拖离按钮才能更改它颜色或恢复原状
请帮忙,因为这让我发疯,看起来很简单的事情似乎很难
提前谢谢您
解决方案1
从代码或
InterfaceBuilder
设置按钮的两个/所有状态的颜色,文本,文本颜色,如下
button.setBackgroundImage(UIImage(named: "cyanColorImage"), forState: .Normal)
button.setBackgroundImage(UIImage(named: "brownColorImage"), forState: .Selected)
并处理目标并仅更改按钮的状态
@IBAction func buttonClickedHandle(sender: UIButton)
{
sender.selected = !sender.selected
}
解决方案2
无需额外变量即可完成此操作。为了实现这个目标
selected
属性来实现您的要求。您不需要为您需要的所有按钮编写单独的方法。只需为所有人编写一个方法即可。
@IBAction func buttonClickedHandle(sender: UIButton)
{
if sender.selected
{
sender.backgroundColor = UIColor.cyanColor()
}
else
{
sender.backgroundColor = UIColor.lightGrayColor()
}
sender.selected = !sender.selected
}
将所有按钮的目标添加到
buttonClickedHandle
并以 sender
的形式访问该特定按钮。您正在为所有按钮执行相同的任务,那么为什么不重复使用所解释的代码。
祝一切顺利!
var buttonState = "cyan"
//the color the button should be when pressed
@IBOutlet weak var case4Btn: UIButton!
//the button
@IBAction func case4BtnClicked(sender: AnyObject) {
//touch up inside
if(buttonState == "cyan"){
//if the button is supposed to be cyan
case4Btn.backgroundColor = UIColor.cyanColor()
//set the background color
buttonState = "gray"
//set it to be gray next time
}
else{
//if it isn't
case4Btn.backgroundColor = UIColor.grayColor()
//set the background color
buttonState = "cyan"
//make it become cyan next time
}
}
var button1State = "cyan"
var button2State = "cyan"
var button3State = "cyan"
//the color the buttons should be when pressed
@IBOutlet weak var case4Btn1: UIButton!
@IBOutlet weak var case4Btn2: UIButton!
@IBOutlet weak var case4Btn3: UIButton!
//the buttons
@IBAction func case4Btn1Clicked(sender: AnyObject) {
//touch up inside
if(button1State == "cyan"){
//if the button is supposed to be cyan
case4Btn1.backgroundColor = UIColor.cyanColor()
//set the background color
button1State = "gray"
//set it to be gray next time
}
else{
//if it isn't
case4Btn1.backgroundColor = UIColor.grayColor()
//set the background color
button1State = "cyan"
//make it become cyan next time
}
}
@IBAction func case4Btn2Clicked(sender: AnyObject) {
//touch up inside
if(button2State == "cyan"){
//if the button is supposed to be cyan
case4Btn2.backgroundColor = UIColor.cyanColor()
//set the background color
button2State = "gray"
//set it to be gray next time
}
else{
//if it isn't
case4Btn2.backgroundColor = UIColor.grayColor()
//set the background color
button2State = "cyan"
//make it become cyan next time
}
}
@IBAction func case4Btn3Clicked(sender: AnyObject) {
//touch up inside
if(button3State == "cyan"){
//if the button is supposed to be cyan
case4Btn3.backgroundColor = UIColor.cyanColor()
//set the background color
button3State = "gray"
//set it to be gray next time
}
else{
//if it isn't
case4Btn3.backgroundColor = UIColor.grayColor()
//set the background color
button3State = "cyan"
//make it become cyan next time
}
}
唯一合法的手势是 Touch Up Inside。完全删除您的其他操作。使用 Bool 属性来跟踪按钮所处的状态。点击按钮时,使用
if
语句根据 Bool 属性更改按钮的背景颜色 — 并更改 Bool 属性以匹配新状态。
@IBOutlet weak var case4Btn: UIButton!
var gray = true
@IBAction func case4BtnClicked(sender: AnyObject) { //touch up inside
if gray {
case4Btn.backgroundColor = UIColor.cyanColor()
} else {
case4Btn.backgroundColor = UIColor.lightGrayColor()
}
gray = !gray
}
@IBAction func TipChanged(_ 发送者: UIButton) {
tenPctButton.isSelected = false
twentyPctButton.isSelected = false
zeroPctButton.isSelected = false
sender.isSelected = true
}