0
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

【SwiftUI】DragGestureを使って画面タップを可視化する

Posted at

はじめに

昨日、@GestureStateについての記事を書きました
今回はそれの応用で実用的なものを作ってみます

サンプルアプリ

これは画面タップを可視化しています。
Simulator Screen Recording - iPhone 15 - 2023-11-09 at 23.17.59.gif

実装

import SwiftUI

struct ContentView: View {
    @GestureState private var location: CGPoint?

    var body: some View {
        VStack {
            if let location {
                Circle()
                    .frame(width: 50, height: 50)
                    .position(location)
                    .foregroundStyle(.cyan)
            }
        }
        .frame(maxWidth: .infinity, maxHeight: .infinity)
        .background(.red)
        .gesture(dragGesture)
    }
    
    var dragGesture: some Gesture {
        DragGesture(minimumDistance: 0, coordinateSpace: .local)
            .updating($location) { value, location, transaction in
                location = value.location
            }
    }
}

おわり

@GestureStateを使うと自動でnilになるので指を離したことを検知することが簡単に行えます。

0
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
0
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?