3.3.0 • Published 4 years ago

react-native-draggable v3.3.0

Weekly downloads
3,060
License
MIT
Repository
github
Last release
4 years ago

react-native-draggable

Build Status npm version

NPM

UPDATE DEC 2019 (v3.0.0) - This repo has just been completely refreshed and contains very different functionality, please see the new props and usage below

Draggable item for react-native!

npm install react-native-draggable
import Draggable from 'react-native-draggable';

How to use

return (
    <View >
        <Draggable x={75} y={100} renderSize={56} renderColor='black' renderText='A' isCircle shouldReverse onShortPressRelease={()=>alert('touched!!')}/> 
        <Draggable x={200} y={300} renderColor='red' renderText='B'/>
        <Draggable/>
	<Draggable x={50} y={50}>
		<YourComponent/>
	</Draggable>
    </View>
);

Demo

N|Solid in my project =>

return (
    <View style={{backgroundColor: 'blue', flex: 1}} >
        <Draggable 
            imageSource={require('./trump1.png')} 
            renderSize={80} 
            x={200}
            y={300}
            onDragRelease={this._changeFace}
            onLongPress={()=>console.log('long press')}
            onShortPressRelease={()=>console.log('press drag')}
            onPressIn={()=>console.log('in press')}
            onPressOut={()=>console.log('out press')}
        />  
    </View>
);  

Event Demo

DEMO2

Version 3 Demo

DEMOV3

Props spec & Example

Properties

PropTypeExampleDefaultDescription
renderTextstring'ANY''+'text of draggable
isCirclebool{true}---render as circle
renderSizenumber{36}{36}draggable size
imageSourcesourcerequire('./img/xxx.png')---image source
renderColorstring'black'---Colors
childrenComponent<Text>Sup</Text>---children to render as draggable
shouldReversebool{false}{false}should draggable spring back to start when released
disabledbool{false}{false}should draggable be disabled
debugbool{false}{false}should show a debug visualization
touchableOpacityPropsObject{ activeOpactiy: .1 }---props passed to TouchableOpacity component
animatedViewPropsObject{ accessibilityHint: 'drag' }---props passed to Animated.View component
xnumber{0}0initial position x
ynumber{0}0initial position y
znumber{1}1z-index / elevation
minXnumber{0}---min X value for left edge of component
minYnumber{0}---min Y value for top edge of component
maxXnumber{0}---max X value for right edge of component
maxYnumber{0}---max Y value for bottom edge of component

Events

EventTypeArgumentsDescription
onDragfuncevent, gestureStatecalled every frame component is moved
onShortPressReleasefunceventcalled when a press is released that isn't a long press or drag
onDragReleasefuncevent, gestureState, boundscalled when a drag is released
onLongPressfunceventcalled when a long press is started
onPressInfunceventcalled when a press is started
onPressOutfunceventcalled when a press is stopped, or the component is dragged
onReleasefuncevent, wasDraggingcalled at the end of interaction, regardless if press or drag
 onReverse  func   called when a drag is released, if shouldReverse is true 

Arguments (event, gestureState)

event

ArgumentDescription
changedTouchesArray of all touch events that have changed since the last event
identifierThe ID of the touch
locationXThe X position of the touch, relative to the element
locationYThe Y position of the touch, relative to the element
pageXThe X position of the touch, relative to the root element
pageYThe Y position of the touch, relative to the root element
targetThe node id of the element receiving the touch event
timestampA time identifier for the touch, useful for velocity calculation
touchesArray of all current touches on the screen

gestureState

ArgumentDescription
stateIDID of the gestureState- persisted as long as there at least one touch on screen
moveXthe latest screen coordinates of the recently-moved touch
moveYthe latest screen coordinates of the recently-moved touch
x0the screen coordinates of the responder grant
y0the screen coordinates of the responder grant
dxaccumulated distance of the gesture since the touch started
dyaccumulated distance of the gesture since the touch started
vxcurrent velocity of the gesture
vycurrent velocity of the gesture
numberActiveTouchesNumber of touches currently on screen
gestureStatecalled at the end of interaction, regardless if press or drag

bounds

ArgumentDescription
leftas x at the top left corner
topas y at the top left corner
rightas x at the bottom right corner
bottomas y at the bottom right corner

Methods (not supported above V2.0.0)

MethodParamsDescription 
reversePosition---use onReverse callback instead. manually reset Draggable to start position
getPosition---use onDragRelease callback instead. get the accurate coordinates x,y from the bounds

What's next?

This Draggable is used to be a Draggable Button in my project. Let me know if you have any idea or demand, let's discuss and develop it.