Add animations

This commit is contained in:
Hariharan Mudaliar 2025-11-18 02:28:03 +05:30
commit 4aa481ecb1
5 changed files with 175 additions and 21 deletions

View file

@ -11,15 +11,20 @@ struct HoverButton: View {
var icon: String
var iconColor: Color = .white;
var scale: Image.Scale = .medium
var pressEffect: PressEffect? = nil
var action: () -> Void
var contentTransition: ContentTransition = .symbolEffect;
@State private var isHovering = false
@State private var pressOffset: CGFloat = 0
var body: some View {
let size = CGFloat(scale == .large ? 40 : 30)
Button(action: action) {
Button(action: {
triggerPressEffect()
action()
}) {
Rectangle()
.fill(.clear)
.contentShape(Rectangle())
@ -37,10 +42,31 @@ struct HoverButton: View {
}
}
.buttonStyle(PlainButtonStyle())
.offset(x: pressOffset)
.onHover { hovering in
withAnimation(.smooth(duration: 0.3)) {
isHovering = hovering
}
}
}
private func triggerPressEffect() {
guard let pressEffect else { return }
switch pressEffect {
case .nudge(let amount):
withAnimation(.spring(response: 0.2, dampingFraction: 0.55)) {
pressOffset = amount
}
DispatchQueue.main.asyncAfter(deadline: .now() + 0.16) {
withAnimation(.spring(response: 0.28, dampingFraction: 0.72)) {
pressOffset = 0
}
}
}
}
enum PressEffect {
case nudge(CGFloat)
}
}