Vlad Karlugin
Vlad Karlugin

Reputation: 71

Change a button background on click of another button

I use this code so that when I click on a button its background changes to a picture with a white border and when I press it again it changes to a picture with a gray background (the button always has a gray background).

How can I do so that when I click on another button (clear and equal), the background of the "+", "-", "/", "*" changes to gray as it was before pressing.

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
    }

}

Upvotes: 2

Views: 112

Answers (1)

Devang Tandel
Devang Tandel

Reputation: 3008

Here is the code you can Create border to any View, in your case it will be button.

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
}

You can use the above function with you code like

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

let know if it helps, or you need any explanation.

If you want to set with image

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

Upvotes: 2

Related Questions