Skip to content

Instantly share code, notes, and snippets.

@magnuskahr
Created October 3, 2021 08:25
Show Gist options
  • Save magnuskahr/42934c41e76e67db7c4cdc2e1e43c22b to your computer and use it in GitHub Desktop.
Save magnuskahr/42934c41e76e67db7c4cdc2e1e43c22b to your computer and use it in GitHub Desktop.
SwiftUI View pulsating effect
//
// 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