0

I'm creating rotating dial using SwiftUI with drag gesture so that user can rotate dial with finger movements smoothly but i don't know why it is not working smoothly when user start dragging second time. You can check gif and code below. Any suggestion or help would be appreciated.

struct LockerView: View {
    @State private var rotationAngle: Double = 0.0
 .......
var body: some View {
        
        ZStack {
            
            // background layer
            Color.theme.accent
                .ignoresSafeArea()
            
            // content layer
            VStack {
              .........
                ZStack {
                    MainCircle
                    
                    .......
                }
                ........
            }
        }
    }
}

Code for dragging dial:

private var CircleWithNumber: some View {
        let gesture = DragGesture()
            .onChanged { value in
                let vector = CGVector(dx: value.translation.width, dy: value.translation.height)
                let radians = atan2(vector.dy, vector.dx)
                let newAngle = radians * 180 / .pi
                rotationAngle = newAngle
            }
        return ZStack {
            InnerCircle
            Numbers
        }
        .rotationEffect(.degrees(rotationAngle))
        .gesture(gesture)
    }

Output:

enter image description here

Kishan Bhatiya
  • 2,175
  • 8
  • 14
  • Firstly, I think it looks very cool :) Based on your code, I see that you are trying to calculate the angle of rotation, presumably, around the center of the dial, however, you use translation for that and translation is calculated from the point of the initial touch. In other words, you, probably, should do calculations based on the center of the dial. – Baglan Apr 18 '23 at 10:44
  • Thanks for your quick reply let me try this but i think i need exact tick mark on which user end dragging after 1st drag. – Kishan Bhatiya Apr 18 '23 at 13:42

0 Answers0