From 46f65366d3f4176ac9b33f23d13716656f6d55ec Mon Sep 17 00:00:00 2001 From: Meng To Date: Sat, 11 May 2019 08:16:12 -0400 Subject: [PATCH] Looping the Cards --- screens/ProjectsScreen.js | 74 ++++++++++++++++++++++++++++++++------- 1 file changed, 62 insertions(+), 12 deletions(-) diff --git a/screens/ProjectsScreen.js b/screens/ProjectsScreen.js index 36280db..d2fc793 100644 --- a/screens/ProjectsScreen.js +++ b/screens/ProjectsScreen.js @@ -3,6 +3,14 @@ import styled from "styled-components"; import Project from "../components/Project"; import { PanResponder, Animated } from "react-native"; +function getNextIndex(index) { + var nextIndex = index + 1; + if (nextIndex > projects.length - 1) { + return 0; + } + return nextIndex; +} + class ProjectsScreen extends React.Component { static navigationOptions = { header: null @@ -11,28 +19,44 @@ class ProjectsScreen extends React.Component { state = { pan: new Animated.ValueXY(), scale: new Animated.Value(0.9), - translateY: new Animated.Value(44) + translateY: new Animated.Value(44), + thirdScale: new Animated.Value(0.8), + thirdTranslateY: new Animated.Value(-50), + index: 0 }; componentWillMount() { this._panResponder = PanResponder.create({ + onMoveShouldSetPanResponder: () => true, + onPanResponderGrant: () => { Animated.spring(this.state.scale, { toValue: 1 }).start(); Animated.spring(this.state.translateY, { toValue: 0 }).start(); + + Animated.spring(this.state.thirdScale, { toValue: 0.9 }).start(); + Animated.spring(this.state.thirdTranslateY, { toValue: 44 }).start(); }, - onMoveShouldSetPanResponder: () => true, + onPanResponderMove: Animated.event([ null, { dx: this.state.pan.x, dy: this.state.pan.y } ]), + onPanResponderRelease: () => { const positionY = this.state.pan.y.__getValue(); // console.log(positionY); if (positionY > 200) { Animated.timing(this.state.pan, { - toValue: { x: this.state.pan.x, y: 1000 } - }).start(); + toValue: { x: 0, y: 1000 } + }).start(() => { + this.state.pan.setValue({ x: 0, y: 0 }); + this.state.scale.setValue(0.9); + this.state.translateY.setValue(44); + this.state.thirdScale.setValue(0.8); + this.state.thirdTranslateY.setValue(-50); + this.setState({ index: getNextIndex(this.state.index) }); + }); } else { Animated.spring(this.state.pan, { toValue: { x: 0, y: 0 } @@ -40,6 +64,9 @@ class ProjectsScreen extends React.Component { Animated.spring(this.state.scale, { toValue: 0.9 }).start(); Animated.spring(this.state.translateY, { toValue: 44 }).start(); + + Animated.spring(this.state.thirdScale, { toValue: 0.8 }).start(); + Animated.spring(this.state.thirdTranslateY, { toValue: -50 }).start(); } } }); @@ -58,10 +85,10 @@ class ProjectsScreen extends React.Component { {...this._panResponder.panHandlers} > + + +