Skip to content

Instantly share code, notes, and snippets.

@peantunes
Last active May 11, 2021 21:03
Show Gist options
  • Save peantunes/1cc1214c08988e1e7b03769cdcbcb07e to your computer and use it in GitHub Desktop.
Save peantunes/1cc1214c08988e1e7b03769cdcbcb07e to your computer and use it in GitHub Desktop.
Reflection effect for SwiftUI
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