SwiftUI 爲咱們提供了各類梯度選項,全部這些選項均可以經過多種方式使用。函數
A color gradient represented as an array of color stops, each having a parametric location value.
gradient是一組顏色的合集,每一個顏色都忽略位置參數spa
線性漸變器擁有沿軸進行漸變函數,咱們能夠自定義設置顏色空間、起點和終點。3d
下面咱們看看LinearGradient效果
code
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: 250) Text("洛神賦圖") .padding() .foregroundColor(.white) .cornerRadius(8) .background(LinearGradient(gradient: Gradient(colors: [.white, .black]), startPoint: .top, endPoint: .bottom)) .offset(y:-280) } } }
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: 250) Text("清明上河圖") .padding() .foregroundColor(.white) .cornerRadius(8) .background(LinearGradient(gradient: Gradient( colors: [.yellow, .red]), startPoint: .topLeading, endPoint: .bottomTrailing)) .offset(y:-180) } } }
在徑向漸變中,咱們必須指定起始半徑點,端半徑點與中心點,從徑向漸變開變.blog
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: 250) Text("富春山居圖") .padding() .foregroundColor(.white) .cornerRadius(8) .background( RadialGradient(gradient: Gradient( colors: [.yellow, .red]), center: .center, startRadius: 2, endRadius: 60)) .offset(y:-180) } } }
在角漸變中,咱們只須要經過中心點。
教程
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: 250) Text("漢宮春曉圖") .padding() .foregroundColor(.white) .cornerRadius(8) .background( RadialGradient(gradient: Gradient( colors: [.yellow, .red]), center: .center, startRadius: 2, endRadius: 60)) .offset(y:-180) } } }
QQ:3365059189
SwiftUI技術交流QQ羣:518696470ip