-
-
Save raduraducu/f2c7ad09706efe5e405a76cad27e82f4 to your computer and use it in GitHub Desktop.
[Pulse ViewModifier] Pulse SwiftUI ViewModifier #SwiftUI #ViewModifier
This file contains 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
// Kieran Brown | |
// Kieran's Components "Pulse and Sheen" | |
// https://kieranb662.github.io/blog/2020/04/17/Pulse-and-Sheen | |
struct PulseEffect<S: Shape>: ViewModifier { | |
var shape: S | |
@State var isOn: Bool = false | |
var animation: Animation { | |
Animation | |
.easeIn(duration: 1) | |
.repeatCount(8, autoreverses: false) | |
} | |
func body(content: Content) -> some View { | |
content | |
.background( | |
ZStack { | |
shape | |
.stroke(Color.yellow, lineWidth: 1) | |
.scaleEffect(self.isOn ? 2 : 1) | |
.opacity(self.isOn ? 0 : 1) | |
shape | |
.stroke(Color.blue) | |
}) | |
.onAppear { | |
withAnimation(self.animation) { | |
self.isOn = true | |
} | |
} | |
} | |
} | |
public extension View { | |
func pulse<S: Shape>(_ shape: S) -> some View { | |
self.modifier(PulseEffect(shape: shape)) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment