Last active
June 18, 2016 18:50
-
-
Save talkol/d4d41be20cdd2a0c5eaee3beeeb7492b to your computer and use it in GitHub Desktop.
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 { PanResponder, View, Dimensions } from 'react-native'; | |
| const dragOffsetForTransparency = 0.8 * Dimensions.get('window').width; | |
| export default class Swipeable extends Component { | |
| constructor(props) { | |
| super(props); | |
| this.panResponder = PanResponder.create({ | |
| onStartShouldSetPanResponder: (evt, gestureState) => true, | |
| onPanResponderMove: (evt, gestureState) => { | |
| this.updateStyleBasedOnDeltaX(gestureState.dx); | |
| }, | |
| onPanResponderRelease: (evt, gestureState) => { | |
| this.updateStyleBasedOnDeltaX(0); | |
| } | |
| }); | |
| } | |
| updateStyleBasedOnDeltaX(dx) { | |
| let opacity = 1 - Math.abs(dx) / dragOffsetForTransparency; | |
| if (opacity < 0) opacity = 0; | |
| this.setContainerStyles(opacity, dx); | |
| } | |
| // notice that the main change is in this function | |
| setContainerStyles(opacity, translateX) { | |
| this.refs['container'].setNativeProps({style: { | |
| opacity: opacity, | |
| transform: [{translateX: translateX}] | |
| }}); | |
| } | |
| render() { | |
| return ( | |
| <View {...this.panResponder.panHandlers} ref='container'> | |
| {this.props.children} | |
| </View> | |
| ); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment