您好,登錄后才能下訂單哦!
這篇文章給大家介紹怎么在iOS中利用SwiftUI實現顏色漸變填充效果,內容非常詳細,感興趣的小伙伴們可以參考借鑒,希望對大家能有所幫助。
Gradient 漸變器
A color gradient represented as an array of color stops, each having a parametric location value.
gradient是一組顏色的合集,每個顏色都忽略位置參數
LinearGradient 線性漸變器
線性漸變器擁有沿軸進行漸變函數,我們可以自定義設置顏色空間、起點和終點。
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) } } }
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: 250) Text("富春山居圖") .padding() .foregroundColor(.white) .cornerRadius(8) .background( RadialGradient(gradient: Gradient( colors: [.yellow, .red]), center: .center, startRadius: 2, endRadius: 60)) .offset(y:-180) } } }
Angular Gradient
在角漸變中,我們只需要通過中心點。
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) } }
關于怎么在iOS中利用SwiftUI實現顏色漸變填充效果就分享到這里了,希望以上內容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。