Skip to content

Instantly share code, notes, and snippets.

@HarshilShah
Created April 2, 2023 09:16
Show Gist options
  • Save HarshilShah/e042587358d9391ae9713a4651970020 to your computer and use it in GitHub Desktop.
Save HarshilShah/e042587358d9391ae9713a4651970020 to your computer and use it in GitHub Desktop.
Convenient initialisers for SwiftUI's LinearGradient view
import SwiftUI
public extension UnitPoint {
var diametricallyOpposite: UnitPoint {
UnitPoint(x: 1 - x, y: 1 - y)
}
}
public extension LinearGradient {
init(colors: [Color], towards end: UnitPoint) {
self.init(colors: colors, startPoint: end.diametricallyOpposite, endPoint: end)
}
init(gradient: Gradient, towards end: UnitPoint) {
self.init(gradient: gradient, startPoint: end.diametricallyOpposite, endPoint: end)
}
init(stops: [Gradient.Stop], towards end: UnitPoint) {
self.init(stops: stops, startPoint: end.diametricallyOpposite, endPoint: end)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment