Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HUD moves to accomodate keyboard #179

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions Demo/DemoViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,24 @@ import PKHUD

class DemoViewController: UIViewController {

let hiddenTextField = UITextField(frame: CGRect.zero)

override func viewDidLoad() {
super.viewDidLoad()

view.addSubview(hiddenTextField)
HUD.registerForKeyboardNotifications()
HUD.dimsBackground = false
HUD.allowsInteraction = false
}

@IBAction func showHideKeyboard(_ sender: Any) {
if hiddenTextField.isEditing {
view.endEditing(true)
} else {
hiddenTextField.becomeFirstResponder()
}
}
@IBAction func showAnimatedSuccessHUD(_ sender: AnyObject) {
HUD.flash(.success, delay: 2.0)
}
Expand Down Expand Up @@ -51,6 +62,10 @@ class DemoViewController: UIViewController {
}
}

deinit {
HUD.deregisterFromKeyboardNotifications()
}

/*

Please note that the above demonstrates the "porcelain" interface - a more concise and clean way to work with the HUD.
Expand Down
20 changes: 20 additions & 0 deletions Demo/Images.xcassets/AppIcon.appiconset/Contents.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
{
"images" : [
{
"idiom" : "iphone",
"size" : "20x20",
"scale" : "2x"
},
{
"idiom" : "iphone",
"size" : "20x20",
"scale" : "3x"
},
{
"idiom" : "iphone",
"size" : "29x29",
Expand Down Expand Up @@ -30,6 +40,16 @@
"size" : "60x60",
"scale" : "3x"
},
{
"idiom" : "ipad",
"size" : "20x20",
"scale" : "1x"
},
{
"idiom" : "ipad",
"size" : "20x20",
"scale" : "2x"
},
{
"idiom" : "ipad",
"size" : "29x29",
Expand Down
83 changes: 51 additions & 32 deletions Demo/Storyboard.storyboard

Large diffs are not rendered by default.

5 changes: 3 additions & 2 deletions PKHUD.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -328,8 +328,8 @@
F99631F419514FAC001F73CA /* Project object */ = {
isa = PBXProject;
attributes = {
LastSwiftUpdateCheck = 0820;
LastUpgradeCheck = 0900;
LastSwiftUpdateCheck = 0830;
LastUpgradeCheck = 0800;
ORGANIZATIONNAME = NSExceptional;
TargetAttributes = {
12CE19F01E25784C0062D873 = {
Expand All @@ -355,6 +355,7 @@
hasScannedForEncodings = 0;
knownRegions = (
en,
Base,
);
mainGroup = F99631F319514FAC001F73CA;
productRefGroup = F99631FD19514FAC001F73CA /* Products */;
Expand Down
9 changes: 9 additions & 0 deletions PKHUD/HUD.swift
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,15 @@ public final class HUD {
HUD.hide(afterDelay: delay, completion: completion)
}

// MARK: Keyboard Methods
public static func registerForKeyboardNotifications() {
PKHUD.sharedHUD.registerForKeyboardNotifications()
}

public static func deregisterFromKeyboardNotifications() {
PKHUD.sharedHUD.deregisterFromKeyboardNotifications()
}

// MARK: Private methods
fileprivate static func contentView(_ content: HUDContentType) -> UIView {
switch content {
Expand Down
8 changes: 8 additions & 0 deletions PKHUD/PKHUD.swift
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,14 @@ open class PKHUD: NSObject {
}
}

internal func registerForKeyboardNotifications() {
container.registerForKeyboardNotifications()
}

internal func deregisterFromKeyboardNotifications() {
container.deregisterFromKeyboardNotifications()
}

// MARK: Timer callbacks

@objc internal func performDelayedHide(_ timer: Timer? = nil) {
Expand Down
70 changes: 67 additions & 3 deletions PKHUD/Window.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import UIKit
/// The window used to display the PKHUD within. Placed atop the applications main window.
internal class ContainerView: UIView {

private var keyboardIsVisible = false
private var keyboardHeight: CGFloat = 0.0
internal let frameView: FrameView
internal init(frameView: FrameView = FrameView()) {
self.frameView = frameView
Expand All @@ -28,21 +30,20 @@ internal class ContainerView: UIView {
fileprivate func commonInit() {
backgroundColor = UIColor.clear
isHidden = true

addSubview(backgroundView)
addSubview(frameView)
}

internal override func layoutSubviews() {
super.layoutSubviews()

frameView.center = center
frameView.center = calculateHudCenter()
backgroundView.frame = bounds
}

internal func showFrameView() {
layer.removeAllAnimations()
frameView.center = center
frameView.center = calculateHudCenter()
frameView.alpha = 1.0
isHidden = false
}
Expand Down Expand Up @@ -101,4 +102,67 @@ internal class ContainerView: UIView {
backgroundView.alpha = 0.0
}
}

// MARK: Notifications
internal func registerForKeyboardNotifications() {
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow(notification:)), name: NSNotification.Name.UIKeyboardWillShow, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillBeHidden(notification:)), name: NSNotification.Name.UIKeyboardWillHide, object: nil)
}

internal func deregisterFromKeyboardNotifications() {
NotificationCenter.default.removeObserver(self, name: NSNotification.Name.UIKeyboardWillShow, object: nil)
NotificationCenter.default.removeObserver(self, name: NSNotification.Name.UIKeyboardWillHide, object: nil)
}

// MARK: Triggered Functions
@objc private func keyboardWillShow(notification: NSNotification) {
keyboardIsVisible = true
guard let userInfo = notification.userInfo else {
return
}
if let keyboardHeight = (userInfo[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue.height {
self.keyboardHeight = keyboardHeight
}
if !self.isHidden {
if let duration = userInfo[UIKeyboardAnimationDurationUserInfoKey] as? NSNumber,
let curve = userInfo[UIKeyboardAnimationCurveUserInfoKey] as? NSNumber {
animateHUDWith(duration: duration.doubleValue,
curve: UIViewAnimationCurve(rawValue: curve.intValue) ?? UIViewAnimationCurve.easeInOut,
toLocation: calculateHudCenter())
}
}
}

@objc private func keyboardWillBeHidden(notification: NSNotification) {
keyboardIsVisible = false
if !self.isHidden {
guard let userInfo = notification.userInfo else {
return
}
if let duration = userInfo[UIKeyboardAnimationDurationUserInfoKey] as? NSNumber,
let curve = userInfo[UIKeyboardAnimationCurveUserInfoKey] as? NSNumber {
animateHUDWith(duration: duration.doubleValue,
curve: UIViewAnimationCurve(rawValue: curve.intValue) ?? UIViewAnimationCurve.easeInOut,
toLocation: calculateHudCenter())
}
}
}

// MARK: - Helpers
private func animateHUDWith(duration: Double, curve: UIViewAnimationCurve, toLocation location: CGPoint) {
UIView.beginAnimations(nil, context: nil)
UIView.setAnimationDuration(TimeInterval(duration))
UIView.setAnimationCurve(curve)
frameView.center = location
UIView.commitAnimations()
}

private func calculateHudCenter() -> CGPoint {
if !keyboardIsVisible {
return center
} else {
let yLocation = (frame.height - keyboardHeight) / 2
return CGPoint(x: center.x, y: yLocation)
}
}
}