Created
May 17, 2020 17:02
-
-
Save shafayeatsumit/bb6c3c1b9f27daf3ffe3fa6670cb56d4 to your computer and use it in GitHub Desktop.
panresponder
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, {useRef} from 'react'; | |
import {Animated, View, StyleSheet, PanResponder, Text} from 'react-native'; | |
export default function App() { | |
const pan = useRef(new Animated.ValueXY()).current; | |
const panResponder = useRef( | |
PanResponder.create({ | |
onStartShouldSetPanResponder: () => { | |
console.log('start'); | |
return true; | |
}, | |
onPanResponderGrant: () => { | |
pan.setOffset({ | |
x: pan.x._value, | |
y: pan.y._value, | |
}); | |
}, | |
onStartShouldSetPanResponderCapture: () => { | |
console.log('start capture'); | |
return true; | |
}, | |
onMoveShouldSetPanResponder: () => { | |
console.log('move'); | |
return true; | |
}, | |
onMoveShouldSetPanResponderCapture: () => { | |
console.log('move capture'); | |
return true; | |
}, | |
onPanResponderMove: Animated.event([null, {dx: pan.x, dy: pan.y}]), | |
onPanResponderRelease: () => { | |
pan.flattenOffset(); | |
}, | |
}), | |
).current; | |
return ( | |
<View style={styles.container}> | |
<Text style={styles.titleText}>Drag this box!</Text> | |
<Animated.View | |
style={{ | |
transform: [{translateX: pan.x}, {translateY: pan.y}], | |
}} | |
{...panResponder.panHandlers}> | |
<View style={styles.box} /> | |
</Animated.View> | |
</View> | |
); | |
} | |
const styles = StyleSheet.create({ | |
container: { | |
flex: 1, | |
alignItems: 'center', | |
justifyContent: 'center', | |
}, | |
titleText: { | |
fontSize: 14, | |
lineHeight: 24, | |
fontWeight: 'bold', | |
}, | |
box: { | |
height: 150, | |
width: 150, | |
backgroundColor: 'blue', | |
borderRadius: 5, | |
}, | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
circle expand and shrink logic