Skip to content

Instantly share code, notes, and snippets.

@andigu
Created September 6, 2017 04:16
Show Gist options
  • Save andigu/907cc3741320ad581be3393dbe921ee6 to your computer and use it in GitHub Desktop.
Save andigu/907cc3741320ad581be3393dbe921ee6 to your computer and use it in GitHub Desktop.
export class SwipeableCard extends Component {
translateX = new Animated.Value(0);
_panResponder = PanResponder.create({
onMoveShouldSetResponderCapture: () => true,
onMoveShouldSetPanResponderCapture: () => true,
onPanResponderMove: Animated.event([null, {dx: this.translateX}]),
onPanResponderRelease: (e, {vx, dx}) => {
const screenWidth = Dimensions.get("window").width;
if (Math.abs(vx) >= 0.5 || Math.abs(dx) >= 0.5 * screenWidth) {
Animated.timing(this.translateX, {
toValue: dx > 0 ? screenWidth : -screenWidth,
duration: 200
}).start(this.props.onDismiss);
} else {
Animated.spring(this.translateX, {
toValue: 0,
bounciness: 10
}).start();
}
}
});
render() {
return (
<View>
<Animated.View
style={{transform: [{translateX: this.translateX}], height: 75}} {...this._panResponder.panHandlers}>
<Card>
<CardItem>
<Body>
<Text>
{this.props.title}
</Text>
</Body>
</CardItem>
</Card>
</Animated.View>
</View>
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment