Created
November 23, 2019 16:52
-
-
Save a-v-ershov/69710037f43ab6471eb9d414396ded17 to your computer and use it in GitHub Desktop.
LongPressGesture exaxmple
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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