IrregularGradient Project logo


Twitter: @joogps

一个 SwiftUI 库,用于渲染美观、动画化且不规则的渐变视图。

注意

本项目使用纯 SwiftUI 实现此效果,这可能会消耗大量计算资源。如果您想要更有效率的方案,请考虑切换到 FluidGradient

安装

此仓库是一个 Swift 包,只需将其包含在您的 Xcode 项目中,并在File > Add package dependencies下定位它。然后,在您要使用它的 Swift 文件中 import IrregularGradient

用法

您可以使用以下修饰符将不规则渐变添加到您的应用中

RoundedRectangle(cornerRadius: 24.0, style: .continuous)
    .irregularGradient(colors: [.orange, .pink, .yellow, .orange, .pink, .yellow], backgroundColor: .orange)

其他参数如下

irregularGradient(colors: [Color], background: () -> View, shouldAnimate: Binding<Bool> = .constant(true), speed: Double = 10)

您也可以使用 IrregularGradient 独立视图,它存在于自己的容器中。

实现原理

此软件包的当前实现是通过创建指定颜色的色块(SwiftUI 的 Ellipse 形状),这些色块在容器中随机移动和缩放,然后进行模糊处理以达到所需的效果。

问题

如果您有任何问题或建议,可以在此 GitHub 存储库上创建一个 issue 或 pull request,甚至可以通过 Twitteremail 与我联系。