IOS_Boss/IOS_study/Library/HUDView.swift
2025-07-27 12:33:06 +08:00

46 lines
1.4 KiB
Swift
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//
// HUDView.swift
//
//
// Created by CC-star on 2025/7/15.
//
import SwiftUI
import Combine
//
extension View {
func hud<Content: View> (isPresented: Binding<Bool>, @ViewBuilder content: () -> Content) -> some View {
ZStack {
self
if isPresented.wrappedValue {
HUDView(content: content).zIndex(1)
}
}.animation(.default, value: isPresented.wrappedValue)
}
}
struct HUDView<Content: View>: View {
@ViewBuilder let content: Content
var body: some View {
content.font(.subheadline).foregroundStyle(Color.aTCR)
.padding(.horizontal, 4).padding(12)
.background(RoundedRectangle(cornerRadius: 8).fill(Color.aBGR))
}
}
@Observable final class HUD {
var isPresented = false
private(set) var title: String = ""
private var dismissTimer: AnyCancellable? // --
func show(_ title: String) {
self.title = title
isPresented = true
dismissTimer?.cancel()// -
// 3-
dismissTimer = Timer.publish(every: 3, on: .main, in: .common)
.autoconnect()
.sink { [weak self] _ in
self?.isPresented = false
self?.dismissTimer?.cancel() //
}
}
}