You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

SwiftUI能否实现macOS调整亮度/音量或Xcode构建时的系统HUD效果?

macOS SwiftUI 实现系统风格HUD方案

参考示意图

实现核心逻辑

系统音量/亮度调节、Xcode构建提示的HUD本质是无焦点、高优先级的磨砂悬浮窗口,按以下步骤实现即可匹配原生效果:


1. 自定义悬浮窗口基类

继承NSWindow做特殊配置,避免抢焦点、适配悬浮层级:

class HUDWindow: NSWindow {
    override init(contentRect: NSRect, styleMask style: NSWindow.StyleMask, backing backingStoreType: NSWindow.BackingStoreType, defer flag: Bool) {
        super.init(contentRect: contentRect, styleMask: .borderless, backing: backingStoreType, defer: flag)
        // 基础样式配置
        self.backgroundColor = .clear
        self.isOpaque = false
        self.hasShadow = true
        // 窗口层级和系统状态栏HUD同级,高于所有普通窗口
        self.level = .statusBar
        // 忽略交互事件,不抢占用户操作焦点
        self.ignoresMouseEvents = true
        self.isReleasedWhenClosed = false
    }
}

2. 实现匹配系统样式的SwiftUI视图

对齐原生HUD的磨砂效果、布局、圆角规格:

struct SystemHUDView: View {
    let icon: String // 传入SF Symbol名称
    let title: String
    
    var body: some View {
        VStack(spacing: 12) {
            Image(systemName: icon)
                .font(.system(size: 40, weight: .light))
            Text(title)
                .font(.system(size: 14, weight: .medium))
        }
        .foregroundColor(.secondary)
        .padding(.horizontal, 24)
        .padding(.vertical, 20)
        // 系统原生超薄磨砂材质
        .background(.ultraThinMaterial, in: RoundedRectangle(cornerRadius: 14, style: .continuous))
        .frame(width: 160, height: 160)
    }
}

3. 全局HUD管控逻辑

用单例统一管理显示、自动消失逻辑,避免重复弹出:

class HUDManager {
    static let shared = HUDManager()
    private var hudWindow: HUDWindow?
    private var hideTask: DispatchWorkItem?
    
    func show(icon: String, title: String, duration: TimeInterval = 2) {
        // 取消之前未执行的隐藏任务,避免冲突
        hideTask?.cancel()
        
        // 初始化窗口,默认在屏幕中下位置弹出,和系统HUD位置对齐
        let screenRect = NSScreen.main?.frame ?? .zero
        let windowRect = CGRect(x: (screenRect.width - 160)/2, y: (screenRect.height - 160)/2 - 100, width: 160, height: 160)
        let window = HUDWindow(contentRect: windowRect, styleMask: .borderless, backing: .buffered, defer: false)
        window.contentView = NSHostingView(rootView: SystemHUDView(icon: icon, title: title))
        window.orderFront(nil)
        self.hudWindow = window
        
        // 淡入动画,和系统动效时长对齐
        window.alphaValue = 0
        NSAnimationContext.runAnimationGroup { context in
            context.duration = 0.2
            window.animator().alphaValue = 1
        }
        
        // 定时隐藏逻辑
        let task = DispatchWorkItem { [weak self] in
            NSAnimationContext.runAnimationGroup { context in
                context.duration = 0.3
                window.animator().alphaValue = 0
            } completionHandler: {
                window.close()
                self?.hudWindow = nil
            }
        }
        hideTask = task
        DispatchQueue.main.asyncAfter(deadline: .now() + duration, execute: task)
    }
}

调用示例

需要弹出HUD时直接调用单例方法即可:

// 调节亮度HUD示例
HUDManager.shared.show(icon: "sun.max.fill", title: "亮度")
// Xcode构建成功HUD示例
HUDManager.shared.show(icon: "checkmark.circle.fill", title: "构建成功")

内容的提问来源于stack exchange,提问作者tstamatakis

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.28 00:24:04