Skip to content

Commit

Permalink
✨ Feat: #17 - KeyboardLayout 추가 (TextField 모션 부드럽게)
Browse files Browse the repository at this point in the history
  • Loading branch information
usa4060 committed Nov 15, 2023
1 parent 8ef991a commit 7be72cd
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 39 deletions.
38 changes: 20 additions & 18 deletions CMC/Sources/Presenter/Auth/SignIn/SignInViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ final class SignInViewController: BaseViewController {
}

signInButton.snp.makeConstraints { make in
make.bottom.equalTo(view.safeAreaLayoutGuide.snp.bottom).offset(-20)
make.bottom.equalTo(view.keyboardLayoutGuide.snp.top).offset(-20)
make.leading.equalToSuperview().offset(24)
make.trailing.equalToSuperview().offset(-24)
make.height.equalTo(56)
Expand All @@ -208,19 +208,29 @@ final class SignInViewController: BaseViewController {

override func bind() {

NotificationManager.shared.keyboardHeightSubject
// NotificationManager.shared.keyboardHeightSubject
// .withUnretained(self)
// .subscribe(onNext: { owner, keyboardHeight in
// let realHeight = keyboardHeight > 0 ? keyboardHeight - 30 : 0
// owner.signInButton.snp.updateConstraints { make in
// make.bottom.equalTo(owner.view.safeAreaLayoutGuide.snp.bottom).offset(-20 - realHeight)
// }
// UIView.animate(withDuration: 0.3) {
// owner.view.layoutIfNeeded()
// }
// })
// .disposed(by: disposeBag)

self.view.rx.tapGesture()
.debug()
.when(.recognized)
.withUnretained(self)
.subscribe(onNext: { owner, keyboardHeight in
let realHeight = keyboardHeight > 0 ? keyboardHeight - 30 : 0
owner.signInButton.snp.updateConstraints { make in
make.bottom.equalTo(owner.view.safeAreaLayoutGuide.snp.bottom).offset(-20 - realHeight)
}
UIView.animate(withDuration: 0.3) {
owner.view.layoutIfNeeded()
}
.subscribe(onNext: { owner, _ in
owner.view.endEditing(true)
})
.disposed(by: disposeBag)


passwordTextField.accessoryState
.observe(on: MainScheduler.instance)
.withUnretained(self)
Expand Down Expand Up @@ -253,11 +263,3 @@ final class SignInViewController: BaseViewController {
}

}


// MARK: - Extension
extension SignInViewController {
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
self.view.endEditing(true)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,6 @@ final class MainSignUpView: BaseView {
mainContentView.snp.makeConstraints { make in
make.edges.equalTo(scrollView.contentLayoutGuide)
make.width.equalTo(scrollView.frameLayoutGuide.snp.width)
// make.height.greaterThanOrEqualTo(500)
}

emailTextField.snp.makeConstraints{ make in
Expand Down Expand Up @@ -242,6 +241,16 @@ final class MainSignUpView: BaseView {
})
.disposed(by: disposeBag)

self.rx.tapGesture()
.when(.recognized)
.withUnretained(self)
.subscribe(onNext: { owner, gesture in
let location = gesture.location(in: owner.scrollView)
if !owner.isPointInsideTextField(location) {
owner.endEditing(true)
}
})
.disposed(by: disposeBag)

let input = MainSignUpViewModel.Input(
email: emailTextField.rx.text.orEmpty.asObservable(),
Expand Down Expand Up @@ -302,3 +311,13 @@ final class MainSignUpView: BaseView {
}

}


// MARK: - Extension
extension MainSignUpView {
fileprivate func isPointInsideTextField(_ point: CGPoint) -> Bool {
// 모든 텍스트 필드를 순회하면서 탭된 위치가 텍스트 필드 내부인지 확인합니다.
let textFields = [emailTextField, passwordTextField, confirmPasswordTextField, nameTextField]
return textFields.contains(where: { $0.frame.contains(point) })
}
}
40 changes: 20 additions & 20 deletions CMC/Sources/Presenter/Auth/SignUp/SignUpViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ class SignUpViewController: BaseViewController {

nextButton.snp.makeConstraints{ nextButton in
nextButton.leading.trailing.equalToSuperview().inset(20)
nextButton.bottom.equalTo(self.view.safeAreaLayoutGuide).offset(-20)
nextButton.bottom.equalTo(self.view.keyboardLayoutGuide.snp.top).offset(-20)
nextButton.height.equalTo(56)
}

Expand All @@ -127,18 +127,18 @@ class SignUpViewController: BaseViewController {

override func bind() {

NotificationManager.shared.keyboardHeightSubject
.withUnretained(self)
.subscribe(onNext: { owner, keyboardHeight in
let realHeight = keyboardHeight > 0 ? keyboardHeight : 20
owner.nextButton.snp.updateConstraints { make in
make.bottom.equalTo(self.view.safeAreaLayoutGuide).offset(-realHeight)
}
UIView.animate(withDuration: 0.3) {
owner.view.layoutIfNeeded()
}
})
.disposed(by: disposeBag)
// NotificationManager.shared.keyboardHeightSubject
// .withUnretained(self)
// .subscribe(onNext: { owner, keyboardHeight in
// let realHeight = keyboardHeight > 0 ? keyboardHeight : 20
// owner.nextButton.snp.updateConstraints { make in
// make.bottom.equalTo(self.view.safeAreaLayoutGuide).offset(-realHeight)
// }
// UIView.animate(withDuration: 0.3) {
// owner.view.layoutIfNeeded()
// }
// })
// .disposed(by: disposeBag)

nextButton.rx.tap
.withLatestFrom(cmcPager.getCurrentPage())
Expand All @@ -151,13 +151,13 @@ class SignUpViewController: BaseViewController {
})
.disposed(by: disposeBag)

self.view.rx.tapGesture()
.when(.recognized)
.withUnretained(self)
.subscribe(onNext: { owner, _ in
owner.view.endEditing(true)
})
.disposed(by: disposeBag)
// self.view.rx.tapGesture()
// .when(.recognized)
// .withUnretained(self)
// .subscribe(onNext: { owner, _ in
// owner.view.endEditing(true)
// })
// .disposed(by: disposeBag)

let input = SignUpViewModel.Input(
backButtonTapped: navigationBar.backButton.rx.tapped().asObservable(),
Expand Down

0 comments on commit 7be72cd

Please sign in to comment.