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

如何在SwiftUI中复现粉蓝渐变带柔化模糊纹理的背景?

SwiftUI实现渐变+柔化模糊背景

要实现你需要的背景效果,只需要通过LinearGradient构建粉蓝渐变,再叠加一层柔化的噪点纹理即可,以下是完整实现代码:

import SwiftUI

struct GradientBlurBackgroundView: View {
    var body: some View {
        ZStack {
            // 粉蓝渐变背景
            LinearGradient(
                gradient: Gradient(colors: [
                    Color(red: 0.98, green: 0.90, blue: 0.95), // 柔和粉色
                    Color(red: 0.90, green: 0.95, blue: 0.98)  // 柔和蓝色
                ]),
                startPoint: .top,
                endPoint: .bottom
            )
            .ignoresSafeArea()
            
            // 柔化模糊纹理层
            if #available(iOS 17.0, *) {
                NoiseTexture(scale: 20)
                    .blur(radius: 8)
                    .opacity(0.15)
                    .ignoresSafeArea()
            } else {
                // iOS17以下兼容方案:用Shape生成噪点并模糊
                Rectangle()
                    .fill(
                        .randomNoise(scale: 20)
                            .opacity(0.2)
                    )
                    .blur(radius: 6)
                    .ignoresSafeArea()
            }
            
            // 这里可以添加你的应用内容
            VStack {
                Text("你的应用内容")
                    .font(.title)
                    .foregroundColor(.primary)
            }
        }
    }
}

#Preview {
    GradientBlurBackgroundView()
}

关键参数调整说明

  • 渐变颜色:可以修改Gradient(colors:)里的RGB值,匹配你想要的柔和粉蓝色调
  • 噪点纹理:
    • iOS17+:调整NoiseTexture的scale参数控制噪点密度,blur(radius)控制柔化程度,opacity控制纹理透明度
    • iOS17以下:通过.randomNoise(scale:)调整噪点密度,同样用blur和opacity控制柔化和透明度

这个方案完全基于SwiftUI原生API实现,不需要额外资源,效果可以通过参数微调来贴近目标样式。

内容的提问来源于stack exchange,提问作者Steven-Carrot

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.25 07:54:20