How to use LongPressGesture in SwiftUI | Continued Learning #1
Click Here
을 눌렀을 때 파란색 바가 차오르도록 한다. Reset
을 눌렀을 때 다시 원래 상태로 돌아가도록 한다.Click Here
을 누르고 있어야 하는 시간isCompleted
를 통해 2번, isSuccess
를 통해 1번 목표.onLongPressGesture(minimumDuration: 1.0, maximumDistance: 50) { isPressing in
// press start -> min duration
} perform: {
// 구현할 동작
}
import SwiftUI
struct LongPressGestureBootCamp: View {
@State private var isCompleted: Bool = false
@State private var isSuccess: Bool = false
var body: some View {
VStack {
Rectangle()
.fill(.blue)
.frame(maxWidth: isCompleted ? .infinity : 0)
.frame(height: 55)
.frame(maxWidth: .infinity, alignment: .leading)
.background(.gray)
HStack {
Text("Click Here")
.foregroundColor(.white)
.padding()
.background(.black)
.cornerRadius(10)
.onLongPressGesture(minimumDuration: 1.0, maximumDistance: 50) { isPressing in
// press start -> min duration
if isPressing {
withAnimation(.easeInOut(duration: 1.0)) {
isCompleted.toggle()
}
} else {
DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) {
if !isSuccess {
withAnimation(.easeInOut) {
isCompleted = false
}
}
}
}
} perform: {
withAnimation(.easeInOut) {
isSuccess = true
}
}
Text("Reset")
.foregroundColor(.white)
.padding()
.background(.black)
.cornerRadius(10)
.onTapGesture {
isCompleted = false
isSuccess = false
}
}
}
}
}