react-native-draggable icon indicating copy to clipboard operation
react-native-draggable copied to clipboard

How to do "drag hold" event

Open silencer07 opened this issue 5 years ago • 2 comments

Basically I am trying to implement my own joystick since the react native joystick libraries are old, buggy and unsupported already

joystick screenshot

All is good except I cannot find a way to listen to "drag hold" event which should continously trigger like onDrag event, but with the event values not changing(because the finger is staying at same spot

is this even possible with current API or an enhancement is needed to support this event?

silencer07 avatar Aug 05 '20 10:08 silencer07

Sample Code: (the red dot at the center of the screenshot above)

<Draggable
        renderColor={styles.fieldOfViewContainer.borderColor}
        renderText=""
        isCircle={true}
        shouldReverse={true}
        renderSize={20}
        x={width / 2 - 10}
        y={height / 2 - 10}
        z={2}
        minX={0}
        minY={0}
        maxX={width}
        maxY={height}
        touchableOpacityProps={{
          hitSlop: {
            bottom: 10,
            left: 10,
            right: 10,
            top: 10
          }
        }}
        onDrag={(event) => {
          let horizontalDirection = null
          console.log(`event.nativeEvent.locationX`, event.nativeEvent.locationX)
          const threshold = 7.5
          if (event.nativeEvent.locationX < -(threshold)) {
            horizontalDirection = Direction.LEFT
          } else if (event.nativeEvent.locationX > threshold) {
            horizontalDirection = Direction.RIGHT
          }

          let verticalDirection = null
          if (event.nativeEvent.locationY < -(threshold)) {
            verticalDirection = Direction.UP
          } else if (event.nativeEvent.locationY > threshold) {
            verticalDirection = Direction.DOWN
          }

          onDrag([
            horizontalDirection,
            verticalDirection
          ])
        }}
      />

silencer07 avatar Aug 05 '20 10:08 silencer07

Ok figured out that i can do it with interval

let dragHoldInterval = null

....

onDrag={(event) => {
          .....
          clearInterval(dragHoldInterval)
          onDrag([
            horizontalDirection,
            verticalDirection
          ])
          dragHoldInterval = setInterval(() => {
            onDrag([
              horizontalDirection,
              verticalDirection
            ])
          }, 10)
        }}
        onDragRelease={() => clearInterval(dragHoldInterval)}

would be nice if this can be built-in to the framework

silencer07 avatar Aug 06 '20 04:08 silencer07