Skip to content

Commit 42719ba

Browse files
authored
Fix BezierAnimation fraction issue (#463)
1 parent a6d645e commit 42719ba

File tree

5 files changed

+37
-5
lines changed

5 files changed

+37
-5
lines changed

Example/HostingExample/ViewController.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,6 @@ class ViewController: NSViewController {
6666

6767
struct ContentView: View {
6868
var body: some View {
69-
ColorViewControllerRepresentableExample()
69+
ColorAnimationExample()
7070
}
7171
}

Example/SharedExample/Animation/ColorAnimationExample.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@ struct ColorAnimationExample: View {
1515
Color(platformColor: showRed ? .red : .blue)
1616
.frame(width: showRed ? 200 : 400, height: showRed ? 200 : 400)
1717
}
18-
.animation(.easeInOut(duration: 2), value: showRed)
18+
.animation(.easeInOut(duration: 5), value: showRed)
1919
.onAppear {
20-
DispatchQueue.main.asyncAfter(deadline: .now() + 2) {
20+
DispatchQueue.main.asyncAfter(deadline: .now() + 1) {
2121
showRed.toggle()
2222
}
2323
}

Sources/OpenSwiftUICore/Animation/Animatable/AnimatablePair.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,6 @@ extension AnimatablePair: Sendable where First: Sendable, Second: Sendable {}
7676

7777
extension AnimatablePair: CustomDebugStringConvertible {
7878
public var debugDescription: String {
79-
"\(first), \(second)"
79+
"(\(first), \(second))"
8080
}
8181
}

Sources/OpenSwiftUICore/Animation/Animation/BezierAnimation.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -489,7 +489,7 @@ package struct BezierAnimation: InternalCustomAnimation {
489489
guard duration > 0, duration >= elapsed else {
490490
return nil
491491
}
492-
return curve.value(at: elapsed.clamp(min: 0.0, max: 1.0))
492+
return curve.value(at: (elapsed / duration).clamp(min: 0.0, max: 1.0))
493493
}
494494

495495
package var function: Animation.Function {
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
//
2+
// BezierAnimationTests.swift
3+
// OpenSwiftUICoreTests
4+
5+
import OpenSwiftUICore
6+
import Testing
7+
import Numerics
8+
9+
// MARK: - BezierAnimationTests
10+
11+
struct BezierAnimationTests {
12+
@Test(
13+
.bug(
14+
"https://github.com/OpenSwiftUIProject/OpenSwiftUI/issues/459",
15+
id: "459",
16+
"BezierAnimation's fraction is behavior like a fixed 1 time duration animation"
17+
)
18+
)
19+
func fraction() throws {
20+
let animation = BezierAnimation(
21+
curve: .init(
22+
startControlPoint: .topLeading,
23+
endControlPoint: .bottomTrailing
24+
),
25+
duration: 100
26+
)
27+
let f1 = try #require(animation.fraction(for: 10.0))
28+
let f2 = try #require(animation.fraction(for: 50.0))
29+
#expect(f1.isApproximatelyEqual(to: 0.1, absoluteTolerance: 0.01))
30+
#expect(f2.isApproximatelyEqual(to: 0.5, absoluteTolerance: 0.01))
31+
}
32+
}

0 commit comments

Comments
 (0)