How to make simple HUD in SwiftUI

Issue #723

Use @ViewBuilder to build dynamic content for our HUD. For blur effect, here I use NSVisualEffectView, but we can use .blur modifier also

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
struct HUD<Content>: View where Content: View {
let content: () -> Content

init(@ViewBuilder content: @escaping () -> Content) {
self.content = content
}

var body: some View {
content()
.frame(width: 80, height: 80)
.background(
VisualEffectView(material: .hudWindow)
.clipShape(RoundedRectangle(cornerRadius: 12))
.shadow(color: Color.black.opacity(0.22), radius: 12, x: 0, y: 5)
)
}
}

Then we can make some wrappers for information and progress HUD

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
struct HUD<Content>: View where Content: View {
let content: () -> Content

init(@ViewBuilder content: @escaping () -> Content) {
self.content = content
}

var body: some View {
content()
.frame(width: 80, height: 80)
.background(
VisualEffectView()
)
.cornerRadius(10)
.shadow(color: Color.gray.opacity(0.3), radius: 1, x: 0, y: 1)
}
}

Updated at 2020-12-26 20:59:10

Comments