单击另一个按钮时更改按钮背景

Posted

技术标签:

【中文标题】单击另一个按钮时更改按钮背景【英文标题】:Change a button background on click of another button 【发布时间】:2017-08-05 08:12:08 【问题描述】:

我使用此代码,以便当我单击一个按钮时,它的背景会变为带有白色边框的图片,而当我再次按下它时,它会变为具有灰色背景的图片(按钮始终具有灰色背景)。

我怎样才能做到这一点,当我点击另一个按钮(清除和相等)时,“+”、“-”、“/”、“*”的背景会变为按下之前的灰色。

import UIKit

class ViewController: UIViewController 

    @IBOutlet weak var displayResultLabel: UILabel!
    var stillTyping = false
    var dotIsPlaced = false
    var firstOperand: Double = 0
    var secondOperand: Double = 0
    var operationSign: String = ""


    var currentInput: Double 
        get 
            return Double (displayResultLabel.text!)!
        
        set 
            let value = "\(newValue)"
            let ValueArray = (value.components(separatedBy:"."))
            if ValueArray[1] == "0" 
                displayResultLabel.text = "\(ValueArray[0])"
             else 
                displayResultLabel.text = "\(newValue)"
            
            stillTyping = false
        
    

    @IBAction func numberPressed(_ sender: UIButton) 
        let number = sender.currentTitle!

        if stillTyping 
            if (displayResultLabel.text?.characters.count)! < 20 
                displayResultLabel.text = displayResultLabel.text! + number
            
         else 
            displayResultLabel.text = number
            stillTyping = true
        
    

    @IBAction func twoOperandsSignPressed(sender: UIButton) 
        operationSign = sender.currentTitle!
        firstOperand = currentInput
        stillTyping = false
        dotIsPlaced = false
    

    func operateWithTwoOperands(operation: (Double, Double) -> Double) 
        currentInput = operation(firstOperand, secondOperand)
        stillTyping = false
    

    @IBAction func equalitySignPressed(sender: UIButton) 

        if stillTyping 
            secondOperand = currentInput
        

        dotIsPlaced = false

        switch operationSign 

        case "+":
            operateWithTwoOperands$0 + $1

        case "-":
            operateWithTwoOperands$0 - $1

        case "✕":
            operateWithTwoOperands$0 * $1

        case "÷":
            operateWithTwoOperands$0 / $1
        default: break

        
    

    @IBAction func clearButtonPressed(_ sender: UIButton) 
        firstOperand = 0
        secondOperand = 0
        currentInput = 0
        displayResultLabel.text = "0"
        dotIsPlaced = false
        operationSign = ""

    

    // +,-
    @IBAction func plusMinusButtonPressed(_ sender: UIButton) 
        currentInput = -currentInput
    

    @IBAction func percentageButtonPressed(_ sender: UIButton) 
        if firstOperand == 0 
            currentInput = currentInput / 100
         else 
            secondOperand = firstOperand * currentInput / 100
        
    

    @IBAction func squareRootButtonPressed(_ sender: UIButton) 
        currentInput = sqrt(currentInput)
    

    @IBAction func dotButtonPressed(_ sender: UIButton) 
        if stillTyping && !dotIsPlaced 
            displayResultLabel.text = displayResultLabel.text! + "."
            dotIsPlaced = true
         else if !stillTyping && !dotIsPlaced 
            displayResultLabel.text = "0."
        

    @IBAction func PercentAnimate(_ sender: UIButton) 

    if sender.currentBackgroundImage == image_off     
        sender.setBackgroundImage(Image_on, for: .normal)         
     else 
        sender.setBackgroundImage(image_off, for: .normal)
     
    if (previousButton !== sender) 
        previousButton.setBackgroundImage(image_off, for: .normal)
        previousButton = sender
    

    override var preferredStatusBarStyle: UIStatusBarStyle 
        return .lightContent
    


【问题讨论】:

你为什么不简单地使用边框颜色? 【参考方案1】:

这是您可以为任何视图创建边框的代码,在您的情况下它将是按钮。

func createBordersWithColor(color: UIColor, myView : UIView) 

    myView.layer.borderWidth = 1
    myView.layer.cornerRadius = 0
    myView.layer.shouldRasterize = false
    myView.layer.rasterizationScale = 2
    myView.clipsToBounds = true
    myView.layer.masksToBounds = true
    let cgColor: CGColor = color.cgColor
    myView.layer.borderColor = cgColor

你可以在你的代码中使用上面的函数

@IBAction func PercentAnimate(_ sender: UIButton) 
    let btnCurrent : UIButton = sender as! UIButton
    let btnPrevious : UIButton = previousButton as! UIButton
    createBorderWithColor(color : UIColor.clear , myView : btnPrevious)
    createBorderWithColor(color : UIColor.clear , myView : btnCurrent)
    if (previousButton !== sender) 
        previousButton = sender
    

如果有帮助,请告知,或者您需要任何解释。

如果要设置图片

@IBAction func PercentAnimate(_ sender: UIButton) 
let btnCurrent : UIButton = sender as! UIButton
let btnPrevious : UIButton = previousButton as! UIButton
btnPrevious.setBackgroundImage(image_off, for: .normal)
btnCurrent.setBackgroundImage(image_on, for: .normal)
if (previousButton !== sender) 

    previousButton = sender


【讨论】:

@ Dev_Tandel Где должна находиться «func createBordersWithColor...»? 在你的课堂上,你可以把@IBAction func PercentAnimate(_ sender: UIButton) 完成了吗? @VladKarlugin 不喜欢它的高度、宽度 对不起,我没有得到你。

以上是关于单击另一个按钮时更改按钮背景的主要内容,如果未能解决你的问题,请参考以下文章

ios在按钮单击时更改背景

UIButton 背景图像从另一个按钮更改

For循环多个按钮和字符串:如果单击另一个按钮,如何更改按钮文本和背景并恢复为默认设置?

如何在反应js中更改按钮onClick的背景颜色?

单击按钮时更改布局背景颜色

JTable:单击按钮时更改单元格背景