Skip to content

Instantly share code, notes, and snippets.

@CyrilNb
Created December 4, 2023 01:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save CyrilNb/3c068eb3e1a74472ce24d60198ce1aba to your computer and use it in GitHub Desktop.
Save CyrilNb/3c068eb3e1a74472ce24d60198ce1aba to your computer and use it in GitHub Desktop.
iOS SwftUI gestures
// ContentView.swift
import SwiftUI
struct ContentView: View {
@State var dragAmount: CGSize = .zero
@State var currentMagnificationAmount = 0.0
@State var finalMagnificationAmount = 1.0
@State var currentRotationAngle: Angle = .zero
@State var finalRotationAngle: Angle = .zero
var body: some View {
List {
Text("Tap Gesture")
.onTapGesture {
print("Tap Gesture Detected")
}
Text("Double Tap")
.onTapGesture(count: 2) {
print("Double Tap Detected")
}
Text("Triple Tap")
.onTapGesture(count: 3) {
print("Triple Tap Detected")
}
Text("Long Press")
.onLongPressGesture {
print("Long Press Detected")
}
Text("Long Press (5 second wait)")
.onLongPressGesture(minimumDuration: 5) {
print("5 second Long Press Detected")
}
Text("Drag Gesture")
.offset(dragAmount)
.gesture(
DragGesture()
.onChanged { drag in
dragAmount = drag.translation
}
.onEnded { _ in
withAnimation {
dragAmount = .zero
}
}
)
Text("Magnification Gesture")
.scaleEffect(currentMagnificationAmount + finalMagnificationAmount)
.gesture(
// Note: This is not a MagnifyGesture()
MagnificationGesture()
.onChanged { amount in
currentMagnificationAmount = amount - 1
}
.onEnded { amount in
finalMagnificationAmount += currentMagnificationAmount
currentMagnificationAmount = 0
}
)
Text("Rotation Gesture")
.rotationEffect(currentRotationAngle + finalRotationAngle)
.gesture(
// Note: this is not a RotateGesture()
RotationGesture()
.onChanged { angle in
currentRotationAngle = angle
}
.onEnded { angle in
finalRotationAngle += currentRotationAngle
currentMagnificationAmount = .zero
}
)
}
}
}
#Preview {
ContentView()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment