So the first thing you're going to want to do is to utilize LinearGradient
and RadialGradient
which are the two types of gradients that you can use to compose several layers. There are other ways you can do it by using .background
or .overlay
modifiers however in my example I want to keep it as simple as possible and allow you the freedom to explore other options with it. I've created two extensions so that you can simply reference LinearGradient.blueClearGradient
, for example, directly anywhere in your app. NOTE:: I used different colors, feel free to use what you want to use for your colors. I did this to make it more visually apparent what the individual pieces of the view were composed of.
extension LinearGradient {
static let blueClearGradient = LinearGradient(colors: [.blue, .clear],
startPoint: .bottom,
endPoint: .top)
}
extension RadialGradient {
static let radialYellowGradient = RadialGradient(colors: [.yellow, .clear],
center: .topLeading,
startRadius: 100,
endRadius: 400)
static let radialRedGradient = RadialGradient(colors: [.red, .clear],
center: .topTrailing,
startRadius: 100,
endRadius: 400)
}
Once you have those extensions it's as simple as utilizing them the same as you would any other view. You can use them to .fill
or even as a straight up view themselves. In this example, I used them as a .fill
for a shape.
struct GradientExample: View {
var body: some View {
ZStack {
Rectangle()
.fill(RadialGradient.radialRedGradient)
Rectangle()
.fill(RadialGradient.radialYellowGradient)
Rectangle()
.fill(LinearGradient.blueClearGradient)
}
}
}
Doing it this way produces a view that might look something like this.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments