Skip to content

Commit 0ed5914

Browse files
committed
Add ElasticEaseInEaseOutAnimationExample
1 parent af1e9f3 commit 0ed5914

File tree

3 files changed

+54
-2
lines changed

3 files changed

+54
-2
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-
ValueActionModifierExample()
69+
ElasticEaseInEaseOutView()
7070
}
7171
}

Example/SharedExample/Animation/ColorAnimationExample.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import OpenSwiftUI
77
#else
88
import SwiftUI
99
#endif
10-
import Foundation
1110

1211
struct ColorAnimationExample: View {
1312
@State private var showRed = false
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
//
2+
// ElasticEaseInEaseOutAnimationExample.swift
3+
// SharedExample
4+
5+
#if OPENSWIFTUI
6+
import OpenSwiftUI
7+
#else
8+
import SwiftUI
9+
#endif
10+
import Foundation
11+
12+
private struct ElasticEaseInEaseOutAnimation: CustomAnimation {
13+
let duration: TimeInterval
14+
15+
func animate<V>(value: V, time: TimeInterval, context: inout AnimationContext<V>) -> V? where V: VectorArithmetic {
16+
if time > duration { return nil } // The animation has finished.
17+
18+
let p = time / duration
19+
let s = sin((20 * p - 11.125) * ((2 * Double.pi) / 4.5))
20+
if p < 0.5 {
21+
return value.scaled(by: -(pow(2, 20 * p - 10) * s) / 2)
22+
} else {
23+
return value.scaled(by: (pow(2, -20 * p + 10) * s) / 2 + 1)
24+
}
25+
}
26+
}
27+
28+
extension Animation {
29+
static var elasticEaseInEaseOut: Animation { elasticEaseInEaseOut(duration: 0.35) }
30+
31+
static func elasticEaseInEaseOut(duration: TimeInterval) -> Animation {
32+
Animation(ElasticEaseInEaseOutAnimation(duration: duration))
33+
}
34+
}
35+
36+
struct ElasticEaseInEaseOutView: View {
37+
@State private var isActive = false
38+
39+
var body: some View {
40+
VStack(alignment: isActive ? .trailing : .leading) {
41+
Color.red
42+
.frame(width: 100.0, height: 100.0)
43+
.onAppear {
44+
withAnimation(.elasticEaseInEaseOut(duration: 5.0)) {
45+
isActive.toggle()
46+
}
47+
}
48+
Color.blue
49+
.frame(maxWidth: .infinity)
50+
}
51+
.padding()
52+
}
53+
}

0 commit comments

Comments
 (0)