Skip to content

Commit

Permalink
Fix issue where manually interpolated keyframes could animate incorre…
Browse files Browse the repository at this point in the history
…ctly (airbnb#2285)
  • Loading branch information
calda committed Jan 11, 2024
1 parent 2562590 commit 5515927
Show file tree
Hide file tree
Showing 66 changed files with 37 additions and 2 deletions.
13 changes: 13 additions & 0 deletions Example/Example/ExampleApp.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,24 @@ import SwiftUI

@main
struct ExampleApp: App {

// MARK: Lifecycle

init() {
// Register fonts from the Samples/Fonts directory
for fontAssetURL in Bundle.main.urls(forResourcesWithExtension: "ttf", subdirectory: "Samples/Fonts") ?? [] {
CTFontManagerRegisterFontsForURL(fontAssetURL as CFURL, .process, nil)
}
}

// MARK: Internal

var body: some Scene {
WindowGroup {
NavigationStack {
AnimationListView(content: .directory("Samples"))
}
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,11 @@ enum Keyframes {

let minimumTime = times.min() ?? 0
let maximumTime = times.max() ?? 0
let animationLocalTimeRange = Int(minimumTime)...Int(maximumTime)

// We disable Core Animation interpolation when using manually interpolated keyframes,
// so we don't animate between these values. To prevent the animation from being choppy
// even at low playback speed, we have to interpolate at a very high fidelity.
let animationLocalTimeRange = stride(from: minimumTime, to: maximumTime, by: 0.1)

let interpolatedKeyframes = try animationLocalTimeRange.compactMap { localTime -> Keyframe<CombinedResult>? in
let interpolatedValues = untypedInterpolators.map { interpolator in
Expand All @@ -272,7 +276,10 @@ enum Keyframes {

return Keyframe(
value: combinedResult,
time: AnimationFrameTime(localTime))
time: AnimationFrameTime(localTime),
// Since we already manually interpolated the keyframes, have Core Animation display
// each value as a static keyframe rather than trying to interpolate between them.
isHold: true)
}

return KeyframeGroup(keyframes: ContiguousArray(interpolatedKeyframes))
Expand Down
Binary file added Tests/Samples/Fonts/Pacifico-Regular.ttf
Binary file not shown.
1 change: 1 addition & 0 deletions Tests/Samples/Issues/issue_1939.json

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions Tests/Samples/Issues/issue_2226.json

Large diffs are not rendered by default.

6 changes: 6 additions & 0 deletions Tests/SnapshotConfiguration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,12 @@ extension SnapshotConfiguration {
10, // Should show frame 10
]),

"Issues/issue_2226": SnapshotConfiguration.default
.framesToSnapshot([
19.25,
113,
]),

// Test cases for `AnimationFontProvider`
"Nonanimating/Text_Glyph": .customFontProvider(HardcodedFontProvider(font: UIFont(name: "Chalkduster", size: 36)!)),

Expand Down
5 changes: 5 additions & 0 deletions Tests/SnapshotTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,11 @@ class SnapshotTests: XCTestCase {
}

override func setUp() {
// Register fonts from the Samples/Fonts directory
for fontAssetURL in Bundle.lottie.urls(forResourcesWithExtension: "ttf", subdirectory: "Samples/Fonts") ?? [] {
CTFontManagerRegisterFontsForURL(fontAssetURL as CFURL, .process, nil)
}

LottieLogger.shared = .printToConsole
TestHelpers.snapshotTestsAreRunning = true
isRecording = false // Change it here to `true` if you want to generate the snapshots
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Supports Core Animation engine
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Supports Core Animation engine
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 5515927

Please sign in to comment.