๐ฐupdate gesture states
SwiftUI โฉ Gestures โฉ update gesture states
Gesture state is "transient" which means it will reset back to its initial state when user ends or cancels the gesture.
// โญ๏ธ updates @GestureState var as the gestureโs value changes.
// `updating($state){ value, state, transaction in ... }`
func updating<State>(
_ state: GestureState<State>,
body: @escaping (
Self.Value, // gesture's current value
inout State, // @GestureState var to be updated
inout Transaction // animation "context" (โ)
) -> Void
) -> GestureStateGesture<Self, State>โฌ๏ธ ้่ฆ๏ผ TimerView
struct ContentView: View {
// โญ๏ธ gesture state:
// reset to initial value when ends or cancels.
@GestureState var isPressing = false
var body: some View {
// long press (for 3 sec)
let press = LongPressGesture(minimumDuration: 3)
// โญ๏ธ update gesture state
.updating($isPressing) { value, state, _ in
state = value
}
return Circle()
.fill(isPressing ? .pink : .green)
.frame(width: 100, height: 100, alignment: .center)
.gesture(press) // โญ๏ธ apply gesture
.overlay {
if isPressing {
// ๐ TimeView: count-down timer
TimerView(seconds: 3) { sec in
Text("\(sec)").font(.system(size: 50)).bold()
}
}
}
}
}ๅ๏ผใ .updating() ็ body closure ็็ฌฌไธๅๅๆธ transaction ๆฏๅนนไป้บผ็โใ
ๅ๏ผใ .updating() ไผผไนๅช่ฝๆดๆฐ GestureState๏ผไธ่ฝๆดๆฐ ๏ผ State ่ฎๆธโใ
็ญ๏ผใ ๆดๆฐ view states ่ฆ็จ .onChanged() ใ
๐ Adding Interactivity with Gestures โญ๏ธ
Last updated
Was this helpful?