๐+LinearGradient
Last updated
Last updated
import SwiftUI
extension Gradient {
// general linear gradient ---------------------------
public static func linear(
from start: UnitPoint,
to end: UnitPoint,
colors : [Color] // use array
) -> LinearGradient
{
LinearGradient(
gradient : Gradient(colors: colors),
startPoint: start,
endPoint : end
)
}
public static func linear(
from start: UnitPoint,
to end: UnitPoint,
colors : Color... // use variadic parameter
) -> LinearGradient
{
linear(from: start, to: end, colors: colors)
}
// specialized linear gradients ------------------------
// top to bottom
public static func vertical(_ colors: Color...) -> LinearGradient {
linear(from: .top, to: .bottom, colors: colors)
}
// leading to trailing
public static func horizontal(_ colors: Color...) -> LinearGradient {
linear(from: .leading, to: .trailing, colors: colors)
}
// top leading to bottom trailing
public static func diagonal(_ colors: Color...) -> LinearGradient {
linear(from: .topLeading, to: .bottomTrailing, colors: colors)
}
// top leading to bottom trailing
public static func diagonal2(_ colors: Color...) -> LinearGradient {
linear(from: .bottomLeading, to: .topTrailing, colors: colors)
}
}
import SwiftUI
import PlaygroundSupport
struct ContentView: View {
var body: some View {
HStack {
Group {
Gradient.horizontal(.red, .orange)
.watermark("horizontal", .bottom)
Gradient.vertical(.green, .blue)
.watermark("vertical", .bottom)
Gradient.diagonal(.white, .gray)
.watermark("diagonal", .bottom)
Gradient.diagonal2(.yellow, .purple)
.watermark("diagonal2", .bottom)
} // Group
.frame(width: 100, height: 100)
.shadow(radius: 4)
.border(Color.black)
} // container (HStack)
.padding(30)
.background(Color(white: 0.85))
}
}
PlaygroundPage.current.setLiveView(ContentView())
SwiftUI โฉ Drawing and Animation โฉ LinearGradient
ShapeStyle extension ShapeStyle+LinearGradient for LinearGradient.