Created
April 12, 2022 21:38
-
-
Save smrfeld/0720ff029024351fbd4cd4071daf7fc1 to your computer and use it in GitHub Desktop.
Circle animation fixed
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 CircleWithAnimatableAngle: Shape { | |
var angle: Float | |
var radius: Float | |
var animatableData: Float { | |
get { angle } | |
set { angle = newValue } | |
} | |
func path(in rect: CGRect) -> Path { | |
return Path { path in | |
let x = CGFloat(400 + radius * cos(angle)) | |
let y = CGFloat(400 - radius * sin(angle)) | |
path.move(to: CGPoint(x: x, y: y)) | |
path.addEllipse(in: CGRect(x: x - 25.0, y: y - 25.0, width: 50.0, height: 50.0)) | |
} | |
} | |
} | |
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) | |
CircleWithAnimatableAngle(angle: angle, radius: radius) | |
.foregroundColor(.blue) | |
} | |
.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