Skip to content
This repository was archived by the owner on Aug 30, 2023. It is now read-only.

Commit bb42e61

Browse files
authored
[breaking] Use the keyPath as the key when the animator is not additive. (#22)
* [breaking] Use the keyPath as the key when the animator is not additive. When the animator is not additive we don't actually want our animations to stack on top of one another. There are some scenarios where stacking absolute animations is desired, e.g. creating a "keyframe" animation composed of a variety of beginTime-adjusted animations, but in practice this is the exception rather than the norm. As such, the API is being adjusted to use the keyPath as the animation key when the animator is not additive. * More docs. * Method name.
1 parent 8be151c commit bb42e61

File tree

3 files changed

+14
-5
lines changed

3 files changed

+14
-5
lines changed

src/MDMMotionAnimator.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,10 @@ NS_SWIFT_NAME(MotionAnimator)
6666
/**
6767
Adds a single animation to the layer with the given timing structure.
6868
69+
If `additive` is disabled, the animation will be added to the layer with the keyPath as its key.
70+
In this case, multiple invocations of this function on the same key path will remove the
71+
animations added from prior invocations.
72+
6973
@param timing The timing to be used for the animation.
7074
@param layer The layer to be animated.
7175
@param values The values to be used in the animation. Must contain exactly two values. Supported
@@ -81,6 +85,10 @@ NS_SWIFT_NAME(MotionAnimator)
8185
/**
8286
Adds a single animation to the layer with the given timing structure.
8387
88+
If `additive` is disabled, the animation will be added to the layer with the keyPath as its key.
89+
In this case, multiple invocations of this function on the same key path will remove the
90+
animations added from prior invocations.
91+
8492
@param timing The timing to be used for the animation.
8593
@param layer The layer to be animated.
8694
@param values The values to be used in the animation. Must contain exactly two values. Supported

src/MDMMotionAnimator.m

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,8 @@ - (void)animateWithTiming:(MDMMotionTiming)timing
100100

101101
// When we use a nil key, Core Animation will ensure that the animation is added with a
102102
// unique key - this enables our additive animations to stack upon one another.
103-
[layer addAnimation:animation forKey:nil];
103+
NSString *key = _additive ? nil : keyPath;
104+
[layer addAnimation:animation forKey:key];
104105

105106
for (void (^tracer)(CALayer *, CAAnimation *) in _tracers) {
106107
tracer(layer, animation);

tests/unit/MotionAnimatorTests.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,10 @@ class MotionAnimatorTests: XCTestCase {
4747
XCTAssertTrue(true)
4848
}
4949

50-
func testAnimatorOnlyAddsAnimationsForKeyPath() {
50+
func testAnimatorOnlyUsesSingleNonAdditiveAnimationForKeyPath() {
5151
let animator = MotionAnimator()
52+
animator.additive = false
53+
5254
let timing = MotionTiming(delay: 0,
5355
duration: 1,
5456
curve: .init(type: .bezier, data: (0, 0, 0, 0)),
@@ -64,9 +66,7 @@ class MotionAnimatorTests: XCTestCase {
6466
UIView.animate(withDuration: 0.5) {
6567
animator.animate(with: timing, to: view.layer, withValues: [0, 1], keyPath: .rotation)
6668

67-
// Setting transform.rotation.z will create an implicit transform if implicit animations
68-
// aren't disabled by the animator properly, so verify that such an animation doesn't exist:
69-
XCTAssertNil(view.layer.animation(forKey: "transform"))
69+
XCTAssertEqual(view.layer.animationKeys()?.count, 1)
7070
}
7171
}
7272
}

0 commit comments

Comments
 (0)