Created
November 30, 2016 06:54
-
-
Save jverkoey/65f697f9e771ae1183806ed6021f47d0 to your computer and use it in GitHub Desktop.
Tossable drawer transition director in Material Motion
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
| func willBeginTransition(_ transition: Transition) { | |
| self.transition = transition | |
| runtime = StreamRuntime(runtime: transition.runtime) | |
| if transition.foreViewController.preferredContentSize != .zero { | |
| drawerSize = transition.foreViewController.preferredContentSize | |
| } else { | |
| drawerSize = transition.foreViewController.view.bounds.size | |
| } | |
| if transition.direction == .forward { | |
| transition.foreViewController.view.bounds = CGRect(origin: .zero, size: drawerSize) | |
| } | |
| let bounds = transition.containerView.bounds | |
| backValue = bounds.maxX + drawerSize.width / 2 | |
| foreValue = bounds.maxX - drawerSize.width / 2 | |
| positionMemory = Memory(initialValue: transition.direction == .forward ? backValue : foreValue) | |
| let layer = transition.foreViewController.view.layer | |
| layer.position.x = positionMemory.value | |
| spring = springWith(destination: transition.direction == .forward ? foreValue : backValue, | |
| target: layer, | |
| source: positionMemory, | |
| property: positionMemory.property, | |
| threshold: 1) | |
| springStream = spring.rubberBanded(below: foreValue, above: backValue, length: rubberBandLength) | |
| runtime.apply(springStream, to: layer, property: propertyOf(layer).positionX) | |
| } | |
| func add(_ gestureRecognizer: UIGestureRecognizer) { | |
| switch gestureRecognizer { | |
| case let pan as UIPanGestureRecognizer: | |
| let view = transition.foreViewController.view! | |
| let draggable = ADraggable(subscribedTo: pan, | |
| readInitialPointFrom: view, | |
| property: propertyOf(view).position) | |
| .x() | |
| .passthrough(memory: positionMemory) | |
| .rubberBanded(below: foreValue, above: backValue, length: rubberBandLength) | |
| runtime.pause(spring, whileStreamIsActive: draggable) | |
| runtime.apply(draggable, to: view, property: propertyOf(view).positionX) | |
| let velocity = AVelocity(subscribedTo: pan).x() | |
| runtime.apply(velocity, to: spring, property: spring.velocityProperty) | |
| default: () | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment