rotating dial with finger movements not dragging smoothly - SwiftUI

157 Views Asked by At

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

0

There are 0 best solutions below