Skip to content

Instantly share code, notes, and snippets.

@gtzsb
Created July 11, 2020 07:40
Show Gist options
  • Save gtzsb/665762187f5cf1e20b5264c3a810744a to your computer and use it in GitHub Desktop.
Save gtzsb/665762187f5cf1e20b5264c3a810744a to your computer and use it in GitHub Desktop.
SwiftUI Gradient
import SwiftUI
import PlaygroundSupport
let gradient: Gradient = Gradient(colors: [.yellow, .green, .blue, .purple, .red])
extension Color {
public static let systemBackground: Color = Color(UIColor.systemBackground)
}
struct Shape: View {
var body: some View {
ZStack {
RoundedRectangle(cornerRadius: 20)
.strokeBorder(Color.systemBackground, lineWidth: 10)
.frame(width: 100, height: 100)
Circle()
.strokeBorder(Color.systemBackground, lineWidth: 10)
.frame(width: 50, height: 50)
}
}
}
struct Title: View {
var body: some View {
Text("What a nice grandient!")
.font(.largeTitle)
.foregroundColor(.systemBackground)
.font(.headline)
.fontWeight(.bold)
}
}
struct ContentView: View {
var body: some View {
VStack(spacing: 50) {
Shape()
.overlay(LinearGradient(gradient: gradient, startPoint: .bottomLeading, endPoint: .topTrailing)
.mask(Shape()
.scaledToFill()
)
)
Title()
.overlay(LinearGradient(gradient: gradient, startPoint: .leading, endPoint: .trailing)
.mask(Title()
.scaledToFill()
)
)
}
}
}
PlaygroundPage.current.setLiveView(ContentView())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment