Skip to content

Instantly share code, notes, and snippets.

@kieranb662
Last active June 29, 2020 18:27
Show Gist options
  • Save kieranb662/56da69ee80cd504eeb5badffc54cddd5 to your computer and use it in GitHub Desktop.
Save kieranb662/56da69ee80cd504eeb5badffc54cddd5 to your computer and use it in GitHub Desktop.
[Sheen ViewModifier] SwiftUI Sheen Modifier #SwiftUI #ViewModifier
// Kieran Brown
// Kieran's Components "Pulse and Sheen"
// https://kieranb662.github.io/blog/2020/04/17/Pulse-and-Sheen
struct SheenEffect: ViewModifier {
@State var isOn: Bool = false
var color = Color.white.opacity(0.6)
var width: CGFloat = 50
let animation = {
Animation
.easeInOut(duration: 1)
.delay(0.8)
.repeatCount(8, autoreverses: false)
}()
func body(content: Content) -> some View {
// 1
content.overlay(
GeometryReader { proxy in
RoundedRectangle(cornerRadius: 5)
.foregroundColor(self.color)
// 2
.transformEffect(CGAffineTransform(a: 1, b: 0, c: tan(-.pi/6), d: 1, tx: 0, ty: 0))
// 3
.offset(x: self.isOn ? proxy.size.width : -proxy.size.width, y: 0)
.frame(width: self.width)
.blur(radius: 7)
}.mask(content)
)
.onAppear {
withAnimation(self.animation) {
self.isOn = true
}
}
}
}
public extension View {
func sheen() -> some View {
self.modifier(SheenEffect())
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment