3
2

【SwiftUI】@GestureStateを使ってみる

Posted at

はじめに

@GestureStateという機能があり、使ったことがなかったので使ってみました

サンプルアプリ

Simulator Screen Recording - iPhone 15 - 2023-11-08 at 22.13.27.gif

実装

こちらの公式ドキュメントのコードを使用しています

import SwiftUI

struct ContentView: View {
    @GestureState private var isDetectingLongPress = false

    var longPress: some Gesture {
        LongPressGesture(minimumDuration: 3)
            .updating($isDetectingLongPress) { currentState, gestureState, transaction in
                gestureState = currentState
            }
    }

    var body: some View {
        Circle()
            .fill(self.isDetectingLongPress ? Color.red : Color.green)
            .frame(width: 100, height: 100, alignment: .center)
            .gesture(longPress)
    }
}

@GestureStateのメリット

ジェスチャーが終了すると自動でfalseになるので、
押している間だけ処理をするということに適しています。

  • 長押ししてる間だけ録音
  • 長押ししてる間だけ動画を撮影

色々と使えそうな機能ですね

おわり

この機能は使われているのもみたことがなかったので存在を全く知りませんでした

参考記事

3
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
2