Created
July 11, 2020 07:40
-
-
Save gtzsb/665762187f5cf1e20b5264c3a810744a to your computer and use it in GitHub Desktop.
SwiftUI Gradient
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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