Skip to content

Instantly share code, notes, and snippets.

@ershovio
Created November 23, 2019 16:42
Show Gist options
  • Save ershovio/3e7ee3c176145c2c94d2d7bc0f7ae6d3 to your computer and use it in GitHub Desktop.
Save ershovio/3e7ee3c176145c2c94d2d7bc0f7ae6d3 to your computer and use it in GitHub Desktop.
TapGesture Example
struct TapGestureExample: View {
@State var rectangleColor = Color(.green)
var body: some View {
// TapGesture creation
let tapGesture = TapGesture()
// Change color when tap ended
.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 tapGesture to this view
.gesture(tapGesture)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment