-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Provide convient init for different gradient types
- Loading branch information
1 parent
bfd513c
commit 591b15b
Showing
5 changed files
with
106 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
// | ||
// AngularGradientBuilder.swift | ||
// | ||
// | ||
// Created by Chen Hai Teng on 1/25/24. | ||
// | ||
|
||
import SwiftUI | ||
|
||
public extension AngularGradient { | ||
init(center: UnitPoint = .center, angle: Angle = .zero, @GradientBuilder _ builder: ()-> Gradient ) { | ||
self.init(gradient: builder(), center: center, angle: angle) | ||
} | ||
|
||
init(center: UnitPoint = .center, startAngle: Angle = .zero, endAngle: Angle = .zero, @GradientBuilder _ builder: ()-> Gradient) { | ||
self.init(gradient: builder(), center: center, startAngle: startAngle, endAngle: endAngle) | ||
} | ||
} | ||
|
||
#Preview { | ||
AngularGradient { | ||
Color.red | ||
Color.green | ||
Color.blue | ||
} | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
// | ||
// EllipticalGradientBuilder.swift | ||
// | ||
// | ||
// Created by Chen Hai Teng on 1/25/24. | ||
// | ||
|
||
import SwiftUI | ||
|
||
@available(iOS 15.0, macOS 12.0, tvOS 15.0, watchOS 8.0, *) | ||
public extension EllipticalGradient { | ||
init(center: UnitPoint = .center, startRadiusFraction: CGFloat = 0.0, endRadiusFraction: CGFloat = 0.5, @GradientBuilder _ builder: () -> Gradient) { | ||
self.init(gradient: builder(), center: center, startRadiusFraction: startRadiusFraction, endRadiusFraction: endRadiusFraction) | ||
} | ||
} | ||
|
||
#Preview { | ||
VStack { | ||
if #available(iOS 15.0, macOS 12.0, tvOS 15.0, watchOS 8.0, *) { | ||
EllipticalGradient { | ||
Color.red | ||
Color.green | ||
Color.blue | ||
} | ||
} else { | ||
// Fallback on earlier versions | ||
Text("EllipticalGradient unavailable") | ||
} | ||
} | ||
} |
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
// | ||
// LinearGradientBuilder.swift | ||
// | ||
// | ||
// Created by Chen Hai Teng on 1/25/24. | ||
// | ||
|
||
import SwiftUI | ||
|
||
public extension LinearGradient { | ||
init(startPoint: UnitPoint = .leading, endPoint: UnitPoint = .trailing, @GradientBuilder _ builder: () -> Gradient) { | ||
self.init(gradient: builder(), startPoint: startPoint, endPoint: endPoint) | ||
} | ||
} | ||
|
||
#Preview { | ||
LinearGradient { | ||
Color.red | ||
Color.green | ||
Color.blue | ||
} | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
// | ||
// RadialGradientBuilder.swift | ||
// | ||
// | ||
// Created by Chen Hai Teng on 1/25/24. | ||
// | ||
|
||
import SwiftUI | ||
|
||
public extension RadialGradient { | ||
init(center: UnitPoint = .center, startRadius: CGFloat = 0.0, endRadius: CGFloat = 100.0, @GradientBuilder _ builder: () -> Gradient) { | ||
self.init(gradient: builder(), center: center, startRadius: startRadius, endRadius: endRadius) | ||
} | ||
} | ||
|
||
#Preview { | ||
RadialGradient { | ||
Color.red | ||
Color.green | ||
Color.blue | ||
} | ||
} |