I am trying to use the PanResponder in React Native.
Very simple - I have 2 elements:
I want to effectively be able to drag the circle across the screen. Could someone help me out?
Heres is my code thus far :
import React from 'react';
import { Animated, Dimensions, PanResponder, StyleSheet, View } from 'react-native';
export class MySlider extends React.Component {
constructor(props) {
super(props);
this.state = {
pan: new Animated.ValueXY()
};
}
_panResponder = PanResponder.create({
// Ask to be the responder:
onStartShouldSetPanResponder: (evt, gestureState) => true,
onStartShouldSetPanResponderCapture: (evt, gestureState) => true,
onMoveShouldSetPanResponder: (evt, gestureState) => true,
onMoveShouldSetPanResponderCapture: (evt, gestureState) => true,
onPanResponderGrant: (evt, gestureState) => {
this.state.pan.setValue({x: 0, y: 0});
return true
},
onPanResponderMove: (evt, gestureState) => {
Animated.event([null, {dx: this.state.pan.x, dy: this.state.pan.y}])
return true
},
onPanResponderTerminationRequest: (evt, gestureState) => true,
onPanResponderRelease: (evt, gestureState) => {
return true
},
onPanResponderTerminate: (evt, gestureState) => {
return true
},
onShouldBlockNativeResponder: (evt, gestureState) => {
return true;
},
});
render() {
let { pan } = this.state;
let [translateX, translateY] = [pan.x, pan.y];
const styles = StyleSheet.create({
mover: {
transform: [{translateX:translateX}, {translateY:5}],
height: 50,
width: 50,
borderRadius: 25,
backgroundColor: 'blue'
},
holder: {
height: 100,
width: Dimensions.get('window').width,
backgroundColor: 'yellow'
}
});
return (
<View style={styles.holder}>
<Animated.View style={styles.mover} {...this._panResponder.panHandlers}/>
</View>
)
}
}
i changed your code a bit so try this
import React,{Component} from 'react' import { PanResponder, View, Animated, StyleSheet, Dimensions } from 'react-native' export default class MySlider extends React.Component { constructor(props) { super(props); this.state = { pan: new Animated.ValueXY() }; this._panResponder = PanResponder.create({ onStartShouldSetPanResponder : () => true, onPanResponderGrant: (e, gestureState) => { this.setState({isAddNewSession:true}) }, onPanResponderMove : Animated.event([null,{ //Step 3 dx : this.state.pan.x, dy : this.state.pan.y }]), onPanResponderRelease : (e, gesture) => { this.setState({isAddNewSessionModal:true}, ) Animated.spring( this.state.pan, {toValue:{x:0,y:0}} //To default position you can delete this animated.spring() ).start(); this.setState({isAddNewSession:false}) }, }); } render() { let { pan } = this.state; let [translateX, translateY] = [pan.x, pan.y]; const styles = StyleSheet.create({ mover: { transform: [{translateX:translateX}, {translateY:translateY}], height: 50, width: 50, position:'absolute', top:0, borderRadius: 25, zIndex:20, backgroundColor: 'blue' }, holder: { height: 100, width: Dimensions.get('window').width, backgroundColor: 'yellow' } }); return ( <View style={styles.holder}> <Animated.View style={styles.mover} {...this._panResponder.panHandlers}/> </View> ) } }
let me know if its work or not...
Yes that worked. I copied and pasted your class in place of my class and now it works. I can drag it in two dimensions and when I release it goes back to where it began. Thank you.
what is this.setState({isAddNewSession:true})
, this.setState({isAddNewSessionModal:true})
and this.setState({isAddNewSession:false})
??
Also, when I delete the Animated.Spring, I can drag and drop the circle - but when I drag it a second time, The circle begins from the original spot. Any suggestions on how to pick up where I left off? I tried replacing dx and dy with moveX and moveY but it hasn't worked. I also tried to store the old value in a variable but it is complicated because it is an Animated value and a javascript object.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.