Last active
July 27, 2024 13:21
-
-
Save peantunes/1cc1214c08988e1e7b03769cdcbcb07e to your computer and use it in GitHub Desktop.
Reflection effect for SwiftUI
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
private struct GroundReflectionViewModifier: ViewModifier { | |
let offsetY: CGFloat | |
func body(content: Content) -> some View { | |
content | |
.background( | |
content | |
.mask( | |
LinearGradient( | |
gradient: Gradient(stops: [.init(color: .white, location: 0.0), .init(color: .clear, location: 0.6)]), | |
startPoint: .bottom, | |
endPoint: .top) | |
) | |
.scaleEffect(x: 1.0, y: -1.0, anchor: .bottom) | |
.opacity(0.3) | |
.offset(y: offsetY) | |
) | |
} | |
} | |
extension View { | |
func reflection(offsetY: CGFloat = 1) -> some View { | |
modifier(GroundReflectionViewModifier(offsetY: offsetY)) | |
} | |
} | |
struct GroundReflectionViewModifier_Previews: PreviewProvider { | |
static var previews: some View { | |
HStack { | |
Text("🐈") | |
.xxlText(1) | |
.reflection() | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment