DEV Community

Cover image for React Native Apple App of the day Animation #1 : Setup UI
kris
kris

Posted on • Originally published at kriss.io on

React Native Apple App of the day Animation #1 : Setup UI

In this tutorial, we are going to create React Native Shared Element Transition by replicating Apple app of the day as an example. This tutorial will address an interesting aspect of using React Animation and Dimensions to making images look cool. Here, we are not going to use any external react plugins. All the required components like Image, Animated, Dimensions are provided by the react-native package.

The idea is to start with simple React Native project. Then, we are going to add images and wrap the images with different components from the react-native package and configure it to implement react-native shared element transition.

So, let’s get started!!

Start with an empty project

First, we are going to create a starting boilerplate React Native application. For that, we need to run following commands into our desired directory:

>>react-native init <example-app>

After the installation of the blank react-native application is complete, we are going to run the app in our iOS emulator by using the following command:

>>>react-native run-ios

As a result, we will get the following result in our emulator screen:

Adding Image and configuring it

Now, we are going to add images to our app. For that, we need to remove all unnecessary code from our App.js file. Then, we need to add the following code to our App.js file. The images are imported from the image directory. We can create our own image directory into our project source directory, add our own images and then import them in an array constant as given in the code snippet below:

import React from 'react';import {View} from 'react-native';const images = [{id: 1, src: require('./image/1.jpeg')}, {id: 2, src: require('./image/2.jpeg')}, {id: 3, src: require('./image/3.jpeg')}, {id: 4, src: require('./image/4.jpeg')},];export default class App extends React.Component { render() { return <View></View>; }}

In the code snippet, above we have only included the images constant and a View component from the react-native package. Hence, the app will appear blank in the emulator if we re-run it.

So, in order to add the imported images into our screen, we are going to replace the View component with ScrollView component so that we can view the images on the screen by scrolling up and down the screen. Then, we are going to map the images on to the screen by using map function which helps us iterate through the array and return the JSX elements. Here, the element we are returning from the map function is the Image component in which the imported images are integrated. For that, we can make use of the code provided in the following code snippet:

import React from 'react';import {Image, ScrollView} from 'react-native';const images = [{id: 1, src: require('./image/1.jpeg')}, {id: 2, src: require('./image/2.jpeg')}, {id: 3, src: require('./image/3.jpeg')}, {id: 4, src: require('./image/4.jpeg')},];export default class App extends React.Component { render() { return ( <ScrollView style={{flex: 1}}> {images.map(image => { return <Image key={image.id} source={image.src} />; })} </ScrollView> ); }}

As a result, we are going to get the following screen in our emulator:

As we can see in the emulator simulation above, the images appear on the screen and hence, are scrollable.

Wrapping the Image up

The next step is to wrap the Image component by using TouchableWithOutFeedBack and Animated View ** component in order to create an animation when touched. The *TouchableWithOutFeedBack * component is used to make the images clickable without providing any kind of feedback on the screen while the **Animated View component makes images animate with a proper view. Thus, the code to implement this is provided in the code snippet below:

import React from 'react';import { Image, ScrollView, TouchableWithoutFeedback, Animated,} from 'react-native';const images = [{id: 1, src: require('./image/1.jpeg')}, {id: 2, src: require('./image/2.jpeg')}, {id: 3, src: require('./image/3.jpeg')}, {id: 4, src: require('./image/4.jpeg')},];export default class App extends React.Component { render() { return ( <ScrollView style={{flex: 1}}> {images.map(image => { return ( <TouchableWithoutFeedback key={image.id}> <Animated.View> <Image source={image.src} /> </Animated.View> </TouchableWithoutFeedback> ); })} </ScrollView> ); }}

Adding Size configurations to Image with Dimensions component

In this step, we are going to configure the height and width of our images on the basis of screen size. In order to get the screen height and width, we are going to make use of Dimensions component provided by the react-native package. Dimensions component provides get() function through which we can get the full-screen window width and height. Here, we are going to store those screen window height and width into two variables namely SCREEN_WIDTH AND SCREEN_HEIGHT as shown in the code snippet below:

  Image, ScrollView, TouchableWithoutFeedback, Animated, Dimensions,} from 'react-native';let SCREEN\_WIDTH = Dimensions.get('window').width;let SCREEN\_HEIGHT = Dimensions.get('window').height;

Then, we are going to add those size style configurations to our Animated View component with height decreased by 150 as shown in the code snippet below:

<TouchableWithoutFeedback key={image.id}> <Animated.View style={{ height: SCREEN\_HEIGHT - 150, width: SCREEN\_WIDTH, padding: 15, }}> <Image source={image.src} /> </Animated.View></TouchableWithoutFeedback>

Therefore, we get the following result in our emulator screen:

Adding styles to Image

This step is pretty simple. We are just going to add some inline styles to our Image component in order to make it look more appealing. We are going to add the inline styles using style prop as shown in the code snippet below:

<TouchableWithoutFeedback key={image.id}> <Animated.View style={{ height: SCREEN\_HEIGHT - 150, width: SCREEN\_WIDTH, padding: 15, }}> <Image source={image.src} style={{ flex: 1, height: null, width: null, resizeMode: 'cover', borderRadius: 20, }} /> </Animated.View></TouchableWithoutFeedback>

Hence, we get the following result in our emulator screen where images appear more better than before.

But, as we can see, there is an overlay of emulator’s status bar over the app’s image.

So, in order to fix that we are going to add a SafeAreaView container. The function of SafeAreaView component is to render content within the safe area boundaries of a device. So, we are going to wrap the ScrollView component with SafeAreaView component as shown in the code snippet below:

import React from 'react';import { Image, ScrollView, TouchableWithoutFeedback, Animated, Dimensions, SafeAreaView,} from 'react-native';const images = [{id: 1, src: require('./image/1.jpeg')}, {id: 2, src: require('./image/2.jpeg')}, {id: 3, src: require('./image/3.jpeg')}, {id: 4, src: require('./image/4.jpeg')},];let SCREEN\_WIDTH = Dimensions.get('window').width;let SCREEN\_HEIGHT = Dimensions.get('window').height;export default class App extends React.Component { render() { return ( <SafeAreaView style={{flex: 1}}> <ScrollView style={{flex: 1}}> {images.map(image => { return ( <TouchableWithoutFeedback key={image.id}> <Animated.View style={{ height: SCREEN\_HEIGHT - 150, width: SCREEN\_WIDTH, padding: 15, }}> <Image source={image.src} style={{ flex: 1, height: null, width: null, resizeMode: 'cover', borderRadius: 20, }} /> </Animated.View> </TouchableWithoutFeedback> ); })} </ScrollView> </SafeAreaView> ); }}

Hence, we get the following result our emulator screen:

Finally, our implementation of React Native Shared Element Transition is successful.

Conclusion

In this first part of the tutorial, we learned how to set up the blank react native application. We also learned how to make use of different components from the react-native package. Moreover, we also got detail guidance of how to set up image UI for Animation and make use of Dimensions, ScrollView and SafeAriaView components. Lastly, we successfully completed our implementation of React Native shared Element transitions.

next chapter Here

React native Apple App of the day Animation #2 : Open Image

&lt;!--//--&gt;&lt;![CDATA[//&gt;&lt;!-- !function(a,b){&quot;use strict&quot;;function c(){if(!e){e=!0;var a,c,d,f,g=-1!==navigator.appVersion.indexOf(&quot;MSIE 10&quot;),h=!!navigator.userAgent.match(/Trident.*rv:11./),i=b.querySelectorAll(&quot;iframe.wp-embedded-content&quot;);for(c=0;c<i.length;c++){if(d=i[c],!d.getAttribute("data-secret"))f=Math.random().toString(36).substr(2,10),d.src+="#?secret="+f,d.setAttribute("data-secret",f);if(g||h)a=d.cloneNode(!0),a.removeAttribute("security"),d.parentNode.replaceChild(a,d)}}}var d=!1,e=!1;if(b.querySelector)if(a.addEventListener)d=!0;if(a.wp=a.wp||{},!a.wp.receiveEmbedMessage)if(a.wp.receiveEmbedMessage=function(c){var d=c.data;if(d)if(d.secret||d.message||d.value)if(!/[^a-zA-Z0-9]/.test(d.secret)){var e,f,g,h,i,j=b.querySelectorAll('iframe[data-secret="'+d.secret+'"]'),k=b.querySelectorAll('blockquote[data-secret="'+d.secret+'"]');for(e=0;e<k.length;e++)k[e].style.display="none";for(e=0;e<j.length;e++)if(f=j[e],c.source===f.contentWindow){if(f.removeAttribute("style"),"height"===d.message){if(g=parseInt(d.value,10),g>1e3)g=1e3;else if(~~g<200)g=200;f.height=g}if("link"===d.message)if(h=b.createElement("a"),i=b.createElement("a"),h.href=f.getAttribute("src"),i.href=d.value,i.host===h.host)if(b.activeElement===f)a.top.location.href=d.value}else;}},d)a.addEventListener("message",a.wp.receiveEmbedMessage,!1),b.addEventListener("DOMContentLoaded",c,!1),a.addEventListener("load",c,!1)}(window,document);//-->&lt;!]]&gt;

Credit

content from #1 Apple App of the day Animation by Unsureprogrammer

image from Unsplash

The post React Native Apple App of the day Animation #1 : Setup UI appeared first on Kriss.

Top comments (0)