Created
May 15, 2021 13:59
-
-
Save peantunes/8614c144fb11d787a8b6622911b24bba to your computer and use it in GitHub Desktop.
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
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