-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #82 from wordpress-mobile/merge/1.9.0-into-trunk
Merge 1.9.0 into trunk
- Loading branch information
Showing
9 changed files
with
184 additions
and
18 deletions.
There are no files selected for viewing
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
Large diffs are not rendered by default.
Oops, something went wrong.
63 changes: 63 additions & 0 deletions
63
Example/Example/KeyboardAnimationsExampleViewController.swift
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import UIKit | ||
|
||
class KeyboardAnimationsExampleViewController: UIViewController { | ||
@IBOutlet weak var textView: UITextField! | ||
@IBOutlet weak var bottomConstraintForAnimation: NSLayoutConstraint! | ||
private var notificationObservers: [NSObjectProtocol] = [] | ||
|
||
override func viewDidLoad() { | ||
super.viewDidLoad() | ||
startObservingKeyboardChanges() | ||
} | ||
|
||
deinit { | ||
stopObservingKeyboardChanges() | ||
} | ||
|
||
@IBAction func didTapButton(_ sender: Any) { | ||
if textView.isFirstResponder { | ||
textView.resignFirstResponder() | ||
} else { | ||
textView.becomeFirstResponder() | ||
} | ||
} | ||
|
||
private func keybaordDidOpen(_ endFrame: CGRect) { | ||
bottomConstraintForAnimation.constant = endFrame.height - self.view.safeAreaInsets.bottom + 10 | ||
view.setNeedsLayout() | ||
view.layoutIfNeeded() | ||
} | ||
|
||
func startObservingKeyboardChanges() { | ||
let willShowObserver = NotificationCenter.default.addObserver(forName: UIResponder.keyboardWillShowNotification, object: nil, queue: .main) { (notification) in | ||
UIView.animate(withKeyboard: notification) { (_, endFrame) in | ||
self.keybaordDidOpen(endFrame) | ||
} | ||
} | ||
|
||
let willHideObserver = NotificationCenter.default.addObserver(forName: UIResponder.keyboardWillHideNotification, object: nil, queue: .main) { (notification) in | ||
UIView.animate(withKeyboard: notification) { (beginFrame, endFrame) in | ||
self.bottomConstraintForAnimation.constant = 20 | ||
self.view.setNeedsLayout() | ||
self.view.layoutIfNeeded() | ||
} | ||
} | ||
|
||
let willChangeFrameObserver = NotificationCenter.default.addObserver(forName: UIResponder.keyboardWillChangeFrameNotification, object: nil, queue: .main) { (notification) in | ||
UIView.animate(withKeyboard: notification) { (_, endFrame) in | ||
self.keybaordDidOpen(endFrame) | ||
} | ||
} | ||
|
||
notificationObservers.append(willShowObserver) | ||
notificationObservers.append(willHideObserver) | ||
notificationObservers.append(willChangeFrameObserver) | ||
} | ||
|
||
private func stopObservingKeyboardChanges() { | ||
notificationObservers.forEach { (observer) in | ||
NotificationCenter.default.removeObserver(observer) | ||
} | ||
notificationObservers = [] | ||
} | ||
} |
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
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
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
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
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
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