Skip to content

Commit 31734a5

Browse files
committed
Add keyFrames Effect in animations case study (#231)
1 parent ba785e8 commit 31734a5

File tree

1 file changed

+22
-9
lines changed

1 file changed

+22
-9
lines changed

Examples/CaseStudies/SwiftUICaseStudies/01-GettingStarted-Animations.swift

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,24 @@ private let readMe = """
1717
toggle at the bottom of the screen.
1818
"""
1919

20+
extension Effect where Error == Never {
21+
public static func keyFrames(
22+
values: [(output: Value, duration: TimeInterval)],
23+
scheduler: DateScheduler
24+
) -> Effect {
25+
.concatenate(
26+
values
27+
.enumerated()
28+
.map { index, animationState in
29+
index == 0
30+
? Effect(value: animationState.output)
31+
: Effect(value: animationState.output)
32+
.delay(values[index - 1].duration, on: scheduler)
33+
}
34+
)
35+
}
36+
}
37+
2038
struct AnimationsState: Equatable {
2139
var circleCenter = CGPoint.zero
2240
var circleColor = Color.white
@@ -43,15 +61,10 @@ let animationsReducer = Reducer<AnimationsState, AnimationsAction, AnimationsEnv
4361
return .none
4462

4563
case .rainbowButtonTapped:
46-
return .concatenate(
47-
[Color.red, .blue, .green, .orange, .pink, .purple, .yellow, .white]
48-
.enumerated()
49-
.map { index, color in
50-
index == 0
51-
? Effect(value: .setColor(color))
52-
: Effect(value: .setColor(color))
53-
.delay(1, on: environment.mainQueue)
54-
}
64+
return .keyFrames(
65+
values: [Color.red, .blue, .green, .orange, .pink, .purple, .yellow, .white]
66+
.map { (output: .setColor($0), duration: 1) },
67+
scheduler: environment.mainQueue
5568
)
5669

5770
case let .setColor(color):

0 commit comments

Comments
 (0)