Skip to content

Instantly share code, notes, and snippets.

@jverkoey
Created December 8, 2016 23:27
Show Gist options
  • Save jverkoey/f99b296846ad82ba5adacf181e23a990 to your computer and use it in GitHub Desktop.
Save jverkoey/f99b296846ad82ba5adacf181e23a990 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 Foundation
public class TweenAnimation<A: CAAnimation, T>: CustomDebugStringConvertible {
init(_ animation: A) {
self.animation = animation
}
let animation: A
var layer: CALayer?
var key = NSUUID().uuidString
public var debugDescription: String {
return "\(animation.debugDescription) '\(key)'"
}
}
public typealias TweenStream<A: CAAnimation, T> = MotionObservable<TweenAnimation<A, T>>
public func tween<T>(duration: TimeInterval, values: [T]) -> TweenStream<CAKeyframeAnimation, T> {
return TweenStream(OP(#function, args: [duration, values]), subscriber: { observer in
CATransaction.begin()
CATransaction.setCompletionBlock({
observer.state(.atRest)
})
observer.state(.active)
let animation = CAKeyframeAnimation()
animation.duration = duration
animation.values = values
let tween = TweenAnimation<CAKeyframeAnimation, T>(animation)
observer.next(tween)
CATransaction.commit()
return {
if let layer = tween.layer {
layer.removeAnimation(forKey: tween.key)
}
}
})
}
extension CALayerPropertyBuilder {
func opacity<A: CAPropertyAnimation>(_ key: String? = nil) -> Property<TweenAnimation<A, CGFloat>> {
let layer = self.layer
return Property(#function, object: layer, read: {
guard let key = key else {
assertionFailure("Unable to read nameless animations from CALayer.")
return TweenAnimation(A())
}
return TweenAnimation<A, CGFloat>(layer.animation(forKey: key) as! A)
}, write: { tween in
tween.animation.keyPath = "opacity"
if let key = key {
tween.key = key
}
tween.layer = layer
layer.add(tween.animation, forKey: tween.key)
})
}
func backgroundColor<A: CAKeyframeAnimation>(_ key: String? = nil) -> Property<TweenAnimation<A, UIColor>> {
let layer = self.layer
return Property(#function, object: layer, read: {
guard let key = key else {
assertionFailure("Unable to read nameless animations from CALayer.")
return TweenAnimation(A())
}
return TweenAnimation<A, UIColor>(layer.animation(forKey: key) as! A)
}, write: { tween in
tween.animation.keyPath = "backgroundColor"
tween.animation.values = (tween.animation.values as! [UIColor]).map { $0.cgColor }
if let key = key {
tween.key = key
}
tween.layer = layer
layer.add(tween.animation, forKey: tween.key)
})
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment