Skip to content

Instantly share code, notes, and snippets.

@nekonora
Last active July 14, 2022 04:46
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nekonora/21fd87b1d4192b5d102200199206baee to your computer and use it in GitHub Desktop.
Save nekonora/21fd87b1d4192b5d102200199206baee to your computer and use it in GitHub Desktop.
Sync animation to keyboard appearing
@objc func keyboardWillShow(_ notification: NSNotification) {
let keyboardAnimationDetail = notification.userInfo
let animationCurve: Int = {
if let keyboardAnimationCurve = keyboardAnimationDetail?[UIResponder.keyboardAnimationCurveUserInfoKey] as? Int {
let curve: Int? = UIView.AnimationCurve(rawValue: keyboardAnimationCurve)?.rawValue
return curve ?? 0
} else {
return 0
}
}()
let duration: Double = {
if let animationDuration = keyboardAnimationDetail?[UIResponder.keyboardAnimationDurationUserInfoKey] as? Int {
return Double(animationDuration)
} else {
return 0
}
}()
let options = UIView.AnimationOptions(rawValue: ((UInt(animationCurve << 16))))
let translate = CGAffineTransform(translationX: 0, y: -80)
UIView.animate(withDuration: duration, delay: 0, options: options, animations: {
self.closeButtonUI.alpha = 0
self.buttonsStackView.isHidden = true
self.wrapperView.transform = translate
self.buttonsStackView.layoutIfNeeded()
}, completion: { _ in
})
}
@objc func keyboardWillHide(_ notification: NSNotification) {
let keyboardAnimationDetail = notification.userInfo
let animationCurve: Int = {
if let keyboardAnimationCurve = keyboardAnimationDetail?[UIResponder.keyboardAnimationCurveUserInfoKey] as? Int {
let curve: Int? = UIView.AnimationCurve(rawValue: keyboardAnimationCurve)?.rawValue
return curve ?? 0
} else {
return 0
}
}()
let duration: Double = {
if let animationDuration = keyboardAnimationDetail?[UIResponder.keyboardAnimationDurationUserInfoKey] as? Int {
return Double(animationDuration)
} else {
return 0
}
}()
let options = UIView.AnimationOptions(rawValue: ((UInt(animationCurve << 16))))
let translate = CGAffineTransform.identity
UIView.animate(withDuration: duration, delay: 0, options: options, animations: {
self.closeButtonUI.alpha = 1
self.buttonsStackView.isHidden = false
self.wrapperView.transform = translate
self.buttonsStackView.layoutIfNeeded()
}, completion: { _ in
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment