Skip to content

Instantly share code, notes, and snippets.

@smrfeld
Created April 12, 2022 21:54
Show Gist options
  • Save smrfeld/463e38b5d230a1043cda0987b34de3cb to your computer and use it in GitHub Desktop.
Save smrfeld/463e38b5d230a1043cda0987b34de3cb to your computer and use it in GitHub Desktop.
Circle animation general
struct CircleAnimation: AnimatableModifier {
var angle: Float
var radius: Float
var animatableData: Float {
get { angle }
set { angle = newValue }
}
func body(content: Content) -> some View {
let x = CGFloat(400 + radius * cos(angle))
let y = CGFloat(400 - radius * sin(angle))
content.position(x: x, y: y)
}
}
struct ContentView: View {
@State var angle: Float = Float.pi / 2.0
let radius: Float = 200
var body: some View {
ZStack {
Circle()
.strokeBorder(.black, lineWidth: 2)
.foregroundColor(.clear)
.frame(width: CGFloat(2*radius), height: CGFloat(2*radius))
.position(x: 400, y: 400)
ZStack {
Circle()
.frame(width: 50, height: 50)
.foregroundColor(.blue)
Text("Hello!")
.offset(x: 80)
.font(.system(size: 24))
}
.modifier(CircleAnimation(angle: angle, radius: radius))
}
.frame(width: 800, height: 800)
.background(.white)
.onAppear {
withAnimation(.easeInOut(duration: 2.0)) { // withAnimation tells that states modified in closure are animated
angle = -Float.pi / 2.0
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment