I have a scroll view that contains elements, and I use a matched geometry effect to animate an element above the screen when it is clicked (kind of creating a new view on top). I have to change the IDS in the matched geometry modifier, and this solution details why: https://stackoverflow.com/a/76795258/18070009. But in doing so, I raise the warning below.
Does anyone know why this occurs and how I can fix it?
The warning:
Multiple inserted views in matched geometry group Pair<String, ID>(first: "B", second: SwiftUI.Namespace.ID(id: 3543)) have isSource: true, results are undefined.
The code:
struct NewsReplyView: View {
@Namespace var namespace
@State var text: String = ""
@State var selected: String = ""
@State var show = false
@State var loaded = false
@State var arr = ["1", "2", "3", "4", "5"]
var body: some View {
ZStack {
VStack{
ForEach(arr.indices) { i in
RoundedRectangle(cornerRadius: 20).frame(width: 100, height: 100)
.id(arr[i])
.onTapGesture {
withAnimation(.spring()){
show.toggle()
selected = arr[i]
DispatchQueue.main.asyncAfter(deadline: .now() + 0.3) {
loaded = true
}
}
}
//changing here causes warning
.matchedGeometryEffect(id: loaded ? "A" : selected, in: namespace)
}
}.opacity(show ? 0.0 : 1.0)
if show {
VStack{
RoundedRectangle(cornerRadius: 20).frame(width: 100, height: 100)
.id(selected)
//changing here causes warning
.matchedGeometryEffect(id: loaded ? "B" : selected, in: namespace)
Spacer()
if loaded {
HStack{
TextField("", text: $text)
.onSubmit {
UIApplication.shared.sendAction(#selector(UIResponder.resignFirstResponder), to: nil, from: nil, for: nil)
}
Button {
withAnimation(.spring()){
loaded = false
DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) {
withAnimation(.spring()){
show.toggle()
}
}
}
} label: {
Image(systemName: "xmark.circle.fill")
}
}
}
}
}
}
}
}