Reputation: 61
I'm trying to make a multi stage long press gesture where it switches between pressing, released, or inactive to execute certain action depending on the states appropriately.
However, I have trouble changing the gesture state of the LongPressState. The updating closure does not seem to be called.
Below is the code I have currently.
enum LongPressState {
case inactive
case pressing
case released
}
@GestureState private var longPressState: LongPressState = .inactive
private let longPressDuration: Double = 1.0
private let longPressDistance: CGFloat = 50.0
var longPress: some Gesture {
LongPressGesture(minimumDuration: longPressDuration, maximumDistance: longPressDistance)
.targetedToAnyEntity()
.updating($longPressState) { value, gestureState, transaction in
print("Updating")
gestureState = .pressing
}
.onEnded { value in
print(longPressState)
switch longPressState {
case .inactive:
print("Inactive stage")
case .pressing:
print("Pressing stage")
case .released:
print("Released stage")
}
}
}
var body: some View {
RealityView { content in
...
}
.gesture(longPress)
}
Upvotes: 0
Views: 178
Reputation: 29309
For entity targeting to work on a given RealityKit entity, the entity must have both a CollisionComponent and an InputTargetComponent
var body: some View {
RealityView { content in
let entity = ModelEntity(mesh: .generateBox(size: 0.1), materials: [SimpleMaterial(color: .white, isMetallic: true)])
entity.generateCollisionShapes(recursive: false)
entity.components.set(InputTargetComponent())
content.add(entity)
}
.gesture(longPress)
}
https://developer.apple.com/wwdc23/10203
Upvotes: 0