Last active
July 6, 2018 07:11
-
-
Save dev-yong/cb6dcc929021103eef624cd0d705974b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Created by 이광용 on 2018. 7. 3.. | |
// Copyright © 2018년 이광용. All rights reserved. | |
// | |
import UIKit | |
typealias KeyboardInfo = (CGRect, TimeInterval)->() | |
protocol KeyboardControlDelegate: class { | |
func setKyeboardControl(willShow: KeyboardInfo?, willHide: KeyboardInfo?) | |
func removeKeyboardControl() | |
} | |
extension KeyboardControlService where Self: UIViewController { | |
func setKyeboardControl(willShow: KeyboardInfo? = nil, willHide: KeyboardInfo? = nil) { | |
NotificationCenter.default.addObserver(forName: .UIKeyboardWillShow, object: nil, queue: nil) { (notification) in | |
self.keyboardWillShow(notification, willShow: willShow) | |
} | |
NotificationCenter.default.addObserver(forName: .UIKeyboardWillHide, object: nil, queue: nil) { (notification) in | |
self.kyeboardWillHide(notification, willHide: willHide) | |
} | |
} | |
func removeKeyboardControl() { | |
NotificationCenter.default.removeObserver(self) | |
} | |
func keyboardWillShow(_ notification: Notification, willShow: KeyboardInfo? = nil) | |
{ | |
guard let keyboardSize = (notification.userInfo?[UIKeyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue else {return} | |
guard let animationDuration = notification.userInfo?[UIKeyboardAnimationDurationUserInfoKey] as? TimeInterval else {return} | |
guard let willShow = willShow else {return} | |
willShow(keyboardSize, animationDuration) | |
} | |
func kyeboardWillHide(_ notification: Notification, willHide: KeyboardInfo? = nil) | |
{ | |
guard let keyboardSize = (notification.userInfo?[UIKeyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue else {return} | |
guard let animationDuration = notification.userInfo?[UIKeyboardAnimationDurationUserInfoKey] as? TimeInterval else {return} | |
guard let willHide = willHide else {return} | |
willHide(keyboardSize, animationDuration) | |
} | |
} | |
/* Example | |
class ViewController: UIViewController, KeyboardControlService { | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
self.setKyeboardControl(willShow: setKeyboardWillShow(), willHide: setKyeboarWillHide()) | |
} | |
func setKeyboardWillShow() -> KeyboardInfo{ | |
return { rect, duration in | |
print("keyboard will show - frame : \(rect), duration : \(duration)") | |
} | |
} | |
func setKyeboarWillHide() -> KeyboardInfo{ | |
return { rect, duration in | |
print("keyboard will hide - frame : \(rect), duration : \(duration)") | |
} | |
} | |
deinit { | |
self.removeKeyboardControl() | |
} | |
} | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment