Skip to content

Instantly share code, notes, and snippets.

@jongwonwoo
Created March 28, 2016 06:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jongwonwoo/e759e3d045b6706f3cb0 to your computer and use it in GitHub Desktop.
Save jongwonwoo/e759e3d045b6706f3cb0 to your computer and use it in GitHub Desktop.
public class ElasticTransition : NSObject, ElasticMotionStateMachineDelegate {
init(presentedViewController: UIViewController, presentingViewWidth: Float) {
self.stateMachine = ElasticMotionStateMachine(ElasticMotionDirection.Right, threshold: threshold, vibrationSec: 2.0)
self.stateMachine.delegate = self
}
func elasticMotionStateMachine(stateMachine: ElasticMotionStateMachine, didChangeState state: ElasticMotionState, deltaPoint: CGPoint) {
if stateMachine.direction == ElasticMotionDirection.Right {
let fullOpenedWidth = CGFloat(presentingViewWidth)
switch state {
case .MayOpen:
let newOriginX = presentedViewController.view.frame.origin.x + deltaPoint.x
if newOriginX >= 0 && newOriginX < CGFloat(threshold) {
presentedViewController.view.center = CGPointMake(presentedViewController.view.center.x + deltaPoint.x, presentedViewController.view.center.y)
}
case .MayClose:
let newOriginX = presentedViewController.view.frame.origin.x + deltaPoint.x
if newOriginX >= 0 && newOriginX < fullOpenedWidth {
presentedViewController.view.center = CGPointMake(presentedViewController.view.center.x + deltaPoint.x, presentedViewController.view.center.y)
}
case .WillClose:
presentedViewController.view.center = CGPointMake(presentedViewController.view.frame.width / 2 + CGFloat(threshold), presentedViewController.view.center.y)
case .Closed:
presentedViewController.view.center = CGPointMake(presentedViewController.view.frame.width / 2, presentedViewController.view.center.y)
case .WillOpen:
presentedViewController.view.center = CGPointMake(presentedViewController.view.frame.width / 2 + CGFloat(threshold), presentedViewController.view.center.y)
case .Opened:
presentedViewController.view.center = CGPointMake(presentedViewController.view.frame.width / 2 + fullOpenedWidth, presentedViewController.view.center.y)
}
}
// TODO: handle other directions
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment