Last active
August 29, 2015 14:05
-
-
Save josefdlange/55d4003335c1ad7c7d2f to your computer and use it in GitHub Desktop.
Early Swift version of CSRevealingViewController (Two VCs, one gist)
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
| // | |
| // BottomViewController.swift | |
| // SwipeUpViewControllerDemo | |
| // | |
| // Created by Lange, Josef on 7/14/14. | |
| // | |
| import UIKit | |
| class BottomViewController: UIViewController { | |
| var bottomSpaceConstraint : NSLayoutConstraint? | |
| @IBOutlet var containerView : UIView | |
| init(coder aDecoder: NSCoder!) { | |
| super.init(coder: aDecoder) | |
| } | |
| override func viewDidLoad() { | |
| super.viewDidLoad() | |
| let bindings = ["containerView": self.containerView] | |
| self.bottomSpaceConstraint = NSLayoutConstraint.constraintsWithVisualFormat("V:[containerView]-0-|", options: nil, metrics: nil, views: bindings)[0] as? NSLayoutConstraint | |
| self.view.addConstraint(self.bottomSpaceConstraint) | |
| self.view.layoutIfNeeded() | |
| } | |
| override func didReceiveMemoryWarning() { | |
| super.didReceiveMemoryWarning() | |
| // Dispose of any resources that can be recreated. | |
| } | |
| override func willRotateToInterfaceOrientation(toInterfaceOrientation: UIInterfaceOrientation, duration: NSTimeInterval) { | |
| // "Best guess" what our slideup constrain should be. | |
| if(self.bottomSpaceConstraint?.constant > 0.0) { | |
| self.reveal("UP", animated: false, flip: true) | |
| } else if(self.bottomSpaceConstraint?.constant < 0.0) { | |
| self.reveal("DOWN", animated: false, flip: true) | |
| } | |
| } | |
| override func didRotateFromInterfaceOrientation(toInterfaceOrientation: UIInterfaceOrientation) { | |
| if(self.bottomSpaceConstraint?.constant > 0.0) { | |
| self.reveal("UP", animated: false) | |
| } else if(self.bottomSpaceConstraint?.constant < 0.0) { | |
| self.restoreTopView() | |
| } | |
| } | |
| func reveal(translation : Float) { | |
| println(translation) | |
| let translation = translation * -1.0 | |
| let strong = self.bottomSpaceConstraint as NSLayoutConstraint | |
| if (0 < translation) { | |
| strong.constant = translation | |
| self.view.layoutIfNeeded() | |
| } | |
| } | |
| func reveal(direction: NSString, animated: Bool, flip: Bool = false) { | |
| var changeFlag = 1.0 as Float | |
| if direction == "UP" { | |
| changeFlag = 1.0 as Float | |
| } else if direction == "DOWN" { | |
| changeFlag = -1.0 as Float | |
| } | |
| var constant = flip ? self.view.frame.size.width : self.view.frame.size.height | |
| var newBottomSpace = changeFlag * constant as Float - 100.0 | |
| if(animated == true) { | |
| UIView.animateWithDuration(0.2, animations: { | |
| let strong = self.bottomSpaceConstraint as NSLayoutConstraint | |
| strong.constant = newBottomSpace | |
| self.view.layoutIfNeeded() | |
| }, completion: { | |
| _ in | |
| return | |
| }) | |
| } else { | |
| let strong = self.bottomSpaceConstraint as NSLayoutConstraint | |
| strong.constant = newBottomSpace | |
| self.view.layoutIfNeeded() | |
| } | |
| } | |
| @IBAction func restoreTopView() { | |
| UIView.animateWithDuration(0.05, animations: { | |
| let strong = self.bottomSpaceConstraint as NSLayoutConstraint | |
| strong.constant = 0.0 | |
| self.view.layoutIfNeeded() | |
| }, completion: { | |
| _ in | |
| return | |
| }) | |
| } | |
| } | |
| // | |
| // TopViewController.swift | |
| // SwipeUpViewControllerDemo | |
| // | |
| // Created by Lange, Josef on 7/14/14. | |
| // | |
| import UIKit | |
| class TopViewController: UIViewController, UIGestureRecognizerDelegate { | |
| init(coder aDecoder: NSCoder!) { | |
| super.init(coder: aDecoder) | |
| } | |
| override func viewDidLoad() { | |
| super.viewDidLoad() | |
| var panRecognizer = UIPanGestureRecognizer(target: self, action: "respondToPan:") | |
| self.view.addGestureRecognizer(panRecognizer) | |
| // Do any additional setup after loading the view. | |
| } | |
| override func didReceiveMemoryWarning() { | |
| super.didReceiveMemoryWarning() | |
| // Dispose of any resources that can be recreated. | |
| } | |
| func respondToPan(recognizer: UIPanGestureRecognizer) { | |
| let translationInView = recognizer.translationInView(self.view).y | |
| if(recognizer.state == UIGestureRecognizerState.Ended) { | |
| var verticalVelocity = recognizer.velocityInView(self.view).y | |
| if(abs(verticalVelocity) > 100.0) { | |
| if(verticalVelocity > 0.0) { | |
| self.returnView() | |
| } else { | |
| self.reveal("UP", animated: false) | |
| } | |
| } | |
| } else { | |
| self.moveMyView(translationInView) | |
| } | |
| } | |
| func returnView() { | |
| let below = self.parentViewController as BottomViewController | |
| below.restoreTopView() | |
| } | |
| func moveMyView(translationInView: Float) { | |
| let below = self.parentViewController as BottomViewController | |
| below.reveal(translationInView) | |
| } | |
| func reveal(direction : NSString, animated: Bool) { | |
| let below = self.parentViewController as BottomViewController | |
| below.reveal(direction, animated: animated) | |
| } | |
| @IBAction func revealBelow(sender : UIControl) { | |
| self.reveal("UP", animated: true) | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment