I'm trying to "select" and "deselect" a view in SwiftUI.
What I thought I could do is, when the user taps on a Text that contains a certain string (in my case it is an emoji) the Text should be surrounded by a rectangle. If the view is being deselected, then the rectangle would disappear.
I have tried the following but it does not work, as nothing changes in the view:
Text(emoji.text)
.onDrag { NSItemProvider(object: emoji.text as NSString) }
.modifier(SelectionSquare(select: selectedEmojis.toggleMatching(element: emoji) ? true: false))
// Getting a warning in the line above: Modifying state during view update, this will cause undefined behavior.
struct SelectionSquare: ViewModifier {
var select: Bool
func body(content: Content) -> some View {
content
.onTapGesture {
content.overlay(Rectangle().border(Color.red, width: 2.0))
// Getting a warning here: Result of call to 'overlay(_:alignment:)' is unused
}
}
}
Why doesn't this work? Is there any other way to do this?
Thanks in advance!
