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

Commit

Permalink
Disable implicit animations when adding explicit animations in the an…
Browse files Browse the repository at this point in the history
…imator (#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.
  • Loading branch information
jverkoey authored Nov 2, 2017
1 parent ee19fda commit 8be151c
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/MDMMotionAnimator.m
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,10 @@ - (void)animateWithTiming:(MDMMotionTiming)timing
}
}

[CATransaction begin];
[CATransaction setDisableActions:YES];
[layer setValue:[values lastObject] forKeyPath:keyPath];
[CATransaction commit];
}

- (void)addCoreAnimationTracer:(void (^)(CALayer *, CAAnimation *))tracer {
Expand Down
22 changes: 22 additions & 0 deletions tests/unit/MotionAnimatorTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -47,5 +47,27 @@ class MotionAnimatorTests: XCTestCase {
XCTAssertTrue(true)
}

func testAnimatorOnlyAddsAnimationsForKeyPath() {
let animator = MotionAnimator()
let timing = MotionTiming(delay: 0,
duration: 1,
curve: .init(type: .bezier, data: (0, 0, 0, 0)),
repetition: .init(type: .none, amount: 0, autoreverses: false))

let window = UIWindow()
window.makeKeyAndVisible()
let view = UIView() // Need to animate a view's layer to get implicit animations.
window.addSubview(view)

XCTAssertEqual(view.layer.delegate as? UIView, view)

UIView.animate(withDuration: 0.5) {
animator.animate(with: timing, to: view.layer, withValues: [0, 1], keyPath: .rotation)

// Setting transform.rotation.z will create an implicit transform if implicit animations
// aren't disabled by the animator properly, so verify that such an animation doesn't exist:
XCTAssertNil(view.layer.animation(forKey: "transform"))
}
}
}

0 comments on commit 8be151c

Please sign in to comment.