点击 select uibutton 然后点击 deselect uibutton
tap to select uibutton then tap to deselect uibutton
您好,我正在尝试实施以下
点击 uibutton 到 select 并突出显示背景,然后再次点击同一按钮将 select 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
}
使用以下代码,当我点击一次 selects 并突出显示 UIButton 按钮时,当我再次点击它时,它会改变颜色按钮不会 deselect,以便我 deselect 我必须点击、按住并拖离按钮才能改变颜色或 return 到原始状态
请帮忙,这让我很生气,看起来很简单的事情似乎很难
提前致谢
唯一合法的手势是 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
}
对于 1 个按钮
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
}
}
解决方案 1
设置按钮的 both/all 状态的颜色、文本、文本颜色来自代码或来自 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
访问该特定按钮。您正在为所有按钮执行相同的任务,那么为什么不按照说明重用代码。
祝一切顺利!
您好,我正在尝试实施以下
点击 uibutton 到 select 并突出显示背景,然后再次点击同一按钮将 select 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
}
使用以下代码,当我点击一次 selects 并突出显示 UIButton 按钮时,当我再次点击它时,它会改变颜色按钮不会 deselect,以便我 deselect 我必须点击、按住并拖离按钮才能改变颜色或 return 到原始状态
请帮忙,这让我很生气,看起来很简单的事情似乎很难
提前致谢
唯一合法的手势是 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
}
对于 1 个按钮
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
}
}
解决方案 1
设置按钮的 both/all 状态的颜色、文本、文本颜色来自代码或来自 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
访问该特定按钮。您正在为所有按钮执行相同的任务,那么为什么不按照说明重用代码。
祝一切顺利!