Swift & SwiftUI
  • İçerikler
  • UI Bileşenleri
    • Text
    • Button
    • Image
    • Picker
    • SecureField
    • Stepper
    • Slider
    • TabView
    • Sheet
    • Action Sheet
    • Alert
  • Layout
    • Fill-Space-Equally
  • State & Data Flow
    • Content
    • EnvironmentObject
    • ObservableObject
    • ObservedObject
    • Binding
  • Gestures
    • TapGesture
    • DragGesture
    • MagnificationGesture
    • RotationGesture
    • LongPressGesture
    • Notes
  • Extra
    • GeometryReader
    • Timer
    • AlignmentGuide
    • PreferenceKey
  • Concurrency
    • Perform asynchronous operation
Powered by GitBook
On this page

Was this helpful?

  1. Gestures

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.

PreviousMagnificationGestureNextLongPressGesture

Last updated 5 years ago

Was this helpful?

rotation-gesture-demo