Skip to content
This repository has been archived by the owner on Aug 13, 2021. It is now read-only.

Commit

Permalink
Provide the model value in the core animation channel.
Browse files Browse the repository at this point in the history
Summary:
Prior to this diff we were piggybacking off the next channel in order to set the desired model value on the layer. After this change, core animation animations will no longer send any values down the next channel. Instead, the desired model value is sent along with the core animation .add event.

This change keeps the next channel focused on providing real-time values, e.g. from POP or a gesture recognizer.

Reviewers: O2 Material Motion, #material_motion, O4 Material Apple platform reviewers, chuga

Reviewed By: O4 Material Apple platform reviewers, chuga

Tags: #material_motion

Differential Revision: http://codereview.cc/D2448
  • Loading branch information
jverkoey committed Jan 4, 2017
1 parent 4ac612d commit f16468a
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 9 deletions.
2 changes: 1 addition & 1 deletion src/MotionObservable.swift
Expand Up @@ -24,7 +24,7 @@ public typealias StateChannel = (MotionState) -> Void
/** A Core Animation channel event. */
public enum CoreAnimationChannelEvent {
/** The provided animation is expected to be added to a layer. */
case add(CAPropertyAnimation, String, initialVelocity: Any?)
case add(CAPropertyAnimation, String, modelValue: Any, initialVelocity: Any?)

/** Any animation with the given key is expected to be removed from a layer. */
case remove(String)
Expand Down
11 changes: 8 additions & 3 deletions src/operators/foundation/_map.swift
Expand Up @@ -26,12 +26,13 @@ extension ExtendableMotionObservable {
}, coreAnimation: { event, coreAnimation in
let transformedInitialVelocity: Any?
switch event {
case .add(let animation, let key, let initialVelocity):
case .add(let animation, let key, let modelValue, let initialVelocity):
if let initialVelocity = initialVelocity {
transformedInitialVelocity = transform(initialVelocity as! T)
} else {
transformedInitialVelocity = nil
}
let transformedModelValue = transform(modelValue as! T)

let copy = animation.copy() as! CAPropertyAnimation
switch copy {
Expand All @@ -45,11 +46,15 @@ extension ExtendableMotionObservable {
if let byValue = basicAnimation.byValue {
basicAnimation.byValue = transform(byValue as! T)
}
coreAnimation(.add(basicAnimation, key, initialVelocity: transformedInitialVelocity))
coreAnimation(.add(basicAnimation, key,
modelValue: transformedModelValue,
initialVelocity: transformedInitialVelocity))

case let keyframeAnimation as CAKeyframeAnimation:
keyframeAnimation.values = keyframeAnimation.values?.map { transform($0 as! T) }
coreAnimation(.add(keyframeAnimation, key, initialVelocity: transformedInitialVelocity))
coreAnimation(.add(keyframeAnimation, key,
modelValue: transformedModelValue,
initialVelocity: transformedInitialVelocity))

default:
assertionFailure("Unsupported animation type: \(type(of: animation))")
Expand Down
3 changes: 2 additions & 1 deletion src/properties/CALayer+Properties.swift
Expand Up @@ -53,12 +53,13 @@ public class CALayerReactivePropertyBuilder {
var lastAnimationKey: String?
return ReactiveProperty(read: read, write: write, coreAnimation: { event in
switch event {
case .add(let animation, let key, let initialVelocity):
case .add(let animation, let key, let modelValue, let initialVelocity):
if let initialVelocity = initialVelocity {
applyInitialVelocity(initialVelocity, to: animation)
}

animation.keyPath = keyPath
layer.setValue(modelValue, forKeyPath: keyPath)
layer.add(animation, forKey: key)

case .remove(let key):
Expand Down
5 changes: 3 additions & 2 deletions src/sources/coreAnimationSpringSource.swift
Expand Up @@ -48,11 +48,12 @@ public func coreAnimationSpringSource<T where T: Subtractable, T: Zeroable>(_ sp
CATransaction.setCompletionBlock {
observer.state(.atRest)
}
observer.next($0)

let key = NSUUID().uuidString
animationKeys.append(key)
observer.coreAnimation(.add(animation, key, initialVelocity: spring.initialVelocity.read()))
observer.coreAnimation(.add(animation, key,
modelValue: $0,
initialVelocity: spring.initialVelocity.read()))

CATransaction.commit()
}
Expand Down
5 changes: 3 additions & 2 deletions src/sources/coreAnimationTweenSource.swift
Expand Up @@ -45,9 +45,10 @@ public func coreAnimationTweenSource<T>(_ tween: Tween<T>) -> MotionObservable<T
observer.state(.atRest)
}

observer.next(values.last!)
let key = NSUUID().uuidString
observer.coreAnimation(.add(animation, key, initialVelocity: nil))
observer.coreAnimation(.add(animation, key,
modelValue: values.last!,
initialVelocity: nil))

CATransaction.commit()

Expand Down

0 comments on commit f16468a

Please sign in to comment.