Vi Tiet
Vi Tiet

Reputation: 61

VisionOS Development - Detect which hand executed the gesture

Is it possible to detect which hand chirality that executed a gesture?

For example,

    TapGesture(count: 2)
            .targetedToAnyEntity()
            .onEnded { value in
                // if left hand
                    // do something
                // if right hand
                    // do something
            }

Upvotes: 1

Views: 291

Answers (3)

Vi Tiet
Vi Tiet

Reputation: 61

I was able to detect hand chirality (kind of) with this hack. However, in order for it to work correctly, only one hand can be seen when this is queried.

func getFirstTrackedHandChirality() async -> HandAnchor.Chirality? {
    for await update in handTracking.anchorUpdates {
        let handAnchor = update.anchor
        guard handAnchor.isTracked else { continue }
        
        if let fingerTip = handAnchor.handSkeleton?.joint(.indexFingerTip) {
            guard fingerTip.isTracked else { continue }
            return handAnchor.chirality
        }
    }
    return nil
}

Upvotes: 1

lorem ipsum
lorem ipsum

Reputation: 29309

With VisionOS 2 it will be possible using Compositor-Services

https://developer.apple.com/documentation/visionos-release-notes/visionos-2-release-notes#Compositor-Services

SpatialEventGesture


struct ParticlePlayground: View {
    @State var model = ParticlesModel()


    var body: some View {
        Canvas { context, size in
            for particle in model.particles {
                context.fill(Path(ellipseIn: particle.frame),
                             with: .color(particle.color))
            }
        }
        .gesture(
            SpatialEventGesture()
                .onChanged { events in
                    for event in events {
                        if event.phase == .active { //or chirality

                            // Update particle emitters.
                            model.emitters[event.id] = ParticlesModel.Emitter(
                                location: event.location
                            )
                        } else {
                            // Remove emitters when no longer active.
                            model.emitters[event.id] = nil
                        }
                    }
                }
                .onEnded { events in
                    for event in events {
                        // Remove emitters when no longer active.
                        model.emitters[event.id] = nil
                    }
                }
        )
    }
}

Upvotes: 2

rob mayoff
rob mayoff

Reputation: 385690

I do not think SwiftUI offers that information as of visionOS 1.1. You’ll need to use ARKit’s hand tracking APIs if you want to know which hand is gesturing. It will be a substantial amount of work. Start with the ARKit in visionOS documentation and check out the HandTrackingProvider class.

Upvotes: 3

Related Questions