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

SwiftUI中为SF Symbol添加描边,还原苹果节日灯效

仅用SF Symbol实现SwiftUI节日灯效方案

核心思路

无需Vision框架,通过双层SF Symbol叠加+动态模糊/阴影+动画,模拟苹果官网的发光闪烁灯效——底层图标做模糊发光处理,上层叠加清晰的主体图标,避免图标本身失真。

基础单图标灯效实现

struct FestivalLightIcon: View {
    @State private var isGlowing = true
    let iconName: String
    
    var body: some View {
        ZStack {
            // 发光边框层:放大+模糊+阴影模拟光晕
            Image(systemName: iconName)
                .font(.system(size: 60))
                .foregroundColor(.yellow)
                .blur(radius: isGlowing ? 8 : 4)
                .shadow(color: .yellow.opacity(0.8), radius: isGlowing ? 12 : 6)
            
            // 清晰主体层:保证图标细节
            Image(systemName: iconName)
                .font(.system(size: 40))
                .foregroundColor(.white)
        }
        .onAppear {
            // 循环闪烁动画
            withAnimation(Animation.easeInOut(duration: 1.5).repeatForever(autoreverses: true)) {
                isGlowing.toggle()
            }
        }
    }
}

进阶灯串错落闪烁效果

如果是多图标组成灯串,给每个图标设置不同的动画延迟,模拟自然的错落发光:

struct LightStringView: View {
    // 可选的SF Symbol列表
    private let iconList = ["star.fill", "circle.fill", "heart.fill", "diamond.fill"]
    @State private var glowStates: [Bool] = []
    
    init() {
        _glowStates = State(initialValue: Array(repeating: true, count: iconList.count))
    }
    
    var body: some View {
        HStack(spacing: 25) {
            ForEach(0..<iconList.count, id: \.self) { index in
                FestivalLightIcon(iconName: iconList[index])
                    .onAppear {
                        // 每个图标延迟启动动画
                        DispatchQueue.main.asyncAfter(deadline: .now() + Double(index) * 0.3) {
                            withAnimation(Animation.easeInOut(duration: 1.2).repeatForever(autoreverses: true)) {
                                glowStates[index].toggle()
                            }
                        }
                    }
            }
        }
    }
}

效果调整技巧

  • 调整foregroundColor:用暖黄、橙红这类色调匹配节日氛围
  • 修改blur(radius:)和shadow(radius:)数值:数值越大,发光范围越广
  • 动画参数调整:修改duration控制闪烁频率,autoreverses控制明暗过渡的自然度

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.15 05:45:32