With the increased number of applications on Google Play Store and App Store, it has become an extremely difficult task for mobile app developers to get good ratings on their months of hard work. Users will normally not think for a second before uninstalling your application if your app fails to provide necessary functionality and at the same time be pleasing for their eye.
In enhancing the user interface and user experience of an app, Animations play a great role. The appropriate use of UI animations can help decrease cognitive load, draw and direct user’s attention, and create an experience easier to grasp.
USING LOTTIE FOR ANIMATIONS:
Lottie is a iOS, android and React Native library by Airbnb that enables you to embed highly performant JSON based Adobe AfterEffects animations into your mobile app that renders After Effects animations in real time. Lottie makes it easier to render vector based animations and art in realtime with minimal code. You can find more here.
USING LOTTIE IN REACT-NATIVE:
Using Lottie in react-native is particularly very easy and straight forward.
You can use these animations very easily in your React Native app thanks to airbnb. The repo and official docs can be found at link below:
https://github.com/react-native-community/lottie-react-native
This tutorial uses the following versions:
- React Native: 0.63
- Lottie-ios: 3.1.8,
- Lottie-react-native: 3.5.0
- Node v14.7.0
Throughout this tutorial, I am using NPM but feel free to use Yarn instead.
Create new React Native App:
- npx react-native init LottieAnimationApp
- cd LottieAnimationApp
- npm install
Installing Lottie (React Native >= 0.60.0):
- npm i –save lottie-react-native
- npm i –save lottie-ios@3.1.8
- cd ios & pod install
- Go back to root and npx react-native run-ios
*IMPORTANT: If you face linking issue, check the troubleshooting guide at https://github.com/react-native-community/lottie-react-native
Using Lottie in React Native:
For this demo, I’ve downloaded animations as Lottie JSON from https://lottiefiles.com/ and place the downloaded JSON in the project structure as shown.
Next, go App.js and replace the contents with to get the following output:
import React from 'react'; import {StyleSheet, TouchableOpacity, View, Text} from 'react-native'; import LottieView from 'lottie-react-native'; class App extends React.Component { state = { animationPlaying: false, showEat: false, showSleep: false, showCode: false, showRepeat: false, }; eatPressed = () => { this.setState({ animationPlaying: true, showEat: true, showSleep: false, showCode: false, showRepeat: false, }); }; sleepPressed = () => { this.setState({ animationPlaying: true, showEat: false, showSleep: true, showCode: false, showRepeat: false, }); }; codePressed = () => { this.setState({ animationPlaying: true, showEat: false, showSleep: false, showCode: true, showRepeat: false, }); }; repeatPressed = () => { this.setState({ animationPlaying: true, showEat: false, showSleep: false, showCode: false, showRepeat: true, }); }; animationFinished = () => { console.log("Finished") this.setState({ animationPlaying: false, welcomeAnimationShown: true, buttonText: "Let's Celebrate", }); }; render() { return ( <View style={styles.mainContainer}> {this.state.animationPlaying ? ( <View style={styles.mainContainer}> {this.state.showEat && ( <LottieView ref={(animation) => { this.animation = animation; }} autoPlay loop={false} source={require('./animations/eat.json')} onAnimationFinish={this.animationFinished} /> )} {this.state.showSleep && ( <LottieView ref={(animation) => { this.animation = animation; }} autoPlay loop={false} source={require('./animations/sleep.json')} onAnimationFinish={this.animationFinished} /> )} {this.state.showCode && ( <LottieView ref={(animation) => { this.animation = animation; }} autoPlay loop={false} source={require('./animations/code.json')} onAnimationFinish={this.animationFinished} /> )} {this.state.showRepeat && ( <LottieView ref={(animation) => { this.animation = animation; }} autoPlay loop={false} source={require('./animations/repeat.json')} onAnimationFinish={this.animationFinished} /> )} </View> ) : ( <View> <TouchableOpacity style={styles.animateButton} onPress={this.eatPressed}> <Text style={styles.animateText}>Eat</Text> </TouchableOpacity> <TouchableOpacity style={styles.animateButton} onPress={this.sleepPressed}> <Text style={styles.animateText}>Sleep</Text> </TouchableOpacity> <TouchableOpacity style={styles.animateButton} onPress={this.codePressed}> <Text style={styles.animateText}>Code</Text> </TouchableOpacity> <TouchableOpacity style={styles.animateButton} onPress={this.repeatPressed}> <Text style={styles.animateText}>Repeat</Text> </TouchableOpacity> </View> )} </View> ); } } export default App; const styles = StyleSheet.create({ mainContainer: { justifyContent: 'center', flex: 1, }, animateButton: { height: 60, backgroundColor: '#000', marginHorizontal: 32, borderRadius: 20, justifyContent: 'center', alignItems: 'center', marginBottom: 20, }, animateText: { fontSize: 22, color: '#fff', }, });
You’ll see the following output:
You can find the full list of props and methods available for LottieView in the API document.