keyboard animation cleanup

pull/1/head
Michael Kirk 7 years ago
parent 0808458392
commit 279694e704

@ -198,101 +198,20 @@ public class AttachmentApprovalViewController: UIPageViewController, UIPageViewC
self.setCurrentItem(firstItem, direction: .forward, animated: false) self.setCurrentItem(firstItem, direction: .forward, animated: false)
// NotificationCenter.default.addObserver(self, NotificationCenter.default.addObserver(self,
// selector: #selector(keyboardWillChangeFrame(notification:)), selector: #selector(keyboardWillShow(notification:)),
// name: .UIKeyboardWillChangeFrame, name: .UIKeyboardWillShow,
// object: nil) object: nil)
NotificationCenter.default.addObserver(self,
observers = [ selector: #selector(keyboardDidShow(notification:)),
NotificationCenter.default.addObserver(forName: .UIKeyboardWillShow, object: nil, queue: nil) { [weak self] notification in name: .UIKeyboardDidShow,
guard let strongSelf = self else { return } object: nil)
NotificationCenter.default.addObserver(self,
guard let userInfo = notification.userInfo else { selector: #selector(keyboardWillHide(notification:)),
owsFailDebug("userInfo was unexpectedly nil") name: .UIKeyboardWillHide,
return object: nil)
}
guard let keyboardStartFrame = userInfo[UIKeyboardFrameBeginUserInfoKey] as? CGRect else {
owsFailDebug("keyboardEndFrame was unexpectedly nil")
return
}
guard let keyboardEndFrame = userInfo[UIKeyboardFrameEndUserInfoKey] as? CGRect else {
owsFailDebug("keyboardEndFrame was unexpectedly nil")
return
}
Logger.debug("UIKeyboardWillShow frame: \(keyboardStartFrame) -> \(keyboardEndFrame)")
strongSelf.keyboardWillShow(notification: notification)
},
NotificationCenter.default.addObserver(forName: .UIKeyboardDidShow, object: nil, queue: nil) { [weak self] notification in
guard let strongSelf = self else { return }
guard let userInfo = notification.userInfo else {
owsFailDebug("userInfo was unexpectedly nil")
return
}
guard let keyboardStartFrame = userInfo[UIKeyboardFrameBeginUserInfoKey] as? CGRect else {
owsFailDebug("keyboardEndFrame was unexpectedly nil")
return
}
guard let keyboardEndFrame = userInfo[UIKeyboardFrameEndUserInfoKey] as? CGRect else {
owsFailDebug("keyboardEndFrame was unexpectedly nil")
return
}
Logger.debug("UIKeyboardDidShow frame: \(keyboardStartFrame) -> \(keyboardEndFrame)")
strongSelf.keyboardDidShow(notification: notification)
},
NotificationCenter.default.addObserver(forName: .UIKeyboardWillHide, object: nil, queue: nil) { [weak self] notification in
guard let strongSelf = self else { return }
guard let userInfo = notification.userInfo else {
owsFailDebug("userInfo was unexpectedly nil")
return
}
guard let keyboardStartFrame = userInfo[UIKeyboardFrameBeginUserInfoKey] as? CGRect else {
owsFailDebug("keyboardEndFrame was unexpectedly nil")
return
}
guard let keyboardEndFrame = userInfo[UIKeyboardFrameEndUserInfoKey] as? CGRect else {
owsFailDebug("keyboardEndFrame was unexpectedly nil")
return
}
Logger.debug("UIKeyboardWillHide frame: \(keyboardStartFrame) -> \(keyboardEndFrame)")
strongSelf.keyboardWillHide(notification: notification)
}
// NotificationCenter.default.addObserver(forName: .UIKeyboardDidHide, object: nil, queue: nil) { [weak self] notification in
// guard let userInfo = notification.userInfo else {
// owsFailDebug("userInfo was unexpectedly nil")
// return
// }
//
// guard let keyboardStartFrame = userInfo[UIKeyboardFrameBeginUserInfoKey] as? CGRect else {
// owsFailDebug("keyboardEndFrame was unexpectedly nil")
// return
// }
//
// guard let keyboardEndFrame = userInfo[UIKeyboardFrameEndUserInfoKey] as? CGRect else {
// owsFailDebug("keyboardEndFrame was unexpectedly nil")
// return
// }
//
// Logger.debug("UIKeyboardDidHide frame: \(keyboardStartFrame) -> \(keyboardEndFrame)")
// },
]
} }
var observers: [NSObjectProtocol] = []
override public func viewWillAppear(_ animated: Bool) { override public func viewWillAppear(_ animated: Bool) {
Logger.debug("") Logger.debug("")
super.viewWillAppear(animated) super.viewWillAppear(animated)
@ -325,39 +244,20 @@ public class AttachmentApprovalViewController: UIPageViewController, UIPageViewC
} }
var lastObservedKeyboardHeight: CGFloat = 0 var lastObservedKeyboardHeight: CGFloat = 0
var firstObservedKeyboardHeight: CGFloat?
var inputAccessorySnapshotView: UIView? var inputAccessorySnapshotView: UIView?
@objc @objc
func keyboardDidShow(notification: Notification) { func keyboardDidShow(notification: Notification) {
if self.isFirstResponder { // If this is a result of the vc becoming first responder, they keyboard isn't actually
if self.inputAccessorySnapshotView != nil { // showing, rather the inputAccessoryView is now showing, so we want to remove any
removeToolbarSnapshot() // previously added toolbar snapshot.
} else { if isFirstResponder, inputAccessorySnapshotView != nil {
Logger.verbose("nothing to remove") removeToolbarSnapshot()
}
} else {
if self.inputAccessorySnapshotView == nil {
// addToolbarSnapshot()
Logger.verbose("no-op")
} else {
Logger.verbose("nothing to show")
}
} }
} }
@objc @objc
func keyboardWillShow(notification: Notification) { func keyboardWillShow(notification: Notification) {
if self.isFirstResponder {
Logger.verbose("no-op")
} else {
if self.inputAccessorySnapshotView == nil {
// addToolbarSnapshot()
Logger.verbose("no-op")
} else {
Logger.verbose("nothing to show")
}
}
guard let userInfo = notification.userInfo else { guard let userInfo = notification.userInfo else {
owsFailDebug("userInfo was unexpectedly nil") owsFailDebug("userInfo was unexpectedly nil")
return return
@ -374,11 +274,7 @@ public class AttachmentApprovalViewController: UIPageViewController, UIPageViewC
} }
Logger.debug("\(keyboardStartFrame) -> \(keyboardEndFrame)") Logger.debug("\(keyboardStartFrame) -> \(keyboardEndFrame)")
lastObservedKeyboardHeight = keyboardEndFrame.size.height lastObservedKeyboardHeight = keyboardEndFrame.size.height
if firstObservedKeyboardHeight == nil {
firstObservedKeyboardHeight = keyboardEndFrame.size.height
}
viewControllers?.forEach { viewController in viewControllers?.forEach { viewController in
guard let prepViewController = viewController as? AttachmentPrepViewController else { guard let prepViewController = viewController as? AttachmentPrepViewController else {
@ -713,7 +609,7 @@ extension AttachmentApprovalViewController: AttachmentPrepViewControllerDelegate
} }
func prepViewController(_ prepViewController: AttachmentPrepViewController, willBeginEditingCaptionView captionView: CaptionView) { func prepViewController(_ prepViewController: AttachmentPrepViewController, willBeginEditingCaptionView captionView: CaptionView) {
addToolbarSnapshot() addInputAccessorySnapshot()
} }
func prepViewController(_ prepViewController: AttachmentPrepViewController, didBeginEditingCaptionView captionView: CaptionView) { func prepViewController(_ prepViewController: AttachmentPrepViewController, didBeginEditingCaptionView captionView: CaptionView) {
@ -726,7 +622,7 @@ extension AttachmentApprovalViewController: AttachmentPrepViewControllerDelegate
disablePaging() disablePaging()
} }
func addToolbarSnapshot() { func addInputAccessorySnapshot() {
assert(inputAccessorySnapshotView == nil) assert(inputAccessorySnapshotView == nil)
// To fix a layout glitch where the snapshot view is 1/2 the width of the screen, it's key // To fix a layout glitch where the snapshot view is 1/2 the width of the screen, it's key
// that we use `bottomToolView` and not `inputAccessoryView` which can trigger a layout of // that we use `bottomToolView` and not `inputAccessoryView` which can trigger a layout of
@ -771,18 +667,13 @@ extension AttachmentApprovalViewController: AttachmentPrepViewControllerDelegate
// responder there is an instant where the lastObservedKeyboardHeight is effectively // responder there is an instant where the lastObservedKeyboardHeight is effectively
// 0. Rather than animate the CaptionView all the way to the bottom screen edge, and // 0. Rather than animate the CaptionView all the way to the bottom screen edge, and
// then immediately back up as the inputAccessoryView becomes visible, we never inset // then immediately back up as the inputAccessoryView becomes visible, we never inset
// the CaptionView nearer to the bottom edge than it's initial position, which should // the CaptionView nearer to the bottom edge than the `bottomToolView.height`
// be the height of the inputAccessoryView. return max(bottomToolView.bounds.height, lastObservedKeyboardHeight)
if let firstObservedKeyboardHeight = firstObservedKeyboardHeight {
return max(bottomToolView.bounds.height, lastObservedKeyboardHeight)
}
return lastObservedKeyboardHeight
} }
// 3. when the MessageTextView becomes first responder, the keyboard should shift up // 4. when the MessageTextView becomes first responder, the keyboard should shift up
// "in front" of the CaptionView // "in front" of the CaptionView
return 0 return bottomToolView.bounds.height
} }
// MARK: Helpers // MARK: Helpers

Loading…
Cancel
Save