Created
January 17, 2018 04:01
-
-
Save khalid32/b6dccebdbe29f0ea391d92752591f91b to your computer and use it in GitHub Desktop.
example of PanResponder(ES6 format) from http://browniefed.com/blog/react-native-animated-api-with-panresponder/
This file contains hidden or 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, { Component } from 'react'; | |
| import { | |
| AppRegistry, | |
| StyleSheet, | |
| View, | |
| Animated, | |
| PanResponder | |
| } from 'react-native'; | |
| const SQUARE_DIMENSIONS = 100; | |
| export default class AnimatedFlick extends Component{ | |
| constructor(){ | |
| super(); | |
| this.state = { | |
| pan: new Animated.ValueXY() | |
| }; | |
| } | |
| componentWillMount(){ | |
| this._animatedValueX = 0; | |
| this._animatedValueY = 0; | |
| this.state.pan.x.addListener((value) => this._animatedValueX = value.value); | |
| this.state.pan.y.addListener((value) => this._animatedValueY = value.value); | |
| this._panResponder = PanResponder.create({ | |
| onMoveShouldSetResponderCapture: () => true, | |
| onMoveShouldSetPanResponderCapture: () => true, | |
| onPanResponderGrant: (e, gestureState) => { | |
| this.state.pan.setOffset({x: this._animatedValueX, y: this._animatedValueY}); | |
| this.state.pan.setValue({x: 0, y: 0}); | |
| }, | |
| onPanResponderMove: Animated.event([ | |
| null, {dx: this.state.pan.x, dy: this.state.pan.y}, | |
| ]), | |
| onPanResponderRelease: () => { | |
| Animated.spring(this.state.pan, { | |
| toValue: 0 | |
| }).start(); | |
| } | |
| }); | |
| } | |
| componentWillUnmount(){ | |
| this.state.pan.x.removeAllListeners(); | |
| this.state.pan.y.removeAllListeners(); | |
| } | |
| getStyle = () => { | |
| return [ | |
| styles.square, | |
| { | |
| transform: [ | |
| { translateX: this.state.pan.x }, | |
| { translateY: this.state.pan.y }, | |
| { rotate: this.state.pan.x.interpolate({ | |
| inputRange: [-200, 0, 200], | |
| outputRange: ["-30deg", "0deg", "30deg"] | |
| }) | |
| } | |
| ] | |
| }, | |
| { | |
| opacity: this.state.pan.x.interpolate({ | |
| inputRange: [-200, 0, 200], | |
| outputRange: [0.5, 1, 0.5] | |
| }) | |
| } | |
| ]; | |
| } | |
| render(){ | |
| return ( | |
| <View style={styles.container}> | |
| <Animated.View | |
| style={this.getStyle()} | |
| {...this._panResponder.panHandlers} | |
| /> | |
| </View> | |
| ); | |
| } | |
| } | |
| const styles = StyleSheet.create({ | |
| container: { | |
| flex: 1, | |
| alignItems: 'center', | |
| justifyContent: 'center' | |
| }, | |
| square: { | |
| width: SQUARE_DIMENSIONS, | |
| height: SQUARE_DIMENSIONS, | |
| backgroundColor: 'blue' | |
| } | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment