Created
October 3, 2021 08:25
-
-
Save magnuskahr/42934c41e76e67db7c4cdc2e1e43c22b to your computer and use it in GitHub Desktop.
SwiftUI View pulsating effect
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
// | |
// View+pulsating.swift | |
// | |
// Created by Magnus Jensen on 02/10/2021. | |
// | |
import SwiftUI | |
extension View { | |
func pulsating() -> some View { | |
self.modifier(OpacityFadingModifier()) | |
} | |
} | |
private struct OpacityFadingModifier: ViewModifier { | |
@State private var value = 1.0 | |
func body(content: Content) -> some View { | |
content | |
.opacity(value) | |
.transition(.scale) // Fixes an issue moving the `content` when in a list | |
.animation(.easeOut(duration: 0.5).repeatForever(autoreverses: true), value: value) | |
.onAppear { | |
value = 0.25 | |
} | |
} | |
} | |
struct View_pulsating_Previews: PreviewProvider { | |
static var previews: some View { | |
Color.green.pulsating() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment