This article mainly introduces the iOS SwiftUI color gradient filling effect implementation, the article through the example code introduction is very detailed, need friends can understand

SwiftUI provides us with a variety of gradient options, all of which can be used in a variety of ways.

Gradient on the Gradient editor

A color gradient represented as an array of color stops, each having a parametric location value.

Gradient is a collection of colors, each ignoring a positional parameter

A LinearGradient

The linear gradient has a gradient function along the axis, and we can customize the color space, starting point and end point. So let’s take a look at the LinearGradient effect

import SwiftUI struct LineView: View { var gradient: Gradient { let stops: [Gradient.Stop] = [ .init(color: .red, location: 0.5),.init(color:.yellow, location: 0.5)] return Gradient(stops: stops)} var body: some View { ZStack { LinearGradient(gradient: gradient, startPoint: .top, endPoint: .trailing) .edgesIgnoringSafeArea(.all) Image("1") .resizable() .aspectRatio(contentMode: .fit) .clipShape(Circle()) .overlay(Circle() .stroke(lineWidth: 8) .foregroundColor(.white)) .frame(width: Background-color (.white).cornerradius (8). Background (LinearGradient(gradient: Gradient(colors: [.white, .black]), startPoint: .top, endPoint: .bottom)) .offset(y:-280) } } }Copy the code

import SwiftUI struct LineGradient3Color: View { var body: some View { ZStack { LinearGradient(gradient: Gradient( colors: [.blue, .white, .pink]), startPoint: .topLeading, endPoint: .bottomTrailing) .edgesIgnoringSafeArea(.all) Image("2") .resizable() .aspectRatio(contentMode: .fit) .clipShape(Circle()) .overlay(Circle() .stroke(lineWidth: 8) .foregroundColor(.white)) .frame(width: Background-color (.white).cornerradius (8). Background (LinearGradient(gradient: Gradient( colors: [.yellow, .red]), startPoint: .topLeading, endPoint: .bottomTrailing)) .offset(y:-180) } } }Copy the code

Radial Gradient

In the radial gradient, we must specify the starting radius, end radius, and center point to change from the radial gradient.

import SwiftUI struct RadialView: View { var body: some View { ZStack { RadialGradient(gradient: Gradient( colors: [.blue, .black]), center: .center, startRadius: 2, endRadius: 650) .edgesIgnoringSafeArea(.all) Image("3") .resizable() .aspectRatio(contentMode: .fit) .clipShape(Circle()) .overlay(Circle() .stroke(lineWidth: 8) .foregroundColor(.white)) .frame(width: Background-color (.white).cornerradius (8). Background (gradient: RadialGradient(gradient: gradient) Gradient( colors: [.yellow, .red]), center: .center, startRadius: 2, endRadius: 60)) .offset(y:-180) } } }Copy the code

Angular Gradient

In Angle gradient, we just need to go through the center point.

import SwiftUI struct AngularView: View { var body: some View { ZStack { AngularGradient(gradient: Gradient( colors: [.green, .blue, .black, .green, .blue, .black, .green]), center: .center) .edgesIgnoringSafeArea(.all) Image("4") .resizable() .aspectRatio(contentMode: .fit) .clipShape(Circle()) .overlay(Circle() .stroke(lineWidth: 8) .foregroundColor(.white)) .frame(width: Background-color (.white).cornerradius (8). Background (gradient: RadialGradient) Gradient( colors: [.yellow, .red]), center: .center, startRadius: 2, endRadius: 60)) .offset(y:-180) } } }Copy the code

The above is all the content of this article, I hope to help you learn

The original address