Created
March 28, 2016 06:52
-
-
Save jongwonwoo/e759e3d045b6706f3cb0 to your computer and use it in GitHub Desktop.
This file contains 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
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