Skip to content

Commit

Permalink
Dismiss Popup with slide down animation
Browse files Browse the repository at this point in the history
  • Loading branch information
ricardopereira committed Feb 20, 2019
1 parent b35ce8d commit f1a1dc2
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 8 deletions.
5 changes: 3 additions & 2 deletions Example/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ class ViewController: UIViewController {
return button
}()

lazy var exampleSignInView = ExampleSignInView()
lazy var popupViewController = WSPopupViewController(popupView: exampleSignInView)

override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .white
Expand All @@ -31,9 +34,7 @@ class ViewController: UIViewController {
}

@objc func showtimeTapped(_ sender: Any) {
let exampleSignInView = ExampleSignInView()
exampleSignInView.userTextField.text = "JohnDoe123"
let popupViewController = WSPopupViewController(popupView: exampleSignInView)
//let popupViewController = WSPopupViewController(popupViewType: ExampleSignInView.self)
popupViewController.modalPresentationStyle = .overCurrentContext
popupViewController.modalTransitionStyle = .crossDissolve
Expand Down
40 changes: 34 additions & 6 deletions Source/WSPopupViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,14 @@ public class WSPopupViewController: WSScrollViewController {
fatalError("init(coder:) has not been implemented")
}

deinit {
//print("Deinitialized")
}

public override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = UIColor(red: 0, green: 0, blue: 0, alpha: 0.3)

scrollView.alwaysBounceVertical = true
scrollView.showsVerticalScrollIndicator = false
scrollView.keyboardDismissMode = .interactive
scrollView.delegate = self
scrollView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(self.dismissKeyboard)))
Expand Down Expand Up @@ -63,6 +66,11 @@ public class WSPopupViewController: WSScrollViewController {
}
}

public override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
restoreAnimatedProperties()
}

public override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
registerKeyboardNotifications()
Expand Down Expand Up @@ -161,6 +169,12 @@ public class WSPopupViewController: WSScrollViewController {
)
}

private func restoreAnimatedProperties() {
view.backgroundColor = UIColor(red: 0, green: 0, blue: 0, alpha: 0.3)
popupView.alpha = 1
scrollView.contentInset.top = 0
}

@objc public func dismissKeyboard() {
view.endEditing(true)
}
Expand All @@ -170,6 +184,21 @@ public class WSPopupViewController: WSScrollViewController {
dismiss(animated: true, completion: nil)
}

@objc public func dismissPopupWithSlideDownAnimation(dismissKeyboardManually: Bool = false) {
if dismissKeyboardManually {
dismissKeyboard()
}
UIView.animate(
withDuration: 0.5, delay: 0.0, usingSpringWithDamping: 1.0, initialSpringVelocity: 0.0, options: .curveLinear, animations: { () -> Void in
self.view.backgroundColor = .clear
self.popupView.alpha = 0
self.scrollView.contentInset.top = 2000 //hide
self.scrollView.contentOffset.y = 2000 //hide
}, completion: { isFinished in
self.dismiss(animated: true, completion: nil)
})
}

@objc private func textDidBeginEditing(_ sender: Notification) {
activeInput = sender.object as? UIView
}
Expand Down Expand Up @@ -210,11 +239,10 @@ public class WSPopupViewController: WSScrollViewController {

extension WSPopupViewController: UIScrollViewDelegate {

public func scrollViewDidScroll(_ scrollView: UIScrollView) {
}

public func scrollViewDidEndDragging(_ scrollView: UIScrollView, willDecelerate decelerate: Bool) {
//TODO: dismiss popup on slide down (scrollView.contentOffset.y)
if scrollView.contentOffset.y < 0 && scrollView.panGestureRecognizer.velocity(in: popupView).y > 160 {
dismissPopupWithSlideDownAnimation()
}
}

}
Expand Down

0 comments on commit f1a1dc2

Please sign in to comment.