Created
January 10, 2017 19:33
-
-
Save jverkoey/93a614051af6498214c72962d4a922ec 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
let pan = UIPanGestureRecognizer() | |
let rotate = UIRotationGestureRecognizer() | |
let scale = UIPinchGestureRecognizer() | |
[pan, rotate, scale].forEach { $0.delegate = self } | |
let directlyManipulable = DirectlyManipulable(view: square, | |
containerView: view, | |
panGestureRecognizer: pan, | |
rotationGestureRecognizer: rotate, | |
scaleGestureRecognizer: scale) | |
let positionSpring = Spring(to: createProperty(withInitialValue: view.center), | |
initialValue: directlyManipulable.draggable.property, | |
threshold: 1) | |
runtime.write(directlyManipulable.draggable.velocityStream, to: positionSpring.initialVelocity) | |
let rotationSpring = Spring(to: createProperty(withInitialValue: 0), | |
initialValue: directlyManipulable.rotatable.property, | |
threshold: CGFloat(M_PI / 180.0)) | |
runtime.write(directlyManipulable.rotatable.velocityStream, to: rotationSpring.initialVelocity) | |
let scaleSpring = Spring(to: createProperty(withInitialValue: 1), | |
initialValue: directlyManipulable.scalable.property, | |
threshold: 0.01) | |
runtime.write(directlyManipulable.scalable.velocityStream, to: scaleSpring.initialVelocity) | |
directlyManipulable.connect(with: runtime) | |
let gestures = runtime.createChild() | |
gestures.write(directlyManipulable.draggable.valueStream, to: Callback { _ in }) | |
gestures.write(directlyManipulable.rotatable.valueStream, to: Callback { _ in }) | |
gestures.write(directlyManipulable.scalable.valueStream, to: Callback { _ in }) | |
subscription = gestures.state.subscribe { state in | |
if state == .active { | |
positionSpring.initialVelocity.write(.zero) | |
rotationSpring.initialVelocity.write(0) | |
scaleSpring.initialVelocity.write(0) | |
} | |
} | |
let stateIsActiveStream = gestures.state._map { $0 == .atRest } | |
let positionSpring$ = popSpringSource(positionSpring) | |
runtime.write(positionSpring$.valve(stateIsActiveStream), | |
to: directlyManipulable.draggable.property) | |
let rotationSpring$ = popSpringSource(rotationSpring) | |
runtime.write(rotationSpring$.valve(stateIsActiveStream), | |
to: directlyManipulable.rotatable.property) | |
let scaleSpring$ = popSpringSource(scaleSpring) | |
runtime.write(scaleSpring$.valve(stateIsActiveStream), | |
to: directlyManipulable.scalable.property) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment