Created
April 12, 2022 21:54
-
-
Save smrfeld/463e38b5d230a1043cda0987b34de3cb to your computer and use it in GitHub Desktop.
Circle animation general
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
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