Created
December 1, 2015 20:50
-
-
Save AdamSliwakowski/6fa1e920254ce584d203 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
// | |
// ViewController.swift | |
// KeyboardTest | |
// | |
// Created by Adam Śliwakowski on 01.12.2015. | |
// Copyright © 2015 Adam Śliwakowski. All rights reserved. | |
// | |
import UIKit | |
typealias AnimationClosure = (() -> Void) | |
class ViewController: UIViewController { | |
@IBOutlet weak var constraintYCenter: NSLayoutConstraint! | |
@IBOutlet weak var textField: UITextField! | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
setupKeyboardNotifcationListeners() | |
} | |
override func viewDidAppear(animated: Bool) { | |
super.viewDidAppear(animated) | |
textField.becomeFirstResponder() | |
} | |
override func keyboardWillShowAnimation() -> AnimationClosure { | |
return { | |
self.constraintYCenter.constant = 100 | |
self.view.layoutIfNeeded() | |
} | |
} | |
override func keyboardWillHideAnimation() -> AnimationClosure { | |
return { } | |
} | |
deinit { | |
removeKeyboardNotificationListeners() | |
} | |
} | |
extension UIViewController { | |
var notifications: [(key: String, sel: String)] { | |
return [ | |
(UIKeyboardWillShowNotification, "keyboardWillShow:"), | |
(UIKeyboardWillHideNotification, "keyboardWillHide:") | |
] | |
} | |
var notificationCenter: NSNotificationCenter { | |
return NSNotificationCenter.defaultCenter() | |
} | |
func keyboardWillShowAnimation() -> AnimationClosure { | |
assertionFailure("this needs to be overriden") | |
return { } | |
} | |
func keyboardWillHideAnimation() -> AnimationClosure { | |
assertionFailure("this needs to be overriden") | |
return { } | |
} | |
func setupKeyboardNotifcationListeners() { | |
notifications.forEach { notificationCenter.addObserver(self, selector: Selector($0.sel), name: $0.key, object: nil) } | |
} | |
func removeKeyboardNotificationListeners() { | |
notifications.forEach { notificationCenter.removeObserver(self, name: $0.key, object: nil) } | |
} | |
func keyboardWillShow(notification: NSNotification) { | |
animate(keyboardWillShowAnimation(), notification: notification) | |
} | |
func keyboardWillHide(notification: NSNotification) { | |
animate(keyboardWillHideAnimation(), notification: notification) | |
} | |
func animate(animation: AnimationClosure, notification: NSNotification) { | |
let userInfo = notification.userInfo as! Dictionary<String, AnyObject> | |
let animationDuration = userInfo[UIKeyboardAnimationDurationUserInfoKey] as! NSTimeInterval | |
let animationCurve = userInfo[UIKeyboardAnimationCurveUserInfoKey]!.intValue | |
let curveAnimationOption = UIViewAnimationOptions(rawValue: UInt(animationCurve)) | |
let options: UIViewAnimationOptions = [.BeginFromCurrentState, curveAnimationOption] | |
UIView.animateWithDuration(animationDuration, delay: 0, options: options, animations: animation, completion: nil) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment