From e65d1efa51c13bebdd335d1dd648a80697810e02 Mon Sep 17 00:00:00 2001 From: juliand665 Date: Sun, 1 Jul 2018 20:49:21 +0200 Subject: [PATCH 1/3] cleaned up functionally identical but a lot nicer to read --- Spring/KeyboardLayoutConstraint.swift | 101 ++++++++++---------------- 1 file changed, 40 insertions(+), 61 deletions(-) diff --git a/Spring/KeyboardLayoutConstraint.swift b/Spring/KeyboardLayoutConstraint.swift index 7d1cf9c..34dfdb9 100644 --- a/Spring/KeyboardLayoutConstraint.swift +++ b/Spring/KeyboardLayoutConstraint.swift @@ -23,88 +23,67 @@ import UIKit #if !os(tvOS) -@available(tvOS, unavailable) public class KeyboardLayoutConstraint: NSLayoutConstraint { + private var offset: CGFloat = 0 - private var offset : CGFloat = 0 - private var keyboardVisibleHeight : CGFloat = 0 + private var visibleKeyboardHeight: CGFloat = 0 { + didSet { + constant = offset + visibleKeyboardHeight + } + } - @available(tvOS, unavailable) override public func awakeFromNib() { super.awakeFromNib() offset = constant - NotificationCenter.default.addObserver(self, selector: #selector(KeyboardLayoutConstraint.keyboardWillShowNotification(_:)), name: NSNotification.Name.UIKeyboardWillShow, object: nil) - NotificationCenter.default.addObserver(self, selector: #selector(KeyboardLayoutConstraint.keyboardWillHideNotification(_:)), name: NSNotification.Name.UIKeyboardWillHide, object: nil) + NotificationCenter.default.addObserver( + self, + selector: #selector(keyboardWillShowNotification), + name: .UIKeyboardWillShow, + object: nil + ) + NotificationCenter.default.addObserver( + self, + selector: #selector(keyboardWillHideNotification), + name: .UIKeyboardWillHide, + object: nil + ) } deinit { NotificationCenter.default.removeObserver(self) } - // MARK: Notification + private func animateChanges(accordingTo userInfo: [AnyHashable: Any]) { + guard + let duration = userInfo[UIKeyboardAnimationDurationUserInfoKey] as? TimeInterval, + let options = userInfo[UIKeyboardAnimationCurveUserInfoKey] as? UInt + else { return } + + UIView.animate( + withDuration: duration, + delay: 0, + options: UIView.AnimationOptions(rawValue: options), + animations: UIApplication.shared.keyWindow!.layoutIfNeeded + ) + } @objc func keyboardWillShowNotification(_ notification: Notification) { - if let userInfo = notification.userInfo { - if let frameValue = userInfo[UIKeyboardFrameEndUserInfoKey] as? NSValue { - let frame = frameValue.cgRectValue - keyboardVisibleHeight = frame.size.height - } - - self.updateConstant() - switch (userInfo[UIKeyboardAnimationDurationUserInfoKey] as? NSNumber, userInfo[UIKeyboardAnimationCurveUserInfoKey] as? NSNumber) { - case let (.some(duration), .some(curve)): - - let options = UIViewAnimationOptions(rawValue: curve.uintValue) - - UIView.animate( - withDuration: TimeInterval(duration.doubleValue), - delay: 0, - options: options, - animations: { - UIApplication.shared.keyWindow?.layoutIfNeeded() - return - }, completion: { finished in - }) - default: - - break - } - - } + guard let userInfo = notification.userInfo else { return } + + guard let frame = userInfo[UIKeyboardFrameEndUserInfoKey] as? CGRect else { return } + visibleKeyboardHeight = frame.size.height + animateChanges(accordingTo: userInfo) } @objc func keyboardWillHideNotification(_ notification: NSNotification) { - keyboardVisibleHeight = 0 - self.updateConstant() + guard let userInfo = notification.userInfo else { return } - if let userInfo = notification.userInfo { - - switch (userInfo[UIKeyboardAnimationDurationUserInfoKey] as? NSNumber, userInfo[UIKeyboardAnimationCurveUserInfoKey] as? NSNumber) { - case let (.some(duration), .some(curve)): - - let options = UIViewAnimationOptions(rawValue: curve.uintValue) - - UIView.animate( - withDuration: TimeInterval(duration.doubleValue), - delay: 0, - options: options, - animations: { - UIApplication.shared.keyWindow?.layoutIfNeeded() - return - }, completion: { finished in - }) - default: - break - } - } - } - - func updateConstant() { - self.constant = offset + keyboardVisibleHeight + visibleKeyboardHeight = 0 + + animateChanges(accordingTo: userInfo) } - } #endif From efd6688d6776a7d20c9daebffb50d4043151061b Mon Sep 17 00:00:00 2001 From: juliand665 Date: Sun, 1 Jul 2018 20:51:47 +0200 Subject: [PATCH 2/3] improved method order --- Spring/KeyboardLayoutConstraint.swift | 28 +++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/Spring/KeyboardLayoutConstraint.swift b/Spring/KeyboardLayoutConstraint.swift index 34dfdb9..4f827b9 100644 --- a/Spring/KeyboardLayoutConstraint.swift +++ b/Spring/KeyboardLayoutConstraint.swift @@ -55,20 +55,6 @@ public class KeyboardLayoutConstraint: NSLayoutConstraint { NotificationCenter.default.removeObserver(self) } - private func animateChanges(accordingTo userInfo: [AnyHashable: Any]) { - guard - let duration = userInfo[UIKeyboardAnimationDurationUserInfoKey] as? TimeInterval, - let options = userInfo[UIKeyboardAnimationCurveUserInfoKey] as? UInt - else { return } - - UIView.animate( - withDuration: duration, - delay: 0, - options: UIView.AnimationOptions(rawValue: options), - animations: UIApplication.shared.keyWindow!.layoutIfNeeded - ) - } - @objc func keyboardWillShowNotification(_ notification: Notification) { guard let userInfo = notification.userInfo else { return } @@ -85,5 +71,19 @@ public class KeyboardLayoutConstraint: NSLayoutConstraint { animateChanges(accordingTo: userInfo) } + + private func animateChanges(accordingTo userInfo: [AnyHashable: Any]) { + guard + let duration = userInfo[UIKeyboardAnimationDurationUserInfoKey] as? TimeInterval, + let options = userInfo[UIKeyboardAnimationCurveUserInfoKey] as? UInt + else { return } + + UIView.animate( + withDuration: duration, + delay: 0, + options: UIView.AnimationOptions(rawValue: options), + animations: UIApplication.shared.keyWindow!.layoutIfNeeded + ) + } } #endif From deb5ada4527e0778281b8555ad60c2ca41b2fd32 Mon Sep 17 00:00:00 2001 From: juliand665 Date: Sun, 1 Jul 2018 20:58:05 +0200 Subject: [PATCH 3/3] swift 4.1 compatibility that's what i get for developing with a beta version of Xcode :facepalm: --- Spring/KeyboardLayoutConstraint.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Spring/KeyboardLayoutConstraint.swift b/Spring/KeyboardLayoutConstraint.swift index 4f827b9..3f6bea3 100644 --- a/Spring/KeyboardLayoutConstraint.swift +++ b/Spring/KeyboardLayoutConstraint.swift @@ -81,7 +81,7 @@ public class KeyboardLayoutConstraint: NSLayoutConstraint { UIView.animate( withDuration: duration, delay: 0, - options: UIView.AnimationOptions(rawValue: options), + options: UIViewAnimationOptions(rawValue: options), animations: UIApplication.shared.keyWindow!.layoutIfNeeded ) }