๐ดGradient
Last updated
Last updated
import SwiftUI
extension Gradient {
// general linear gradient ---------------------------
/// `Gradient.linear(from: start, to: end, colors: colors)`
///
/// - Parameters:
/// - from: ๆผธๅฑค้ๅง็ๅฐๆน
/// - end: ๆผธๅฑค็ตๆ็ๅฐๆน
/// - colors: ๆผธๅฑค้ก่ฒ (`[Color]`)
public static func linear(
from start: UnitPoint,
to end: UnitPoint,
colors : [Color] // use array
) -> LinearGradient
{
LinearGradient(
gradient : Gradient(colors: colors),
startPoint: start,
endPoint : end
)
}
/// `Gradient.linear(from: start, to: end, colors: c1, c2, c3)`
///
/// - Parameters:
/// - from: ๆผธๅฑค้ๅง็ๅฐๆน
/// - end: ๆผธๅฑค็ตๆ็ๅฐๆน
/// - colors: ๆผธๅฑค้ก่ฒ (`Color...`)
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 ------------------------
/// `Gradient.down(colors)`
/// - Parameters:
/// - colors: ๆผธๅฑค้ก่ฒ (`[Color]`)
public static func down(_ colors: [Color]) -> LinearGradient {
linear(from: .top, to: .bottom, colors: colors)
}
/// `Gradient.down(color1, color2, color3)`
public static func down(_ colors: Color...) -> LinearGradient {
down(colors)
}
/// `Gradient.up(colors)`
public static func up(_ colors: [Color]) -> LinearGradient {
linear(from: .bottom, to: .top, colors: colors)
}
/// `Gradient.up(color1, color2, color3)`
public static func up(_ colors: Color...) -> LinearGradient {
up(colors)
}
/// `Gradient.right(colors)`
/// - Parameters:
/// - colors: ๆผธๅฑค้ก่ฒ (`[Color]`)
public static func right(_ colors: [Color]) -> LinearGradient {
linear(from: .leading, to: .trailing, colors: colors)
}
/// `Gradient.right(c1, c2, c3)`
public static func right(_ colors: Color...) -> LinearGradient {
right(colors)
}
/// `Gradient.left(colors)`
public static func left(_ colors: [Color]) -> LinearGradient {
linear(from: .trailing, to: .leading, colors: colors)
}
/// `Gradient.left(c1, c2, c3)`
public static func left(_ colors: Color...) -> LinearGradient {
left(colors)
}
/// `Gradient.bottomRight(colors)`
/// - Parameters:
/// - colors: ๆผธๅฑค้ก่ฒ
public static func bottomRight(_ colors: [Color]) -> LinearGradient {
linear(from: .topLeading, to: .bottomTrailing, colors: colors)
}
/// Gradient.bottomRight(color1, color2, color3)
public static func bottomRight(_ colors: Color...) -> LinearGradient {
bottomRight(colors)
}
/// `Gradient.topLeft(colors)`
public static func topLeft(_ colors: [Color]) -> LinearGradient {
linear(from: .bottomTrailing, to: .topLeading, colors: colors)
}
/// `Gradient.topLeft(color1, color2, color3)`
public static func topLeft(_ colors: Color...) -> LinearGradient {
topLeft(colors)
}
/// `Gradient.topRight(colors)`
/// - Parameters:
/// - colors: ๆผธๅฑค้ก่ฒ
public static func topRight(_ colors: [Color]) -> LinearGradient {
linear(from: .bottomLeading, to: .topTrailing, colors: colors)
}
/// Gradient.topRight(color1, color2, color3)
public static func topRight(_ colors: Color...) -> LinearGradient {
topRight(colors)
}
/// Gradient.bottomLeft(colors)
public static func bottomLeft(_ colors: [Color]) -> LinearGradient {
linear(from: .topTrailing, to: .bottomLeading, colors: colors)
}
/// Gradient.bottomLeft(color1, color2, color3)
public static func bottomLeft(_ colors: Color...) -> LinearGradient {
bottomLeft(colors)
}
}
import SwiftUI
import PlaygroundSupport
struct ContentView: View {
var body: some View {
HStack {
Group {
// โญ๏ธ custom `Gradient` extension
Gradient.right(.orange, .red)
.watermark("right", .bottom)
Gradient.down(.green, .blue)
.watermark("down", .bottom)
Gradient.bottomRight(.white, .gray)
.watermark("bottomRight", .bottom)
Gradient.topRight(.yellow, .purple)
.watermark("topRight", .bottom)
} // Group
.frame(width: 120, height: 120)
.shadow(radius: 4)
.border(Color.black)
} // container (HStack)
.padding(10)
.background(Color(white: 0.85))
}
}
PlaygroundPage.current.setLiveView(ContentView())
can be applied on Text.
Sarun โฉ
SwiftUI โฉ Drawing and Animation โฉ Gradient LinearGradient