Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import SwiftUI
- struct InteractiveArc: View {
- @State private var startAngle = Angle.degrees(0)
- @State private var endAngle = Angle.degrees(120)
- let center = CGPoint(x: 150, y: 150)
- let radius: CGFloat = 100
- var body: some View {
- ZStack {
- // Background Circle
- Circle()
- .stroke(style: StrokeStyle(lineWidth: 1, dash: [5]))
- .foregroundColor(.gray.opacity(0.7))
- .frame(width: radius * 2, height: radius * 2)
- .position(center)
- // Arc
- Path { path in
- path.addArc(center: center,
- radius: radius,
- startAngle: startAngle,
- endAngle: endAngle,
- clockwise: false)
- }
- .stroke(Color.blue, lineWidth: 6)
- // Start Handle
- Circle()
- .fill(Color.green)
- .frame(width: 20, height: 20)
- .position(position(for: startAngle))
- .gesture(
- DragGesture().onChanged { value in
- updateAngle(&startAngle, from: value.location)
- }
- )
- // End Handle
- Circle()
- .fill(Color.red)
- .frame(width: 20, height: 20)
- .position(position(for: endAngle))
- .gesture(
- DragGesture().onChanged { value in
- updateAngle(&endAngle, from: value.location)
- }
- )
- }
- .frame(width: 300, height: 300)
- }
- // Convert angle to CGPoint on the circle
- func position(for angle: Angle) -> CGPoint {
- let radians = angle.radians
- let x = center.x + cos(radians) * radius
- let y = center.y + sin(radians) * radius
- return CGPoint(x: x, y: y)
- }
- // Convert drag position to angle relative to center
- func updateAngle(_ angle: inout Angle, from location: CGPoint) {
- let dx = location.x - center.x
- let dy = location.y - center.y
- let radians = atan2(dy, dx)
- angle = Angle(radians: radians)
- }
- }
- #Preview {
- ArcsDemo()
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement