RotationGesture

Rotation Gesture is applicable to any view

Usage

@State var rotationAngle: Angle = .degrees(0)
@State var currentAngle: Angle = .degrees(0)

var body: some View {
        
    Rectangle()
        .rotation(currentAngle + rotationAngle)
        .foregroundColor(.orange)
        .frame(height: 50)
        .padding()
        .gesture(
            RotationGesture()
                .onChanged { angle in
                    self.rotationAngle = angle
                }
                .onEnded { angle in
                    // To keep the latest state.
                    self.rotationAngle += self.currentAngle
                    self.currentAngle = .degrees(0)
                }
        )
     
}

Result for sample code is below.

Last updated