Skip to content

Commit

Permalink
[FEAT][#7] AttandanceDetailViewController 출석버튼 추가, 키보드에 따라 버튼 이동 로직 구현
Browse files Browse the repository at this point in the history
  • Loading branch information
tngusmiso committed Feb 11, 2023
1 parent 722afe0 commit 23bab8f
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 0 deletions.
21 changes: 21 additions & 0 deletions Extensions/UIView+Extension.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,25 @@ extension UIView {
func addSubviews(_ views: UIView...) {
views.forEach { addSubview($0) }
}

/// 키보드 움직임에 따라서 움직이도록 함.
func moveWithKeyboard(willShow keyboardWillShow: Bool, notification: NSNotification, safeAreaBottomInset: CGFloat = 0) {
guard let keyboardSize = (notification.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue,
let keyboardDuration = notification.userInfo?[UIResponder.keyboardAnimationDurationUserInfoKey] as? Double,
let keyboardcurveValue = notification.userInfo?[UIResponder.keyboardAnimationCurveUserInfoKey] as? UInt
else { return }

let keyboardHeight = keyboardSize.height
let curveOption = UIView.AnimationOptions(rawValue: keyboardcurveValue << 16)

if keyboardWillShow {
UIView.animate(withDuration: keyboardDuration, delay: 0, options: [curveOption]) { [weak self] in
self?.transform = CGAffineTransform(translationX: 0, y: -keyboardHeight + safeAreaBottomInset)
}
} else {
UIView.animate(withDuration: keyboardDuration, delay: 0, options: [curveOption]) { [weak self] in
self?.transform = .identity
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,13 @@ class AttendanceDetailViewController: UIViewController {
private let codeContainerView: UIStackView = UIStackView()
private let codeTextFields: [UITextField] = [UITextField(), UITextField(), UITextField(), UITextField()]
private let descriptionLabel: UILabel = UILabel()
private let attendButton: UIButton = UIButton()

// MARK: - Lifecycle
override func viewDidLoad() {
super.viewDidLoad()

self.setupKeyboardNotifications()
self.setupViews()
self.setupLayout()
}
Expand All @@ -29,7 +31,25 @@ class AttendanceDetailViewController: UIViewController {
self.setupNavigation()
}

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
self.view.endEditing(true)
}

// MARK: - Setup
private func setupKeyboardNotifications() {
NotificationCenter.default.addObserver(
self,
selector: #selector(self.keyboardWillShow),
name: UIResponder.keyboardWillShowNotification,
object: nil)

NotificationCenter.default.addObserver(
self,
selector: #selector(self.keyboardWillHide),
name: UIResponder.keyboardWillHideNotification,
object: nil)
}

private func setupNavigation() {

}
Expand Down Expand Up @@ -62,6 +82,9 @@ class AttendanceDetailViewController: UIViewController {
self.descriptionLabel.font = .systemFont(ofSize: 12)
self.descriptionLabel.textColor = .white.withAlphaComponent(0.67)
self.backgroundView.addSubview(self.descriptionLabel)

self.attendButton.setTitle("출석하기", size: 16, weight: .bold, color: .white)
self.attendButton.configurate(bgColor: .rgba(56, 56, 56, 1), cornerRadius: 7, padding: 10)
}

// MARK: - Layout
Expand Down Expand Up @@ -108,5 +131,37 @@ class AttendanceDetailViewController: UIViewController {
make.centerX.equalToSuperview()
make.bottom.equalToSuperview().inset(42)
}

self.view.addSubview(self.attendButton)
self.attendButton.snp.makeConstraints { make in
make.leading.trailing.equalTo(self.view.safeAreaLayoutGuide).inset(24)
make.bottom.equalTo(self.view.safeAreaLayoutGuide).inset(28)
make.height.equalTo(54)
}
}

// MARK: - Actions
@objc func keyboardWillShow(_ notification: NSNotification) {
if self.isEditingCode {
self.attendButton.moveWithKeyboard(
willShow: true,
notification: notification,
safeAreaBottomInset: self.view.safeAreaInsets.bottom
)
}
}

@objc func keyboardWillHide(_ notification: NSNotification) {
self.attendButton.moveWithKeyboard(
willShow: false,
notification: notification,
safeAreaBottomInset: self.view.safeAreaInsets.bottom
)
}

// MARK: - Logics
private var isEditingCode: Bool {
let textFileds = self.codeTextFields
return textFileds[0].isEditing || textFileds[1].isEditing || textFileds[2].isEditing || textFileds[3].isEditing
}
}

0 comments on commit 23bab8f

Please sign in to comment.