NotificationCenter 키보드 보이기,숨기기 / 백그라운드,포그라운드 / 사용자 Notification
/////////////////////////////////////////////////
// NotificationCenter 키보드 Noti 등록
// 키보드 보이기
NotificationCenter.default.addObserver( self, selector: #selector(keyboardWillShow(noti: )) , name: UIResponder.keyboardWillShowNotification, object: nil)
// 키보드 숨기기
NotificationCenter.default.addObserver( self, selector: #selector(keyboardWillHide(noti: )) , name: UIResponder.keyboardWillHideNotification, object: nil)
// NotificationCenter 삭제
NotificationCenter.default.removeObserver(self, name: UIResponder.keyboardWillShowNotification , object: nil)
NotificationCenter.default.removeObserver(self, name: UIResponder.keyboardWillHideNotification , object: nil)
@objc func keyboardWillShow(noti: NSNotification) {
//키보드의 높이를 가져오는 코드
let notiInfo = noti.userInfo! as NSDictionary
let keyboardFrame = notiInfo[UIResponder.keyboardFrameEndUserInfoKey] as! CGRect
let height = keyboardFrame.size.height
self.bottomConstraint.constant = height
//애니메이션 추가 (View가 먼저 올라가서 키보드 올라오는 공백이 순간적으로 보이는 문제로 추가)
let animationDuration = notiInfo[UIResponder.keyboardAnimationDurationUserInfoKey] as! TimeInterval
UIView.animate(withDuration: animationDuration, animations: { [weak self] in
self?.view.layoutIfNeeded()
}) { (Bool) in
}
}
@objc func keyboardWillHide(noti: NSNotification) {
let notiInfo = noti.userInfo! as NSDictionary
self.bottomConstraint.constant = 0
let animationDuration = notiInfo[UIResponder.keyboardAnimationDurationUserInfoKey] as! TimeInterval
UIView.animate(withDuration: animationDuration, animations: { [weak self] in
self?.view.layoutIfNeeded()
}) { (Bool) in
}
}
/////////////////////////////////////////////////
// NotificationCenter 백그라운드 및 포그라운드 Noti 등록
// 포그라운드
NotificationCenter.default.addObserver(self, selector: #selector(onDidBecomeActive(not:)), name: UIApplication.didBecomeActiveNotification, object: nil)
// 백그라운드
NotificationCenter.default.addObserver(self, selector: #selector(onDidEnterBackgroundActive(not:)), name: UIApplication.didEnterBackgroundNotification, object: nil)
// NotificationCenter 삭제
NotificationCenter.default.removeObserver(self, name: UIApplication.didBecomeActiveNotification, object: nil)
NotificationCenter.default.removeObserver(self, name: UIApplication.didEnterBackgroundNotification, object: nil)
@objc func onDidBecomeActive(not: Notification) {
...... 이하 생략 ......
}
사용자 NotificationCenter
//////////////////////////////////////////////////
// NotificationCenter User Noti 등록
NotificationCenter.default.addObserver(self, selector: #selector(UserCustomReceived(noti:)), name: NSNotification.Name(rawValue: "UserCustomNotification"), object: nil)
// NotificationCenter 삭제
NotificationCenter.default.removeObserver(self, name: NSNotification.Name(rawValue: "UserCustomNotification"), object: nil)
// NotificationCenter User 데이터 전송
NotificationCenter.default.post(name: NSNotification.Name(rawValue: "UserCustomNotification"), object: (self?.view.frame.height, height), userInfo: nil)
@objc func UserCustomReceived(noti: Notification){
let height:(viewHeight: CGFloat, playerHeight: CGFloat) = noti.object as! (CGFloat, CGFloat)
}
'Swift > 기타' 카테고리의 다른 글
[SWIFT]스크린 캡쳐 및 녹화 감지 (0) | 2023.04.26 |
---|---|
[SWIFT]Contraints 코드로 활성화 비활성화 제어하기 (0) | 2023.04.24 |
[SWIFT] GPS 현재 위치 정보(위도,경도) 및 상세주소 가져오기 (0) | 2023.04.24 |
[SWIFT]갤러리 및 카메라 사용 UIImagePickerController(디폴트 UI) (0) | 2023.04.20 |
[SWIFT]네트워크 상태체크 (0) | 2023.04.18 |