Advertisement
priore

Auto slide the view when keyboard appears

Sep 12th, 2017
326
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 3.53 KB | None | 0 0
  1. // Auto slide the view when keyboard appears
  2. // https://github.com/priore/DPKeyboardManager
  3. //
  4. // HOW TO USE:
  5. //
  6. // import UIKit
  7. // class DPBaseViewController: UIViewController {
  8. //    let keyboardManager = DPKeyboardManager()
  9. //    override func viewDidAppear(_ animated: Bool) {
  10. //        super.viewDidAppear(animated)
  11. //        keyboardManager.enableKeybaordEvents(self.view)
  12. //    }
  13. //
  14. //    override func viewWillDisappear(_ animated: Bool) {
  15. //        super.viewWillDisappear(animated)
  16. //        keyboardManager.disableKeyboardEvents()
  17. //    }
  18. //}
  19.  
  20. import UIKit
  21. import Foundation
  22.  
  23. open class DPKeyboardManager {
  24.    
  25.     private weak var containerView: UIView?
  26.    
  27.     @objc open var currentView: UIView?
  28.     @objc open var keyboardRect: CGRect = CGRect.zero
  29.     @objc open var keyboardPadding: CGFloat = 10.0
  30.    
  31.     @objc public init() {
  32.         // NOP
  33.     }
  34.    
  35.     @objc public init(_ view: UIView) {
  36.         enableKeybaordEvents(view)
  37.     }
  38.    
  39.     deinit {
  40.         disableKeyboardEvents()
  41.     }
  42.    
  43.     @objc open func enableKeybaordEvents(_ view: UIView) {
  44.        
  45.         containerView = view
  46.        
  47.         NotificationCenter.default.addObserver(self, selector: #selector(textDidBeginEditing), name: NSNotification.Name.UITextFieldTextDidBeginEditing, object: nil)
  48.         NotificationCenter.default.addObserver(self, selector: #selector(textDidBeginEditing), name: NSNotification.Name.UITextViewTextDidBeginEditing, object: nil)
  49.        
  50.         NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow), name: NSNotification.Name.UIKeyboardWillShow, object: nil)
  51.         NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillHide), name: NSNotification.Name.UIKeyboardWillHide, object: nil)
  52.     }
  53.    
  54.     @objc open func disableKeyboardEvents() {
  55.        
  56.         NotificationCenter.default.removeObserver(self)
  57.     }
  58.    
  59.     // MARK: - Keyboard
  60.    
  61.     @objc private func keyboardWillShow(notification:NSNotification) {
  62.        
  63.         let userInfo = notification.userInfo!
  64.         let keyboardFrame:NSValue = userInfo[UIKeyboardFrameEndUserInfoKey] as! NSValue
  65.         self.keyboardRect = keyboardFrame.cgRectValue
  66.        
  67.         translateTextField()
  68.     }
  69.    
  70.     @objc private func keyboardWillHide(notification:NSNotification) {
  71.        
  72.         UIView.animate(withDuration: 0.25) {
  73.             DispatchQueue.main.async {
  74.                 self.containerView?.transform = CGAffineTransform.identity
  75.             }
  76.         }
  77.     }
  78.    
  79.     // MARK: - UITextFiled
  80.    
  81.     @objc private func textDidBeginEditing(norification:NSNotification) {
  82.         self.currentView = norification.object as? UIView
  83.         translateTextField()
  84.     }
  85.    
  86.    
  87.     @objc private func textFieldDidBeginEditingAction(_ textField: UITextField) {
  88.         self.currentView = textField
  89.         translateTextField()
  90.     }
  91.    
  92.     @objc private func translateTextField() {
  93.        
  94.         if let view = currentView, keyboardRect.height > 0 {
  95.             let rect = view.superview?.convert(view.frame, to: nil)
  96.             if let maxY = rect?.maxY, maxY > keyboardRect.minY {
  97.                 let delta = maxY - keyboardRect.minY + keyboardPadding
  98.                 UIView.animate(withDuration: 0.25) {
  99.                     DispatchQueue.main.async {
  100.                         self.containerView?.transform = CGAffineTransform.identity.translatedBy(x: 0, y: -delta)
  101.                     }
  102.                 }
  103.             }
  104.         }
  105.        
  106.     }
  107.    
  108. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement