If you like this site and you like coffee, why not buy me one. Because I love it

Click to buy coffe
avatar
Untitled

Guest 57 17th Nov, 2022

MARKUP 7.25 KB
                                           
                         import UIKit
import Foundation

final class ViewController: UIViewController {

    @IBOutlet var holder: UIView!
    
    private var firstNumber: Int = 0
    private var resultNumber: Int  = 0
    private var currentOperation: Operation?
    
    enum Operation {
        case add
        case subtract
        case multiply
        case divide
    }
    
    
    private var resultLabel: UILabel = {
        let label = UILabel()
        label.text = "0"
        label.textColor = .white
        label.textAlignment = .right
        label.font = UIFont(name: "Helvetica", size: 100)
        return label
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()
    }
    
    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()
        setupNumberPad()
    }
    
    private func setupNumberPad() {
        let buttonSize: CGFloat = view.frame.size.width / 4
        
        let zeroButton = UIButton(
            frame: CGRect(
                x: 0,
                y: holder.frame.size.height - buttonSize,
                width: buttonSize * 3,
                height: buttonSize
            ))
        
        zeroButton.backgroundColor = .white
        zeroButton.setTitleColor(.black, for: .normal)
        zeroButton.setTitle("0", for: .normal)
        
        holder.addSubview(zeroButton)
        
        zeroButton.addTarget(self, action: #selector(zerroTapped), for: .touchUpInside)
        
        let secondRowOfButtons: [UIButton] = addNewRowOfNumberButtons(firstNumberInRow: 1, numberOfRowFromBottom: 2)
        
        for button in secondRowOfButtons {
            holder.addSubview(button)
        }
        
        let thirdRowOfButtons: [UIButton] = addNewRowOfNumberButtons(firstNumberInRow: 4, numberOfRowFromBottom: 3)
        
        for button in thirdRowOfButtons {
            holder.addSubview(button)
        }
        
        let fourthRowOfButtons: [UIButton] = addNewRowOfNumberButtons(firstNumberInRow: 7, numberOfRowFromBottom: 4)
        
        for button in fourthRowOfButtons {
            holder.addSubview(button)
        }
        
        let clearButton = UIButton(
            frame: CGRect(
                x: 0,
                y: holder.frame.size.height - (buttonSize * 5),
                width: view.frame.size.width - buttonSize,
                height: buttonSize
            ))
        
        clearButton.backgroundColor = .white
        clearButton.setTitleColor(.black, for: .normal)
        clearButton.setTitle("Clear", for: .normal)
        
        holder.addSubview(clearButton)
        
        let operations = ["=", "+", "-", "x", "/"]
        
        for x in 0..<5 {
            let button4 = UIButton(
                frame: CGRect(
                    x: buttonSize * 3,
                    y: holder.frame.size.height - (buttonSize * CGFloat(x + 1)),
                    width: buttonSize,
                    height: buttonSize
                ))
            
            button4.backgroundColor = .orange
            button4.setTitleColor(.white, for: .normal)
            button4.setTitle(operations[x], for: .normal)
            
            holder.addSubview(button4)
            
            button4.tag = x + 1
            button4.addTarget(self, action: #selector(operationPressed(_:)), for: .touchUpInside)
        }
        
        resultLabel.frame = CGRect(
            x: 20,
            y: clearButton.frame.origin.y - 110.0,
            width: view.frame.size.width - 40,
            height: 100
        )
        
        holder.addSubview(resultLabel)
        
        clearButton.addTarget(self, action: #selector(clearResult), for: .touchUpInside)
    }
    
    private func addNewRowOfNumberButtons(firstNumberInRow: Int, numberOfRowFromBottom: CGFloat) -> [UIButton] {
        let buttonSize: CGFloat = view.frame.size.width / 4
        var buttons: [UIButton] = [UIButton]()
        
        for x in 0..<3 {
            let button = UIButton(
                frame: CGRect(
                    x: buttonSize * CGFloat(x),
                    y: holder.frame.size.height - (buttonSize * numberOfRowFromBottom),
                    width: buttonSize,
                    height: buttonSize
                ))
            
            button.backgroundColor = .white
            button.setTitleColor(.black, for: .normal)
            button.setTitle("\(x + firstNumberInRow)", for: .normal)
            button.tag = x + (firstNumberInRow + 1)
            button.addTarget(self, action: #selector(numberPressed(_:)), for: .touchUpInside)
            buttons.append(button)
        }
        
        return buttons
    }
    
    @objc private func clearResult() {
        resultLabel.text = "0"
        currentOperation = nil
        firstNumber = 0
    }
    
    @objc private func zerroTapped() {
        if resultLabel.text != "0" {
            if let text = resultLabel.text {
                resultLabel.text = "\(text)\(0)"
            }
        }
    }
    
    @objc private func numberPressed(_ sender: UIButton) {
        let tag = sender.tag - 1
        
        if resultLabel.text == "0" {
            resultLabel.text = "\(tag)"
        } else if let text = resultLabel.text {
            resultLabel.text = "\(text)\(tag)"
        }
    }
    
    @objc private func operationPressed(_ sender: UIButton) {
        let tag = sender.tag
        
        if let text = resultLabel.text, let value = Int(text), firstNumber == 0 {
            firstNumber = value
            resultLabel.text = "0"
        }
        
        if tag == 1 {
            if let operation = currentOperation {
                var secondNumber = 0
                
                if let text = resultLabel.text, let value = Int(text) {
                    secondNumber = value
                }
                
                switch operation {
                    case .add:
                        let result = firstNumber + secondNumber
                        resultLabel.text = "\(result)"
                    case .subtract:
                        let result = firstNumber - secondNumber
                        resultLabel.text = "\(result)"
                    case .multiply:
                        let result = firstNumber * secondNumber
                        resultLabel.text = "\(result)"
                    case .divide:
                        let result = firstNumber / secondNumber
                        resultLabel.text = "\(result)"
                }
            }
        } else if tag == 2 {
            currentOperation = .add
        } else if tag == 3 {
            currentOperation = .subtract
        } else if tag == 4 {
            currentOperation = .multiply
        } else if tag == 5 {
            currentOperation = .divide
        }
    }
}
                      
                                       
To share this paste please copy this url and send to your friends
RAW Paste Data
Recent Pastes
Ta strona używa plików cookie w celu usprawnienia i ułatwienia dostępu do serwisu oraz prowadzenia danych statystycznych. Dalsze korzystanie z tej witryny oznacza akceptację tego stanu rzeczy.
Wykorzystywanie plików Cookie
Jak wyłączyć cookies?
ROZUMIEM