Skip to content

Instantly share code, notes, and snippets.

@ershovio
Created November 23, 2019 16:52
Show Gist options
  • Save ershovio/69710037f43ab6471eb9d414396ded17 to your computer and use it in GitHub Desktop.
Save ershovio/69710037f43ab6471eb9d414396ded17 to your computer and use it in GitHub Desktop.
LongPressGesture exaxmple
struct LongPressGestureExample: View {
@State var rectangleColor = Color(.green)
var body: some View {
// LongPressGesture creation
// Gesture will be handled only if if takes at least 2 seconds
let longPressGesture = LongPressGesture(minimumDuration: 2, maximumDistance: 10)
.onEnded { _ in
if self.rectangleColor == .red {
self.rectangleColor = .green
} else {
self.rectangleColor = .red
}
}
return Rectangle()
// Change color
.foregroundColor(rectangleColor)
.cornerRadius(40)
.frame(width: 200, height: 100, alignment: .center)
// Add the longPressGesture to this view
.gesture(longPressGesture)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment