Advertisement
loloof64

React Native : dragging and animation to original place

Oct 27th, 2017
369
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import React, { Component } from 'react';
  2. import { StyleSheet, View, Animated, PanResponder, Easing } from 'react-native';
  3. import _ from 'underscore';
  4.  
  5. class Square {
  6.     constructor(value, origin, cellsSize) {
  7.         this.value = value;
  8.         this.pan = new Animated.ValueXY();
  9.         this.cellsSize = cellsSize;
  10.         this.boardSize = 3 * this.cellsSize;
  11.         this.minXY = this.cellsSize * (0.5);
  12.         this.maxXY = this.cellsSize * (1.5);
  13.         this.midXY = this.cellsSize;
  14.         this.origin = origin;
  15.         this.constrainedX = this.pan.x.interpolate({
  16.             inputRange: [this.minXY, this.midXY, this.maxXY],
  17.             outputRange: [this.minXY, this.midXY, this.maxXY],
  18.             extrapolate: 'clamp',
  19.         });
  20.         this.constrainedY = this.pan.y.interpolate({
  21.             inputRange: [this.minXY, this.midXY, this.maxXY],
  22.             outputRange: [this.minXY, this.midXY, this.maxXY],
  23.             extrapolate: 'clamp',
  24.         });
  25.  
  26.         const x = parseInt(this.cellsSize * (0.5 + this.origin.file));
  27.         const y = parseInt(this.cellsSize * (0.5 + this.origin.rank));
  28.  
  29.         this.pan.setValue({ x, y });
  30.         this.panResponder = this._buildPanResponder();
  31.     }
  32.  
  33.     get valueString() {
  34.         return this.value;
  35.     }
  36.  
  37.     get panRef() {
  38.         return this.pan;
  39.     }
  40.  
  41.     get panResponderRef() {
  42.         return this.panResponder;
  43.     }
  44.  
  45.     _buildPanResponder() {
  46.         return PanResponder.create({
  47.             onStartShouldSetPanResponder: () => true,
  48.             onPanResponderGrant: (event, gestureState) => {
  49.                 this.pan.setOffset({ x: this.pan.x._value, y: this.pan.y._value });
  50.             },
  51.             onPanResponderMove: (event, gestureState) => {
  52.                 this.pan.setValue({ x: gestureState.dx, y: gestureState.dy });
  53.             },
  54.             onPanResponderRelease: (event, gesture) => {
  55.                 const nativeEvent = event.nativeEvent;
  56.  
  57.                 const origX = parseInt(this.cellsSize * (this.origin.file + 0.5));
  58.                 const origY = parseInt(this.cellsSize * (this.origin.rank + 0.5));
  59.  
  60.                 Animated.timing(
  61.                     this.pan,
  62.                     {
  63.                         toValue: { x: origX, y: origY },
  64.                         duration: 400,
  65.                         delay: 0,
  66.                         easing: Easing.linear
  67.                     }
  68.                 ).start();
  69.  
  70.                 this.pan.flattenOffset()
  71.             }
  72.         });
  73.     }
  74. }
  75.  
  76. export default class TestComponent extends Component {
  77.  
  78.     constructor(props) {
  79.         super(props);
  80.  
  81.         this.cellsSize = 100;
  82.  
  83.         this.squares = [
  84.             new Square('red', { file: 1, rank: 0 }, this.cellsSize),
  85.             new Square('green', { file: 0, rank: 1 }, this.cellsSize),
  86.             new Square('blue', { file: 1, rank: 1 }, this.cellsSize),
  87.         ];
  88.     }
  89.  
  90.     renderACoin(value, file, rank) {
  91.         if (value) {
  92.             let style;
  93.             switch (value.valueString) {
  94.                 case 'red': style = styles.redCoin; break;
  95.                 case 'green': style = styles.greenCoin; break;
  96.                 case 'blue': style = styles.blueCoin; break;
  97.             }
  98.  
  99.             const randomKey = parseInt(Math.random() * 1000000).toString()
  100.  
  101.             return (
  102.                 <Animated.View key={randomKey} style={StyleSheet.flatten([style,
  103.                     {
  104.                         left: value.constrainedX,
  105.                         top: value.constrainedY,
  106.                     }])}
  107.                     {...value.panResponderRef.panHandlers }
  108.                 />
  109.             );
  110.         }
  111.     }
  112.  
  113.     renderAllCoins() {
  114.         return _.map(this.squares, (currSquare) => {
  115.             return this.renderACoin(currSquare, currSquare.origin.file, currSquare.origin.rank);
  116.         });
  117.     }
  118.  
  119.     render() {
  120.  
  121.         return (
  122.             <View style={styles.topLevel}>
  123.                 <View style={StyleSheet.flatten([styles.board])}
  124.                     ref="boardRoot"
  125.                 >
  126.                     <View style={StyleSheet.flatten([styles.whiteCell, {
  127.                         left: 50,
  128.                         top: 50,
  129.                     }])} />
  130.                     <View style={StyleSheet.flatten([styles.blackCell, {
  131.                         left: 150,
  132.                         top: 50,
  133.                     }])} />
  134.                     <View style={StyleSheet.flatten([styles.blackCell, {
  135.                         left: 50,
  136.                         top: 150,
  137.                     }])} />
  138.                     <View style={StyleSheet.flatten([styles.whiteCell, {
  139.                         left: 150,
  140.                         top: 150,
  141.                     }])} />
  142.  
  143.                     {this.renderAllCoins()}
  144.  
  145.                 </View>
  146.             </View>
  147.         );
  148.     }
  149. }
  150.  
  151. const styles = StyleSheet.create({
  152.     topLevel: {
  153.         backgroundColor: "#CCFFCC",
  154.         flex: 1,
  155.         justifyContent: 'center',
  156.         alignItems: 'center',
  157.         flexDirection: 'row',
  158.     },
  159.     board: {
  160.         width: 300,
  161.         height: 300,
  162.         backgroundColor: "#FFCCFF",
  163.     },
  164.     whiteCell: {
  165.         width: 100,
  166.         height: 100,
  167.         backgroundColor: '#FFAA22',
  168.         position: 'absolute',
  169.     },
  170.     blackCell: {
  171.         width: 100,
  172.         height: 100,
  173.         backgroundColor: '#221122',
  174.         position: 'absolute',
  175.     },
  176.     greenCoin: {
  177.         width: 100,
  178.         height: 100,
  179.         position: 'absolute',
  180.         backgroundColor: '#23CC12',
  181.         borderRadius: 50,
  182.     },
  183.     redCoin: {
  184.         width: 100,
  185.         height: 100,
  186.         position: 'absolute',
  187.         backgroundColor: '#FF0000',
  188.         borderRadius: 50,
  189.     },
  190.     blueCoin: {
  191.         width: 100,
  192.         height: 100,
  193.         position: 'absolute',
  194.         backgroundColor: '#0000FF',
  195.         borderRadius: 50,
  196.     },
  197. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement