繁体   English   中英

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

[英]Change a button background on click of another button

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

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

按键

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
    }

}

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

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
}
}

暂无
暂无

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

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