From 6a4be0f08a1db660c02708a605debd0c8974bb4f Mon Sep 17 00:00:00 2001 From: beomsoo0 <73675540+beomsoo0@users.noreply.github.com> Date: Fri, 4 Nov 2022 18:22:45 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20=ED=82=A4=EB=B3=B4=EB=93=9C=20=EC=88=AB?= =?UTF-8?q?=EC=9E=90=20=EB=9E=9C=EB=8D=A4=20=EC=9C=84=EC=B9=98=20UI=20?= =?UTF-8?q?=EA=B5=AC=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ViewController.swift | 1 - README.md | 2 +- .../Classes/RandomNumberKeyboard.swift | 178 +++++++-------- .../Classes/RandomNumberKeyboard.xib | 215 ++++++++++-------- .../Classes/RandomNumberTextField.swift | 42 ++-- 5 files changed, 239 insertions(+), 199 deletions(-) diff --git a/Example/RandomNumberTextField/ViewController.swift b/Example/RandomNumberTextField/ViewController.swift index 9fb1c17..e1246f1 100644 --- a/Example/RandomNumberTextField/ViewController.swift +++ b/Example/RandomNumberTextField/ViewController.swift @@ -27,7 +27,6 @@ class ViewController: UIViewController { cbKeyboard.widthAnchor.constraint(equalToConstant: 120.0).isActive = true cbKeyboard.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true cbKeyboard.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = true - cbKeyboard.setKeyboardBackgroundColor(.red) } } diff --git a/README.md b/README.md index 7e45f76..b4dde48 100644 --- a/README.md +++ b/README.md @@ -7,7 +7,7 @@ ## 📸 Preview -
+
## 🏗 Installation * [CocoaPods](https://guides.cocoapods.org/using/using-cocoapods.html): diff --git a/RandomNumberTextField/Classes/RandomNumberKeyboard.swift b/RandomNumberTextField/Classes/RandomNumberKeyboard.swift index 346df58..7c8656f 100644 --- a/RandomNumberTextField/Classes/RandomNumberKeyboard.swift +++ b/RandomNumberTextField/Classes/RandomNumberKeyboard.swift @@ -8,127 +8,125 @@ import UIKit protocol RandomNumberKeyboardDelegate { - func outputData(_ num: Int?) + func outputData(_ input: KeyboardIput) +} + +enum KeyboardIput { + enum Order { + case delete, clear, complete + } + + case number(Int) + case order(Order) } class RandomNumberKeyboard: UIView { - @IBOutlet var oneButton: UIButton! - @IBOutlet var twoButton: UIButton! - @IBOutlet var threeButton: UIButton! - @IBOutlet var fourButton: UIButton! - @IBOutlet var fiveButton: UIButton! - @IBOutlet var sixButton: UIButton! - @IBOutlet var sevenButton: UIButton! - @IBOutlet var eightButton: UIButton! - @IBOutlet var nineButton: UIButton! - @IBOutlet var zeroButton: UIButton! - @IBOutlet var deleteButton: UIButton! + @IBOutlet var numberButtons: [UIButton]! + @IBOutlet weak var deleteButton: UIButton! + @IBOutlet weak var clearButton: UIButton! + @IBOutlet weak var completeButton: UIButton! + private var allButtons: [UIButton] { + return numberButtons + [deleteButton, clearButton, completeButton] + } static let ideytifier = "RandomNumberKeyboard" - private var numbers = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] + private var numbers: [Int?] = [] var delegate: RandomNumberKeyboardDelegate? - + internal func configure() { - setBackgroundColor(.black) - setFont(UIFont.systemFont(ofSize: 24.0, weight: .bold)) - setTitleColor(.white, for: .normal) - shuffleNumbers() + addTargetNumberButtons() + setButtonsCornerRadius(4.0) + shuffleNumberButtons() + } + + internal func shuffleNumberButtons() { + numbers = makeRandomNumbers() + configureNumberButtons() + } +} + +// MARK: Configure Methods + +private extension RandomNumberKeyboard { + // generate random numbers + func makeRandomNumbers() -> [Int?] { + var numbers: [Int?] = [] + (0.. index, + let number = numbers[index] else { + return + } + delegate?.outputData(KeyboardIput.number(number)) } - @IBAction func nineButtonTapped(_ sender: Any) { - delegate?.outputData(numbers[9]) + + @IBAction func deleteButtonTapped(_ sender: Any) { + delegate?.outputData(KeyboardIput.order(.delete)) } - @IBAction func zeroButtonTapped(_ sender: Any) { - delegate?.outputData(numbers[0]) + + @IBAction func clearButtonTapped(_ sender: Any) { + delegate?.outputData(KeyboardIput.order(.clear)) } - @IBAction func deleteButtonTapped(_ sender: Any) { - delegate?.outputData(nil) + @IBAction func completeButtonTapped(_ sender: Any) { + delegate?.outputData(KeyboardIput.order(.complete)) } } diff --git a/RandomNumberTextField/Classes/RandomNumberKeyboard.xib b/RandomNumberTextField/Classes/RandomNumberKeyboard.xib index 5447b08..6a6c439 100644 --- a/RandomNumberTextField/Classes/RandomNumberKeyboard.xib +++ b/RandomNumberTextField/Classes/RandomNumberKeyboard.xib @@ -6,6 +6,7 @@ + @@ -17,175 +18,193 @@ - + - - + + - - + + + + - - + + + + - - + + + + - - + + - - - - + @@ -197,32 +216,48 @@ - - + + - - - - + + + + + + - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/RandomNumberTextField/Classes/RandomNumberTextField.swift b/RandomNumberTextField/Classes/RandomNumberTextField.swift index 15bb28f..9dff828 100644 --- a/RandomNumberTextField/Classes/RandomNumberTextField.swift +++ b/RandomNumberTextField/Classes/RandomNumberTextField.swift @@ -20,8 +20,8 @@ public class RandomNumberTextField: UITextField { configureKeyboardView() } - public override func layoutSubviews() { - keyboardView?.shuffleNumbers() + override public func layoutSubviews() { + configureKeyboardView() } private func configureKeyboardView() { @@ -32,7 +32,7 @@ public class RandomNumberTextField: UITextField { inputView = keyboardView keyboardView.delegate = self } - + private func loadViewFromNib(nib: String) -> UIView? { let bundle = Bundle(for: type(of: self)) let nib = UINib(nibName: nib, bundle: bundle) @@ -54,24 +54,32 @@ public extension RandomNumberTextField { } func shuffleKeyboardNumbers() { - keyboardView?.shuffleNumbers() + // keyboardView?.shuffleNumbers() } } extension RandomNumberTextField: RandomNumberKeyboardDelegate { - func outputData(_ num: Int?) { - var newText = "" - - if let num = num { - newText = (text ?? "") + num.toString - } else { - if let curText = text, - curText != "" { - newText = curText - newText.removeLast() - } + func outputData(_ input: KeyboardIput) { + switch input { + case .number(let number): + numberHandler(number) + case .order(let order): + orderHandler(order) + } + } + + private func numberHandler(_ number: Int) { + text = (text ?? "") + number.toString + } + + private func orderHandler(_ order: KeyboardIput.Order) { + switch order { + case .delete: + text?.popLast() + case .clear: + text?.removeAll() + case .complete: + resignFirstResponder() } - - self.text = newText } }