Skip to content

Instantly share code, notes, and snippets.

@talkol
Last active June 18, 2016 18:50
Show Gist options
  • Select an option

  • Save talkol/d4d41be20cdd2a0c5eaee3beeeb7492b to your computer and use it in GitHub Desktop.

Select an option

Save talkol/d4d41be20cdd2a0c5eaee3beeeb7492b to your computer and use it in GitHub Desktop.
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