Skip to content

Commit

Permalink
Merge pull request #82 from wordpress-mobile/merge/1.9.0-into-trunk
Browse files Browse the repository at this point in the history
Merge 1.9.0 into trunk
  • Loading branch information
jkmassel authored Jan 11, 2021
2 parents cf177b3 + fd6f923 commit 105a797
Show file tree
Hide file tree
Showing 9 changed files with 184 additions and 18 deletions.
4 changes: 4 additions & 0 deletions Example/Example.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
objects = {

/* Begin PBXBuildFile section */
46B30DCD2583F63100A25E66 /* KeyboardAnimationsExampleViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 46B30DCC2583F63100A25E66 /* KeyboardAnimationsExampleViewController.swift */; };
F1C4EBE5218A37D700B8A9F7 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = F1C4EBE4218A37D700B8A9F7 /* AppDelegate.swift */; };
F1C4EBE7218A37D700B8A9F7 /* ViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = F1C4EBE6218A37D700B8A9F7 /* ViewController.swift */; };
F1C4EBEA218A37D700B8A9F7 /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = F1C4EBE8218A37D700B8A9F7 /* Main.storyboard */; };
Expand Down Expand Up @@ -70,6 +71,7 @@
/* End PBXCopyFilesBuildPhase section */

/* Begin PBXFileReference section */
46B30DCC2583F63100A25E66 /* KeyboardAnimationsExampleViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = KeyboardAnimationsExampleViewController.swift; sourceTree = "<group>"; };
F1C4EBE1218A37D700B8A9F7 /* Example.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = Example.app; sourceTree = BUILT_PRODUCTS_DIR; };
F1C4EBE4218A37D700B8A9F7 /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = "<group>"; };
F1C4EBE6218A37D700B8A9F7 /* ViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ViewController.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -139,6 +141,7 @@
children = (
F1C4EBE4218A37D700B8A9F7 /* AppDelegate.swift */,
F1C4EBE6218A37D700B8A9F7 /* ViewController.swift */,
46B30DCC2583F63100A25E66 /* KeyboardAnimationsExampleViewController.swift */,
F1C4EBE8218A37D700B8A9F7 /* Main.storyboard */,
F1C4EBEB218A37D800B8A9F7 /* Assets.xcassets */,
F1C4EBED218A37D800B8A9F7 /* LaunchScreen.storyboard */,
Expand Down Expand Up @@ -346,6 +349,7 @@
buildActionMask = 2147483647;
files = (
F1C4EBE7218A37D700B8A9F7 /* ViewController.swift in Sources */,
46B30DCD2583F63100A25E66 /* KeyboardAnimationsExampleViewController.swift in Sources */,
F1C4EBE5218A37D700B8A9F7 /* AppDelegate.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand Down
96 changes: 83 additions & 13 deletions Example/Example/Base.lproj/Main.storyboard

Large diffs are not rendered by default.

63 changes: 63 additions & 0 deletions Example/Example/KeyboardAnimationsExampleViewController.swift
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 = []
}
}
5 changes: 5 additions & 0 deletions Example/Example/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ class ViewController: UITableViewController
self.showFancyButtons()
})
]),
DemoSection(title: "Keyboard Animations", rows: [
DemoRow(title: "Keyboard Animations", action: {
self.performSegue(withIdentifier: "KeyboardAnimationsSegue", sender: self)
})
])
]
}

Expand Down
2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ GEM
httpclient (2.8.3)
i18n (0.9.5)
concurrent-ruby (~> 1.0)
json (2.2.0)
json (2.3.1)
minitest (5.12.2)
molinillo (0.6.6)
nanaimo (0.2.6)
Expand Down
2 changes: 1 addition & 1 deletion WordPressUI.podspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Pod::Spec.new do |s|
s.name = "WordPressUI"
s.version = "1.8.0"
s.version = "1.9.0"
s.summary = "Home of reusable WordPress UI components."

s.description = <<-DESC
Expand Down
8 changes: 8 additions & 0 deletions WordPressUI/BottomSheet/BottomSheetViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -167,10 +167,18 @@ public class BottomSheetViewController: UIViewController {
}

@objc func keyboardWillShow(_ notification: NSNotification) {
guard childViewController?.presentedViewController == nil else {
return
}

self.presentedVC?.transition(to: .expanded)
}

@objc func keyboardWillHide(_ notification: NSNotification) {
guard childViewController?.presentedViewController == nil else {
return
}

self.presentedVC?.transition(to: .collapsed)
}
}
Expand Down
2 changes: 1 addition & 1 deletion WordPressUI/BottomSheet/DrawerPresentationController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -484,7 +484,7 @@ private extension DrawerPresentationController {
/// Halts scrolling when scrolling down from expanded or up from compact
haltScrolling(scrollView)

} else if scrollView.isScrolling || isPresentedViewAnimating {
} else if scrollView.isScrolling {

if isPresentedViewAnchored {
/// Allow normal scrolling (with tracking)
Expand Down
20 changes: 18 additions & 2 deletions WordPressUI/Extensions/UIView+Animations.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import Foundation

import UIKit

// MARK: UIView Animation Helpers
//
Expand Down Expand Up @@ -153,6 +152,23 @@ extension UIView {
}
}

/// Coordinates an animation block alongside a keyboard's notification animation event.
/// - Parameters:
/// - notification: A notficiation from a keyboard change event (keyboardWillShowNotification, keyboardWillHideNotification, etc)
/// - animations: The animation block to be preformed. The block will provide the rects from keyboardFrameBeginUserInfoKey and keyboardFrameEndUserInfoKey to the animation block.
///
public static func animate(withKeyboard notification: Notification, _ animations: @escaping (CGRect, CGRect) -> Void ) {
guard let userInfo = notification.userInfo else { return }
let duration: TimeInterval = userInfo[UIResponder.keyboardAnimationDurationUserInfoKey] as? TimeInterval ?? 0
let beginFrame: CGRect = (userInfo[UIResponder.keyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue ?? CGRect.zero
let endFrame: CGRect = (userInfo[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue ?? CGRect.zero
let animationCurve: AnimationOptions = AnimationOptions(rawValue: (userInfo[UIResponder.keyboardAnimationCurveUserInfoKey] as? UInt ?? 0))

UIView.animate(withDuration: duration, delay: 0, options: animationCurve, animations: {
animations(beginFrame, endFrame)
}, completion: nil)
}

/// Private Constants
///
private struct Animations {
Expand Down

0 comments on commit 105a797

Please sign in to comment.