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

Commit 8be151c

Browse files
authored
Disable implicit animations when adding explicit animations in the animator (#20)
* Disable implicit animations when adding explicit animations in the animator. A client using the animator expects that it will only add animations for the key path that was specified, but it's possible that setting the final value for a given key path can create implicit animations. We now disable implicit animations when setting the final value to the layer. * Remove unnecessary state.
1 parent ee19fda commit 8be151c

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

src/MDMMotionAnimator.m

+3
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,10 @@ - (void)animateWithTiming:(MDMMotionTiming)timing
112112
}
113113
}
114114

115+
[CATransaction begin];
116+
[CATransaction setDisableActions:YES];
115117
[layer setValue:[values lastObject] forKeyPath:keyPath];
118+
[CATransaction commit];
116119
}
117120

118121
- (void)addCoreAnimationTracer:(void (^)(CALayer *, CAAnimation *))tracer {

tests/unit/MotionAnimatorTests.swift

+22
Original file line numberDiff line numberDiff line change
@@ -47,5 +47,27 @@ class MotionAnimatorTests: XCTestCase {
4747
XCTAssertTrue(true)
4848
}
4949

50+
func testAnimatorOnlyAddsAnimationsForKeyPath() {
51+
let animator = MotionAnimator()
52+
let timing = MotionTiming(delay: 0,
53+
duration: 1,
54+
curve: .init(type: .bezier, data: (0, 0, 0, 0)),
55+
repetition: .init(type: .none, amount: 0, autoreverses: false))
56+
57+
let window = UIWindow()
58+
window.makeKeyAndVisible()
59+
let view = UIView() // Need to animate a view's layer to get implicit animations.
60+
window.addSubview(view)
61+
62+
XCTAssertEqual(view.layer.delegate as? UIView, view)
63+
64+
UIView.animate(withDuration: 0.5) {
65+
animator.animate(with: timing, to: view.layer, withValues: [0, 1], keyPath: .rotation)
66+
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"))
70+
}
71+
}
5072
}
5173

0 commit comments

Comments
 (0)