Skip to content

Instantly share code, notes, and snippets.

@snowzurfer
Last active February 26, 2023 06:44
Show Gist options
  • Save snowzurfer/cd99629585e27a271e07c5aedf16d4e1 to your computer and use it in GitHub Desktop.
Save snowzurfer/cd99629585e27a271e07c5aedf16d4e1 to your computer and use it in GitHub Desktop.
SwiftUI ❤️ like ❤️ button with animated increments
import SwiftUI
struct LikeButton: View {
@State private var likesCount = 0
@State private var tappedLike = false
var body: some View {
Button {
tappedLike = false
likesCount += 1
withAnimation(.easeInOut(duration: 0.1)) {
tappedLike = true
}
} label: {
Image(systemName: likesCount == 0 ? "heart" : "heart.fill")
.renderingMode(.original)
}
.overlay {
Text("+\(likesCount)")
.font(.caption2)
.offset(x: 0, y: tappedLike ? -30 : 0)
.opacity(tappedLike ? 1 : 0)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment