Skip to content

Instantly share code, notes, and snippets.

@jverkoey
Created December 8, 2016 23:26
Show Gist options
  • Save jverkoey/8944724a92c35ed1d9fc2eefbab981b4 to your computer and use it in GitHub Desktop.
Save jverkoey/8944724a92c35ed1d9fc2eefbab981b4 to your computer and use it in GitHub Desktop.
/*
Copyright 2016-present The Material Motion Authors. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
import UIKit
import pop
let globalObject = NSObject()
func spring<P: Readable>(to: CGFloat, from: P) -> Spring<CGFloat> where P.T == CGFloat {
return Spring<CGFloat>(OP(#function, args: [from]), to: to, initialVelocity: 0) { observer in
let animation = POPSpringAnimation()
animation.springSpeed = 6
animation.springBounciness = 12
let popProperty = POPMutableAnimatableProperty()
popProperty.threshold = 1
popProperty.readBlock = { _, toWrite in
let value = from.read()
toWrite![0] = value
}
popProperty.writeBlock = { _, toRead in
observer.next(toRead![0])
}
animation.property = popProperty
return animation
}
}
func spring<P: Readable>(to: CGPoint, from: P) -> Spring<CGPoint> where P.T == CGPoint {
return Spring<CGPoint>(OP(#function, args: [from]), to: to, initialVelocity: .zero) { observer in
let animation = POPSpringAnimation()
animation.springSpeed = 6
animation.springBounciness = 12
let popProperty = POPMutableAnimatableProperty()
popProperty.threshold = 1
popProperty.readBlock = { _, toWrite in
let value = from.read()
toWrite![0] = value.x
toWrite![1] = value.y
}
popProperty.writeBlock = { _, toRead in
observer.next(CGPoint(x: toRead![0], y: toRead![1]))
}
animation.property = popProperty
return animation
}
}
class Spring<T: CustomDebugStringConvertible>: MotionObservable<T> {
public var initialVelocity: Property<T> {
return Property(#function, object: self, read: _initialVelocity.read, write: _initialVelocity.write)
}
public var destination: Property<T> {
return Property(#function, object: self, read: _destination.read, write: _destination.write)
}
private let _initialVelocity: Property<T>
private let _destination: Property<T>
public init(_ op: OP, to: T, initialVelocity: T, animation builder: @escaping (AnyMotionObserver<T>) -> POPSpringAnimation) {
var initialVelocity = initialVelocity
_initialVelocity = Property(read: { return initialVelocity }, write: { initialVelocity = $0 })
let destinationObservers = NSMutableOrderedSet()
var destination = to
let destinationProp = Property(read: { return destination }, write: { destination = $0 })
_destination = destinationProp
var args = op.args!
args.insert(_destination, at: 0)
args.append(_initialVelocity)
let op = OP(op.name, args: args, parent: op.parent)
super.init(op, subscriber: { observer in
let animation = builder(observer)
animation.toValue = destination
animation.velocity = initialVelocity
animation.removedOnCompletion = false
observer.state(.active)
animation.animationDidStartBlock = { anim in
observer.state(.active)
}
animation.completionBlock = { anim, finished in
observer.state(.atRest)
}
let subscription = destinationProp.subscribe { value in
animation.toValue = value
animation.isPaused = false
observer.state(.active)
}
let key = NSUUID().uuidString
globalObject.pop_add(animation, forKey: key)
return {
globalObject.pop_removeAnimation(forKey: key)
subscription.unsubscribe()
}
})
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment