Advertisement
Guest User

app.js

a guest
Feb 18th, 2020
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /* eslint-disable react-hooks/exhaustive-deps */
  2. /* eslint-disable react-native/no-inline-styles */
  3. import React from 'react';
  4. import {
  5.   StyleSheet,
  6.   ScrollView,
  7.   View,
  8.   Animated,
  9.   Text,
  10.   Image,
  11.   Dimensions,
  12.   TouchableNativeFeedback,
  13.   SafeAreaView,
  14. } from 'react-native';
  15.  
  16. const IMAGES = [
  17.   {id: 1, url: require('./assets/images/one.png')},
  18.   {id: 2, url: require('./assets/images/two.png')},
  19.   {id: 3, url: require('./assets/images/three.png')},
  20.   {id: 4, url: require('./assets/images/four.png')},
  21. ];
  22.  
  23. const SCREEN_WIDTH = Dimensions.get('window').width;
  24. const SCREEN_HEIGHT = Dimensions.get('window').height;
  25.  
  26. const App = () => {
  27.   const allImages = [];
  28.   const positions = new Animated.ValueXY();
  29.   const dimensions = new Animated.ValueXY();
  30.   var viewRef = null;
  31.  
  32.   const [state, setState] = React.useState({
  33.     activeImage: null,
  34.   });
  35.  
  36.   const openImage = index => {
  37.     // we are going to store the image position, dimensions here
  38.     allImages[index].measure((x, y, width, height, pageX, pageY) => {
  39.       // we will store the old coords here...
  40.  
  41.       positions.setValue({
  42.         x: pageX,
  43.         y: pageY,
  44.       });
  45.       dimensions.setValue({
  46.         x: width,
  47.         y: height,
  48.       });
  49.     });
  50.  
  51.     setState({activeImage: IMAGES[index]});
  52.   };
  53.  
  54.   var animatedImageStyle = React.useRef({});
  55.  
  56.   React.useEffect(() => {
  57.     console.log('1 activeImage', state.activeImage);
  58.     if (state.activeImage) {
  59.       console.log(
  60.         '1 animation style hook called',
  61.         JSON.stringify(positions),
  62.         JSON.stringify(dimensions),
  63.       );
  64.       animatedImageStyle.current = {
  65.         width: dimensions.x,
  66.         height: dimensions.y,
  67.         top: positions.y,
  68.         left: positions.x,
  69.       };
  70.     }
  71.   }, [positions.x, dimensions.x]);
  72.  
  73.   React.useEffect(() => {
  74.     // this is ref to our destination view
  75.     console.log('2 activeImage', state.activeImage);
  76.     if (state.activeImage) {
  77.       console.log(
  78.         '2 animation hook is being called',
  79.         JSON.stringify(positions),
  80.         JSON.stringify(dimensions),
  81.       );
  82.       // viewRef.measure((x, y, width, height, pageX, pageY) => {
  83.       Animated.parallel([
  84.         Animated.timing(positions.x, {
  85.           toValue: 0,
  86.           duration: 1000,
  87.         }),
  88.         Animated.timing(positions.y, {
  89.           toValue: 0,
  90.           duration: 1000,
  91.         }),
  92.         Animated.timing(dimensions.x, {
  93.           toValue: SCREEN_WIDTH,
  94.           duration: 1000,
  95.         }),
  96.         Animated.timing(dimensions.y, {
  97.           toValue: (SCREEN_HEIGHT * 2) / 3,
  98.           duration: 1000,
  99.         }),
  100.       ]).start(() => {
  101.         console.log('3 transformation ended');
  102.       });
  103.     }
  104.     // });
  105.   }, [positions.x, dimensions.x]);
  106.  
  107.   console.log('NONE root', animatedImageStyle.current);
  108.  
  109.   return (
  110.     <SafeAreaView style={{flex: 1}}>
  111.       <ScrollView style={{flex: 1}}>
  112.         {IMAGES.map((image, index) => (
  113.           <TouchableNativeFeedback
  114.             key={image.id}
  115. ===================================================
  116.             onPress={() => openImage(index)}>
  117.             <View style={styles.imageView}>
  118.               <Image
  119.                 ref={image => (allImages[index] = image)}
  120.                 source={image.url}
  121.                 style={{
  122.                   flex: 1,
  123.                   height: null,
  124.                   width: null,
  125.                   borderRadius: 20,
  126.                   resizeMode: 'cover',
  127.                 }}
  128.               />
  129.             </View>
  130.           </TouchableNativeFeedback>
  131.         ))}
  132.       </ScrollView>
  133.       <View
  134.         style={[
  135.           StyleSheet.absoluteFill,
  136.           {backgroundColor: 'rgba(240,240,240,0.7)'},
  137.         ]}
  138.         pointerEvents={state.activeImage ? 'auto' : 'none'}>
  139.         <View style={{flex: 2}} ref={view => (viewRef = view)}>
  140.           <Animated.Image
  141.             source={state.activeImage ? state.activeImage.url : null}
  142.             style={[
  143.               {
  144.                 width: null,
  145.                 height: null,
  146.                 top: 0,
  147.                 left: 0,
  148.                 // borderRadius: 20,
  149.                 resizeMode: 'cover',
  150.               },
  151. ===================================================
  152.               {...animatedImageStyle.current},
  153.             ]}
  154.           />
  155.         </View>
  156.         <View style={{flex: 1}}></View>
  157.       </View>
  158.     </SafeAreaView>
  159.   );
  160. };
  161.  
  162. const styles = StyleSheet.create({
  163.   container: {
  164.     flex: 1,
  165.     justifyContent: 'center',
  166.     alignItems: 'center',
  167.   },
  168.   imageView: {
  169.     height: SCREEN_HEIGHT - 100,
  170.     width: SCREEN_WIDTH,
  171.     padding: 10,
  172.   },
  173. });
  174.  
  175. export default App;
  176.  
  177.  
  178. // OUTPUT
  179.  
  180. NONE root {}
  181.  LOG  1 activeImage null
  182.  LOG  2 activeImage null
  183.  LOG  NONE root {}
  184.  LOG  1 activeImage {"id": 2, "url": 2}
  185.  LOG  1 animation style hook called {"x":0,"y":0} {"x":0,"y":0}
  186.  LOG  2 activeImage {"id": 2, "url": 2}
  187.  LOG  2 animation hook is being called {"x":0,"y":0} {"x":0,"y":0}
  188.  LOG  3 transformation ended
  189.  
  190. // EXPECTED
  191.  
  192. ...
  193.  LOG  1 animation style hook called {"x":123123,"y":123123} {"x":123123,"y":123123}
  194.  LOG  2 activeImage {"id": 2, "url": 2}
  195.  LOG  2 animation hook is being called {"x":123123,"y":123123} {"x":123123,"y":123123}
  196. ...
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement