I have two views that both react to .onHover overlapping in a ZStack. Currently the effect is triggered in the blue view even if it is overlapped by the red view (see video). How can I make sure that only the view at the top of the ZStack will react to .onHover, so that the blue view will not change when the overlapping part of the red view is hovered?
Code:
struct ContentView: View {
var body: some View {
ZStack {
RectangleView(color: .blue)
.frame(width: 300, height: 300)
RectangleView(color: .red)
.frame(width: 150, height: 150)
.offset(x: -150)
}
.frame(width: 600, height: 400)
}
}
struct RectangleView: View {
@State var hover = false
var color: Color
var body: some View {
Rectangle()
.foregroundColor(color)
.overlay(hover ? Color.white.opacity(0.3) : Color.clear)
.onHover { isHovering in
if isHovering {
hover = true
} else {
hover = false
}
}
}
}

