Skip to content

Instantly share code, notes, and snippets.

@ayaysir
Created May 11, 2023 11:50
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 ayaysir/05e509370e3ffb91706f83391930489e to your computer and use it in GitHub Desktop.
Save ayaysir/05e509370e3ffb91706f83391930489e to your computer and use it in GitHub Desktop.
import SwiftUI
struct ContinuousLongPressView: View {
@GestureState private var isDetectingLongPress = false
var longPress: some Gesture {
LongPressGesture(minimumDuration: 1)
.updating($isDetectingLongPress) { value, gestureState, _ in
gestureState = value
}.onEnded { value in
print("LongPress OnEnded")
}
}
@GestureState private var isDetectingContinuousPress = false
var continuousPress: some Gesture {
LongPressGesture(minimumDuration: 0.1)
.sequenced(before: DragGesture(minimumDistance: 0, coordinateSpace: .local))
.updating($isDetectingContinuousPress) { value, gestureState, _ in
switch value {
case .second(true, nil):
gestureState = true
print("updating: Second")
default:
break
}
}.onEnded { value in
switch value {
case .second(_, _):
print("onended: Second")
default:
break
}
}
}
var body: some View {
VStack {
Image(systemName: isDetectingLongPress ? "globe" : "pause.fill")
Button {} label: {
Text("Long Press Button")
}.simultaneousGesture(longPress)
Divider()
Image(systemName: isDetectingContinuousPress ? "globe" : "pause.fill")
Button {} label: {
Text("Continuous Press Button")
}.simultaneousGesture(continuousPress)
}
// Bool 값이 변경되었을 때의 처리는 onChange에서
// .onChange(of: isDetectingLongPress) { newValue in
// print("onChange", newValue)
// }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment