# update gesture states

[SwiftUI](https://lochiwei.gitbook.io/ios/swiftui) ⟩ [Gestures](https://lochiwei.gitbook.io/ios/swiftui/gestures) ⟩ update gesture states

{% hint style="danger" %} <mark style="color:orange;">**Gesture state**</mark> is "<mark style="color:orange;">**transient**</mark>" which means it will <mark style="color:red;">**reset**</mark> back to its <mark style="color:red;">**initial state**</mark> when user <mark style="color:red;">**ends**</mark> or <mark style="color:red;">**cancels**</mark> the gesture.
{% endhint %}

{% tabs %}
{% tab title="⭐️ 重點" %}
{% hint style="info" %}

* To <mark style="color:red;">**update a view**</mark> **as a gesture changes**, add a [`GestureState`](https://developer.apple.com/documentation/swiftui/gesturestate) property to your view and update it with [`updating(_:body:)`](https://developer.apple.com/documentation/swiftui/gesture/updating\(_:body:\)).&#x20;
* SwiftUI invokes the <mark style="color:red;">**updating**</mark> callback as soon as it <mark style="color:orange;">**recognizes**</mark> the gesture and whenever the <mark style="color:orange;">**value of the gesture changes**</mark>.&#x20;
* SwiftUI <mark style="color:red;">**doesn’t invoke**</mark> the <mark style="color:orange;">**updating**</mark> callback when the user <mark style="color:red;">**ends**</mark> or <mark style="color:red;">**cancels**</mark> a gesture. Instead, the <mark style="color:red;">**gesture state**</mark> property automatically <mark style="color:red;">**resets**</mark> its state back to its <mark style="color:red;">**initial value**</mark>.
  {% endhint %}
  {% endtab %}

{% tab title="🔸 定義" %}

```swift
// ⭐️ 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>
```

{% endtab %}

{% tab title="💾 程式" %}
⬆️ 需要： [timerview](https://lochiwei.gitbook.io/ios/swift/scope/framework/built-in-frameworks/combine/timer/timerview "mention")

```swift
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()
                    }   
                }
            }
    }
}
```

{% endtab %}

{% tab title="📗 參考" %}

* [x] [SwiftUI](https://developer.apple.com/documentation/swiftui) ⟩ [Gestures](https://developer.apple.com/documentation/swiftui/gestures) ⟩ [Adding Interactivity with Gestures](https://developer.apple.com/documentation/swiftui/adding-interactivity-with-gestures) ⭐️
  {% endtab %}

{% tab title="❓" %}
{% hint style="warning" %}
問：「 [.updating()](https://developer.apple.com/documentation/swiftui/gesture/updating\(_:body:\)) 的 <mark style="color:purple;">**body**</mark> closure 的<mark style="color:red;">**第三個參數**</mark> [transaction](https://developer.apple.com/documentation/swiftui/transaction) 是幹什麼的❓」
{% endhint %}

{% hint style="success" %}
問：「 [.updating()](https://developer.apple.com/documentation/swiftui/gesture/updating\(_:body:\)) 似乎只能更新 [gesturestate](https://lochiwei.gitbook.io/ios/swiftui/gestures/gesturestate "mention")，不能更新 [state](https://lochiwei.gitbook.io/ios/swiftui/view/state/value/state "mention") 變數❓」

答：「 更新 view states 要用 .onChanged() 」

👉 [Adding Interactivity with Gestures](https://developer.apple.com/documentation/swiftui/adding-interactivity-with-gestures) ⭐️
{% endhint %}
{% endtab %}
{% endtabs %}
