Created
November 14, 2019 10:22
-
-
Save mikekatz/889e7521092fb701ee2b2242ecf4c539 to your computer and use it in GitHub Desktop.
Get the values in order to plot animation curves for any SwiftUI animation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// created Michael Katz November 2019 | |
// MIT licensed | |
import SwiftUI | |
struct ContentView: View { | |
@State var start: Bool = false | |
var animationToExplore: Animation { | |
// replace this animation with the animation you want to graph | |
return Animation.spring().speed(1) | |
} | |
var body: some View { | |
VStack { | |
Button(action: { | |
withAnimation(self.animationToExplore) { | |
self.start.toggle() | |
} | |
}) { | |
Text("Tap Here") | |
} | |
AnimatedView(start: start) | |
.frame(width: 30, height: 30) | |
.offset(x: start ? 0 : 100, y: 0) // do this just to see something so you know it's working | |
.animation(animationToExplore) | |
} | |
} | |
} | |
struct ContentView_Previews: PreviewProvider { | |
static var previews: some View { | |
ContentView() | |
} | |
} | |
struct AnimatedView: Shape { | |
var start: Bool | |
private var animatedValue: Double | |
var animatableData: Double { | |
get { return animatedValue } | |
set { animatedValue = newValue } | |
} | |
init(start: Bool) { | |
self.start = start | |
self.animatedValue = start ? 1 : 0 | |
} | |
func path(in rect: CGRect) -> Path { | |
print(animatedValue) // put the values in the console | |
return Path(ellipseIn: rect) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment