Skip to content

Instantly share code, notes, and snippets.

@dqhieu
Last active August 25, 2023 07:43
Show Gist options
  • Save dqhieu/7c89a19541a9ab70023f8794130051aa to your computer and use it in GitHub Desktop.
Save dqhieu/7c89a19541a9ab70023f8794130051aa to your computer and use it in GitHub Desktop.
struct ContentView: View {
@State private var isActive = false
@State private var isPressing = false
@State var scale: CGFloat = 1
@State var count = 0
var body: some View {
ZStack {
Circle()
.fill(Color.blue.opacity(0.5))
.frame(width: isActive ? 1100 : 0, height: isActive ? 1100 : 0, alignment: .center)
ZStack {
RoundedRectangle(cornerRadius: 48, style: .continuous)
.fill(isActive ? Color.blue : Color(UIColor.lightGray))
.frame(width: 180, height: 180)
.scaleEffect(x: isPressing ? 0.95 : 1, y: isPressing ? 1.05 : 1)
.onLongPressGesture(minimumDuration: 1, perform: {
withAnimation(.spring()) {
isActive = true
}
UIImpactFeedbackGenerator(style: .soft).impactOccurred()
count += 1
}, onPressingChanged: { isPressing in
withAnimation(.spring()) {
self.isPressing = isPressing
}
})
VStack(spacing: 16) {
Text("☕️")
.scaleEffect(x: isPressing ? 3 : 1.5, y: isPressing ? 3 : 1.5)
.offset(x: 0, y: isPressing ? 18 : 0)
Text("coffee")
.foregroundColor(isActive ? .white : .black)
.offset(x: 0, y: isPressing ? 100 : 0)
}
.font(.title)
if count > 0 {
Text("x\(count)")
.fontWeight(.bold)
.foregroundColor(.blue)
.background(
Circle()
.fill(.white)
.frame(width: 32, height: 32)
)
.offset(x: 50, y: -50)
.onTapGesture {
count -= 1
if count == 0 {
withAnimation(.spring()) {
isActive = false
}
}
}
}
}
.frame(width: 180, height: 189)
.clipped()
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment