I have added mask in a overlay to create a spotlight effect in SwiftUI.
But at the same time I want the user to interact with the view through that window opening that I have such as sliding a slider or tapping a button while totally block other interactions.
The problem I’m facing is that I’m not able to make interaction through this mask window because it’s an overlay. Is there a way I could enable interaction only in the white highlighted area ?
This is how it looks
This is my code to achieve the overlay with highlighted window(spotlight)
ZStack {
VStack {
Slider(
value: $speed,
in: 0...100,
onEditingChanged: { editing in
isEditing = editing
}
)
Text("(speed)")
.foregroundColor(isEditing ? .red : .blue)
}
.padding(.top, 100)
Rectangle()
.fill(Color.black.opacity(0.5))
.mask {
Rectangle()
.overlay(alignment: .topLeading) {
let rect = CGRect(x: 0, y: 500, width:390, height: 80)
RoundedRectangle(cornerRadius: 10, style: .continuous)
.frame(width: rect.width, height: rect.height)
.offset(y:90)
.blendMode(.destinationOut)
}
}
}
2
Answers
So I finally found a solution. Instead of doing mask. What I did is use path and make that clipped so that user can interact through clear pixels. Thanks to this answer here.
Maybe you can use a combination of the .disabled and .zIndex instead of a mask.
Just a first thought, but it should work.