Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save peantunes/8614c144fb11d787a8b6622911b24bba to your computer and use it in GitHub Desktop.
Save peantunes/8614c144fb11d787a8b6622911b24bba to your computer and use it in GitHub Desktop.
struct RecursivelyGlowInTheDarkViewModifier: ViewModifier {
let colors: [Color]
func body(content: Content) -> some View {
var modifiedColors = colors // First create a local variable
let nextColor: Color?
if !modifiedColors.isEmpty { // if it is not empty, remove the first element to use it
nextColor = modifiedColors.remove(at: 0)
} else { // or set as nil
nextColor = nil
}
// calling the builder
return buildView(content: content, nextColor: nextColor, colors: modifiedColors)
}
// Using @ViewBuilder to accept different results type
@ViewBuilder private func buildView(content: Content, nextColor: Color?, colors: [Color]) -> some View {
if let color = nextColor {
// in the case it has more colors, it adds the shadow and call the modifier again
content
.shadow(color: color, radius: 5)
.modifier(RecursivelyGlowInTheDarkViewModifier(colors: colors))
} else {
// otherwise it only returns the view without modifier
content
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment