SlideShare uma empresa Scribd logo
1 de 33
Baixar para ler offline
How to
Implement
React Native
Animations
Using Animated
API
www.bacancytechnology.com
Table of contents
1. Introduction
2. Tutorial Goal: Learn to Implement
React Native Animations
3. Working with React Native
Animations
4. Steps to Quickly Add Animations in
React Native App
5. Conclusion
Introduction
Animations are an essential part of a UX
of an application. Animations
significantly impact the user for a better
interaction experience and smoothen the
user engagement. Whenever there is an
extended functionality to perform, the
animation is a great idea to engage users.
Animation has a movement from the
static state to the state of motion for
better user interaction. Animations have
various transitions to show elements and
thus make it enjoyable.
You might be tempted to implement
React Native Animations using animated
API, and maybe you are searching for
how to get started with React Native
animations. Here is a step-by-step guide
on adding React Native animations from
scratch.
Tutorial
Goal: Learn
to
Implement
React Native
Animations
We will learn about how to get started
with React Native Animation using the
React Native’s Animated library. The
video is the final output of advanced
animations in React Native.
Watch Video
The main workflow is to create an
Animated Value and use it for different
components. We will be discussing the
following React Native Animation Types
also:
Please add some information to these
points as well
Animated.timing()
It allows us to define an animation to a
specific value over a certain amount of
time.
Animated.spring()
It allows us to define animation from
start points to endpoints without
defining time as we did in timing.
Animated.parallel()
It allows us to have all the defined
animations in the array to trigger at the
same time.
Animated.sequence()
It allows us to have all the defined
animations in the array to trigger one
after another.
Working
with React
Native
Animations
We will start the animation by using the
start() function to our animation-type
methods.
start() takes a completion callback called
when the animation task is completed,
creating an infinite animation.
Using start function with
Animated.timing() :-
Animated.timing(value,{}).start()
Animated.timing() will take two
parameters here, a value and an object.
The object can take values such as
toValue, duration, opacity, etc.
Another parameter we will be using is
useNativeDriver.The native driver helps
send out everything about the animation
to the native code before animation
starts, through which native code can
perform the Animation UI on the thread.
Use this in the animation configuration:
useNativeDriver: true
There are multiple React Native
Animatable components and only these
components can be animated.
Animated.View
Animated.Image
Animated.FlatList
Animated.SectionList
Animated.Text
Animated.ScrollView
Steps to
Quickly Add
Animations
in React
Native App
The entire source code is available here –
Github repo
Creating a React Native App
Initially, we will create a react native app
using:
react-native init Animation
Installing dependencies for Navigation
Install stack navigation for navigating
screens using:
npm install react-native-gesture-handler
react-native-safe-area-context @react-
navigation/native
@react-navigation/stack.
Creating Components
We will create basic animation screens such
as-
1. Fade Animation
2. Spin Animation
3. Scale Animation
4. Spring Animation
5. Parallel Animation
6. Sequence Animation
And a main.js file in which we will import all
these screens for navigating.
We will pass the type of animation of the
component in useEffect() as the argument,
so whenever we navigate to the screen
respective component is rendered
immediately based on the type.
Fade Animation
This screen will create an animation on the
image component, which fades in on render.
Create an Animated.Value() for animation
every time to make component animatable :
const Fade= new Animated.Value(0)
We will call Animated.timing() in
useEffect(). The purpose of
Animated.Timing() is to change the input
value to a specified output value after a
certain amount of time.
useEffect(()=>{
Animated.timing(Fade,{
toValue:1,
duration:3000,
useNativeDriver: true
}).start()
},[Fade])
Animated.Timing() takes the first argument
as the value which needs to be updated; a
second argument is an object containing
three values. The first value toValue:1
updates the value of Fade to 1. The second
argument duration:3000 is the specific
amount of time after which it should reflect
animation, and the third argument is
useNativeDriver: true, which I have
explained above.
< Animated.Image
style=
{{marginTop:5,height:200,width:200,opacity
:Fade,}}
source={{ uri:
"https://images.unsplash.com/photo-
1541963463532-d68292c34b19?ixlib=rb-
1.2.1&#038;ixid=MXwxMjA3fDB8MHxleHBs
b3JlLWZlZWR8M3x8fGVufDB8fHw%3D&#0
38;w=1000&#038;q=80" }}
>
< /Animated.Image >
opacity:Fade takes the value of Fade to
implement animation after the duration of
3000ms.
Spin Animation
Spin animation rotates and makes the
component spin according to the output
degree values.
const Spin= new Animated.Value(0)
const SpinValue= Spin.interpolate({
inputRange: [0, 1],
outputRange: ['0deg', '360deg']
})
Interpolation: Interpolation is a way to
estimate function at intermediate points
defined from input range and output range.
We can change colors, rotate and scale the
animation component using interpolation.
useEffect(()=>{
Animated.timing(Spin,{
toValue:1,
duration:3000,
useNativeDriver:true
}).start()
},[Spin])
< Animated.Image
style=
{{height:150,width:180,marginTop:8,borderR
adius:20,transform:[{rotate:SpinValue}]}}
source={{ uri:
"https://images.unsplash.com/photo-
1541963463532-d68292c34b19?ixlib=rb-
1.2.1&#038;ixid=MXwxMjA3fDB8MHxleHBs
b3JlLWZlZWR8M3x8fGVufDB8fHw%3D&#0
38;w=1000&#038;q=80" }}
>
< /Animated.Image >
We have used transform configuration in
the image style, which suggests rotating
with the parameter SpinValue as output
range of 360 degrees defined with the
interpolation above.
Create Scaling Animations in React Native
Scale animation grows and expands the
component.
const Scale= new Animated.Value(0)
const ScaleValue= Scale.interpolate({
inputRange: [0, 1],
outputRange: [1, 2]
})
useEffect(()=>{
Animated.timing(Scale,{
toValue:1,
duration:3000,
useNativeDriver:true
}).start()
},[Scale])
< Animated.Image
style={{height:100,width:130,transform:
[{scale:ScaleValue}]}}
source={{ uri:
"https://images.unsplash.com/photo-
1541963463532-d68292c34b19?ixlib=rb-
1.2.1&ixid=MXwxMjA3fDB8MHxleHBsb3JlL
WZlZWR8M3x8fGVufDB8fHw%3D&w=100
0&q=80" }}
>
< /Animated.Image >
We have used transform configuration in
the image style, which suggests scaling with
the parameter ScaleValue of specified
output range defined with the interpolation
above.
Spring Animation
Spring Animation is like what spring looks
like when it’s in motion.
const Spring= new Animated.Value(0.2)
useEffect(()=>{
Animated.spring(Spring,{
toValue:1.1,
friction:1,
useNativeDriver:true}).start()
},[Spring])
Here Animated.spring() animation takes a
configuration similar to the
Animated.timing() instead of duration it
uses friction and tension as the physics
spring model.
< Animated.Image
style={{height:150,width:180,transform:
[{scale:Spring}]}}
source={{ uri:
"https://images.unsplash.com/photo-
1541963463532-d68292c34b19?ixlib=rb-
1.2.1&ixid=MXwxMjA3fDB8MHxleHBsb3JlL
WZlZWR8M3x8fGVufDB8fHw%3D&w=100
0&q=80" }}
>
< /Animated.Image >
Parallel Animation
Parallel animation takes an array of
different Animation type configurations to
animinate them parallelly.
Here we are taking three different
animatable components,i.e., Scale, spring,
and rotate, to make them animate
parallelly.
Initializing animated values with
interpolation for three scenario:
const Scale = new Animated.Value(0)
const ScaleValue = Scale.interpolate({
inputRange: [0, 1],
outputRange: [-3, 2]
});
const Spin = new Animated.Value(0)
const SpinValue = Spin.interpolate({
inputRange: [0, 1],
outputRange: ["0deg", "360deg"]
});
const SpringVal = new Animated.Value(1)
Starting Animation for parallel animation
type in use effect:
useEffect(() => {
Animated.parallel(
[
Animated.timing(Scale, {
toValue: 1,
duration: 3000,
useNativeDriver: true
}), //scaling
Animated.spring(SpringVal, {
toValue: 2,
friction: 1,
tension: 0.5,
useNativeDriver: true
}), //spring
Animated.timing(Spin, {
toValue: 1,
duration: 3000,
useNativeDriver: true
}) //spin
])
.start()
}, [Scale, SpringVal, Spin])
Rendering Animated components(View and
text) on the screen with types:
< Animated.View
style={{ height: 50, width: 80,
backgroundColor: 'red',
transform: [{ scaleX: ScaleValue }] }}
>
What's up
Welcome here !!
< /Animated.Text >
Sequence Animation
Sequence Animation takes an array of
different Animation type configurations
and animates them one by one after the
previous one ends.
Here we are taking three different
animatable components,i.e., Scale, spring,
and rotate, to make them animate
sequentially; everything else is similar to
parallel animation.
Initializing steps will be similar to the
parallel except the starting of the animation
component.It uses Animated.sequence()
type in useeffect :
useEffect(() => {
Animated.sequence(
[
Animated.timing(Scale, {
toValue: 1,
duration: 3000,
useNativeDriver: true
}), //scaling
Animated.spring(SpringValue, {
toValue: 2,
friction: 1,
tension: 0.5,
useNativeDriver: true
}), //spring
Animated.timing(Spin, {
toValue: 1,
duration: 3000,
useNativeDriver: true
}) //spin
]).start()
}, [Scale, SpringValue, Spin])
Conclusion
This was all about how to implement React
Native Animations using Animated API
scratch. Animations make your application
presentable and due to which users enjoy
interaction with your app. If you are looking
for a helping hand to animate your React
Native App within the project, then get in
touch with us to leverage our top-notch
React native app development expertise.
We also let you hire React Native developer
at your ease and convenience with your
preferable engagement model.
Thank You
www.bacancytechnology.com

Mais conteúdo relacionado

Semelhante a How to implement react native animations using animated api

Crafting interactions with Core Animations, David Ortinau
Crafting interactions with Core Animations, David OrtinauCrafting interactions with Core Animations, David Ortinau
Crafting interactions with Core Animations, David Ortinau
Xamarin
 

Semelhante a How to implement react native animations using animated api (20)

How to Create Custom Animations in Flutter – A Step-by-Step Guide.pdf
How to Create Custom Animations in Flutter – A Step-by-Step Guide.pdfHow to Create Custom Animations in Flutter – A Step-by-Step Guide.pdf
How to Create Custom Animations in Flutter – A Step-by-Step Guide.pdf
 
Android animations
Android animationsAndroid animations
Android animations
 
How to Animate a Widget Across Screens in Flutter.pptx
How to Animate a Widget Across Screens in Flutter.pptxHow to Animate a Widget Across Screens in Flutter.pptx
How to Animate a Widget Across Screens in Flutter.pptx
 
Android view animation in android-chapter18
Android view animation in android-chapter18Android view animation in android-chapter18
Android view animation in android-chapter18
 
Tailwind Animation: How to Make Eye-Catching Websites
Tailwind Animation: How to Make Eye-Catching WebsitesTailwind Animation: How to Make Eye-Catching Websites
Tailwind Animation: How to Make Eye-Catching Websites
 
Angular animate
Angular animateAngular animate
Angular animate
 
Flutter hooks tutorial (part 1) flutter animation using hooks (use effect and...
Flutter hooks tutorial (part 1) flutter animation using hooks (use effect and...Flutter hooks tutorial (part 1) flutter animation using hooks (use effect and...
Flutter hooks tutorial (part 1) flutter animation using hooks (use effect and...
 
Android Training (Animation)
Android Training (Animation)Android Training (Animation)
Android Training (Animation)
 
Android App Development - 12 animations
Android App Development - 12 animationsAndroid App Development - 12 animations
Android App Development - 12 animations
 
Augmented reality in web rtc browser
Augmented reality in web rtc browserAugmented reality in web rtc browser
Augmented reality in web rtc browser
 
COMP340 TOPIC 4 THREE.JS.pptx
COMP340 TOPIC 4 THREE.JS.pptxCOMP340 TOPIC 4 THREE.JS.pptx
COMP340 TOPIC 4 THREE.JS.pptx
 
Neoito — Animations in Angular 5
Neoito — Animations in Angular 5Neoito — Animations in Angular 5
Neoito — Animations in Angular 5
 
Crafting interactions with Core Animations, David Ortinau
Crafting interactions with Core Animations, David OrtinauCrafting interactions with Core Animations, David Ortinau
Crafting interactions with Core Animations, David Ortinau
 
Simple setup for an Angular EventEmitter
Simple setup for an Angular EventEmitterSimple setup for an Angular EventEmitter
Simple setup for an Angular EventEmitter
 
EPiImage
EPiImageEPiImage
EPiImage
 
Composable and streamable Play apps
Composable and streamable Play appsComposable and streamable Play apps
Composable and streamable Play apps
 
Animation
AnimationAnimation
Animation
 
Demystifying Angular Animations
Demystifying Angular AnimationsDemystifying Angular Animations
Demystifying Angular Animations
 
Unity3d scripting tutorial
Unity3d scripting tutorialUnity3d scripting tutorial
Unity3d scripting tutorial
 
Angular 16 – the rise of Signals
Angular 16 – the rise of SignalsAngular 16 – the rise of Signals
Angular 16 – the rise of Signals
 

Mais de Katy Slemon

Mais de Katy Slemon (20)

React Alternatives Frameworks- Lightweight Javascript Libraries.pdf
React Alternatives Frameworks- Lightweight Javascript Libraries.pdfReact Alternatives Frameworks- Lightweight Javascript Libraries.pdf
React Alternatives Frameworks- Lightweight Javascript Libraries.pdf
 
Data Science Use Cases in Retail & Healthcare Industries.pdf
Data Science Use Cases in Retail & Healthcare Industries.pdfData Science Use Cases in Retail & Healthcare Industries.pdf
Data Science Use Cases in Retail & Healthcare Industries.pdf
 
How Much Does It Cost To Hire Golang Developer.pdf
How Much Does It Cost To Hire Golang Developer.pdfHow Much Does It Cost To Hire Golang Developer.pdf
How Much Does It Cost To Hire Golang Developer.pdf
 
What’s New in Flutter 3.pdf
What’s New in Flutter 3.pdfWhat’s New in Flutter 3.pdf
What’s New in Flutter 3.pdf
 
Why Use Ruby On Rails.pdf
Why Use Ruby On Rails.pdfWhy Use Ruby On Rails.pdf
Why Use Ruby On Rails.pdf
 
How Much Does It Cost To Hire Full Stack Developer In 2022.pdf
How Much Does It Cost To Hire Full Stack Developer In 2022.pdfHow Much Does It Cost To Hire Full Stack Developer In 2022.pdf
How Much Does It Cost To Hire Full Stack Developer In 2022.pdf
 
How to Implement Middleware Pipeline in VueJS.pdf
How to Implement Middleware Pipeline in VueJS.pdfHow to Implement Middleware Pipeline in VueJS.pdf
How to Implement Middleware Pipeline in VueJS.pdf
 
How to Build Laravel Package Using Composer.pdf
How to Build Laravel Package Using Composer.pdfHow to Build Laravel Package Using Composer.pdf
How to Build Laravel Package Using Composer.pdf
 
Sure Shot Ways To Improve And Scale Your Node js Performance.pdf
Sure Shot Ways To Improve And Scale Your Node js Performance.pdfSure Shot Ways To Improve And Scale Your Node js Performance.pdf
Sure Shot Ways To Improve And Scale Your Node js Performance.pdf
 
How to Develop Slack Bot Using Golang.pdf
How to Develop Slack Bot Using Golang.pdfHow to Develop Slack Bot Using Golang.pdf
How to Develop Slack Bot Using Golang.pdf
 
IoT Based Battery Management System in Electric Vehicles.pdf
IoT Based Battery Management System in Electric Vehicles.pdfIoT Based Battery Management System in Electric Vehicles.pdf
IoT Based Battery Management System in Electric Vehicles.pdf
 
Understanding Flexbox Layout in React Native.pdf
Understanding Flexbox Layout in React Native.pdfUnderstanding Flexbox Layout in React Native.pdf
Understanding Flexbox Layout in React Native.pdf
 
The Ultimate Guide to Laravel Performance Optimization in 2022.pdf
The Ultimate Guide to Laravel Performance Optimization in 2022.pdfThe Ultimate Guide to Laravel Performance Optimization in 2022.pdf
The Ultimate Guide to Laravel Performance Optimization in 2022.pdf
 
New Features in iOS 15 and Swift 5.5.pdf
New Features in iOS 15 and Swift 5.5.pdfNew Features in iOS 15 and Swift 5.5.pdf
New Features in iOS 15 and Swift 5.5.pdf
 
How to Hire & Manage Dedicated Team For Your Next Product Development.pdf
How to Hire & Manage Dedicated Team For Your Next Product Development.pdfHow to Hire & Manage Dedicated Team For Your Next Product Development.pdf
How to Hire & Manage Dedicated Team For Your Next Product Development.pdf
 
Choose the Right Battery Management System for Lithium Ion Batteries.pdf
Choose the Right Battery Management System for Lithium Ion Batteries.pdfChoose the Right Battery Management System for Lithium Ion Batteries.pdf
Choose the Right Battery Management System for Lithium Ion Batteries.pdf
 
Flutter Performance Tuning Best Practices From the Pros.pdf
Flutter Performance Tuning Best Practices From the Pros.pdfFlutter Performance Tuning Best Practices From the Pros.pdf
Flutter Performance Tuning Best Practices From the Pros.pdf
 
Angular Universal How to Build Angular SEO Friendly App.pdf
Angular Universal How to Build Angular SEO Friendly App.pdfAngular Universal How to Build Angular SEO Friendly App.pdf
Angular Universal How to Build Angular SEO Friendly App.pdf
 
How to Set Up and Send Mails Using SendGrid in NodeJs App.pdf
How to Set Up and Send Mails Using SendGrid in NodeJs App.pdfHow to Set Up and Send Mails Using SendGrid in NodeJs App.pdf
How to Set Up and Send Mails Using SendGrid in NodeJs App.pdf
 
Ruby On Rails Performance Tuning Guide.pdf
Ruby On Rails Performance Tuning Guide.pdfRuby On Rails Performance Tuning Guide.pdf
Ruby On Rails Performance Tuning Guide.pdf
 

Último

Último (20)

How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
 
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
 
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot ModelMcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
 
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 AmsterdamDEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
 
Elevate Developer Efficiency & build GenAI Application with Amazon Q​
Elevate Developer Efficiency & build GenAI Application with Amazon Q​Elevate Developer Efficiency & build GenAI Application with Amazon Q​
Elevate Developer Efficiency & build GenAI Application with Amazon Q​
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdfRising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
MS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsMS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectors
 
Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
CNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In PakistanCNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In Pakistan
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 

How to implement react native animations using animated api

  • 1. How to Implement React Native Animations Using Animated API www.bacancytechnology.com
  • 2. Table of contents 1. Introduction 2. Tutorial Goal: Learn to Implement React Native Animations 3. Working with React Native Animations 4. Steps to Quickly Add Animations in React Native App 5. Conclusion
  • 4. Animations are an essential part of a UX of an application. Animations significantly impact the user for a better interaction experience and smoothen the user engagement. Whenever there is an extended functionality to perform, the animation is a great idea to engage users. Animation has a movement from the static state to the state of motion for better user interaction. Animations have various transitions to show elements and thus make it enjoyable. You might be tempted to implement React Native Animations using animated API, and maybe you are searching for how to get started with React Native animations. Here is a step-by-step guide on adding React Native animations from scratch.
  • 6. We will learn about how to get started with React Native Animation using the React Native’s Animated library. The video is the final output of advanced animations in React Native. Watch Video The main workflow is to create an Animated Value and use it for different components. We will be discussing the following React Native Animation Types also:
  • 7. Please add some information to these points as well Animated.timing() It allows us to define an animation to a specific value over a certain amount of time. Animated.spring() It allows us to define animation from start points to endpoints without defining time as we did in timing.
  • 8. Animated.parallel() It allows us to have all the defined animations in the array to trigger at the same time. Animated.sequence() It allows us to have all the defined animations in the array to trigger one after another.
  • 10. We will start the animation by using the start() function to our animation-type methods. start() takes a completion callback called when the animation task is completed, creating an infinite animation. Using start function with Animated.timing() :- Animated.timing(value,{}).start() Animated.timing() will take two parameters here, a value and an object. The object can take values such as toValue, duration, opacity, etc.
  • 11. Another parameter we will be using is useNativeDriver.The native driver helps send out everything about the animation to the native code before animation starts, through which native code can perform the Animation UI on the thread. Use this in the animation configuration: useNativeDriver: true There are multiple React Native Animatable components and only these components can be animated.
  • 14. The entire source code is available here – Github repo Creating a React Native App Initially, we will create a react native app using: react-native init Animation Installing dependencies for Navigation Install stack navigation for navigating screens using: npm install react-native-gesture-handler react-native-safe-area-context @react- navigation/native @react-navigation/stack.
  • 15. Creating Components We will create basic animation screens such as- 1. Fade Animation 2. Spin Animation 3. Scale Animation 4. Spring Animation 5. Parallel Animation 6. Sequence Animation And a main.js file in which we will import all these screens for navigating.
  • 16. We will pass the type of animation of the component in useEffect() as the argument, so whenever we navigate to the screen respective component is rendered immediately based on the type. Fade Animation This screen will create an animation on the image component, which fades in on render. Create an Animated.Value() for animation every time to make component animatable : const Fade= new Animated.Value(0) We will call Animated.timing() in useEffect(). The purpose of Animated.Timing() is to change the input value to a specified output value after a certain amount of time.
  • 17. useEffect(()=>{ Animated.timing(Fade,{ toValue:1, duration:3000, useNativeDriver: true }).start() },[Fade]) Animated.Timing() takes the first argument as the value which needs to be updated; a second argument is an object containing three values. The first value toValue:1 updates the value of Fade to 1. The second argument duration:3000 is the specific amount of time after which it should reflect animation, and the third argument is useNativeDriver: true, which I have explained above.
  • 18. < Animated.Image style= {{marginTop:5,height:200,width:200,opacity :Fade,}} source={{ uri: "https://images.unsplash.com/photo- 1541963463532-d68292c34b19?ixlib=rb- 1.2.1&#038;ixid=MXwxMjA3fDB8MHxleHBs b3JlLWZlZWR8M3x8fGVufDB8fHw%3D&#0 38;w=1000&#038;q=80" }} > < /Animated.Image > opacity:Fade takes the value of Fade to implement animation after the duration of 3000ms. Spin Animation Spin animation rotates and makes the component spin according to the output degree values.
  • 19. const Spin= new Animated.Value(0) const SpinValue= Spin.interpolate({ inputRange: [0, 1], outputRange: ['0deg', '360deg'] }) Interpolation: Interpolation is a way to estimate function at intermediate points defined from input range and output range. We can change colors, rotate and scale the animation component using interpolation. useEffect(()=>{ Animated.timing(Spin,{ toValue:1, duration:3000, useNativeDriver:true }).start() },[Spin])
  • 20. < Animated.Image style= {{height:150,width:180,marginTop:8,borderR adius:20,transform:[{rotate:SpinValue}]}} source={{ uri: "https://images.unsplash.com/photo- 1541963463532-d68292c34b19?ixlib=rb- 1.2.1&#038;ixid=MXwxMjA3fDB8MHxleHBs b3JlLWZlZWR8M3x8fGVufDB8fHw%3D&#0 38;w=1000&#038;q=80" }} > < /Animated.Image > We have used transform configuration in the image style, which suggests rotating with the parameter SpinValue as output range of 360 degrees defined with the interpolation above.
  • 21. Create Scaling Animations in React Native Scale animation grows and expands the component. const Scale= new Animated.Value(0) const ScaleValue= Scale.interpolate({ inputRange: [0, 1], outputRange: [1, 2] }) useEffect(()=>{ Animated.timing(Scale,{ toValue:1, duration:3000, useNativeDriver:true }).start() },[Scale])
  • 22. < Animated.Image style={{height:100,width:130,transform: [{scale:ScaleValue}]}} source={{ uri: "https://images.unsplash.com/photo- 1541963463532-d68292c34b19?ixlib=rb- 1.2.1&ixid=MXwxMjA3fDB8MHxleHBsb3JlL WZlZWR8M3x8fGVufDB8fHw%3D&w=100 0&q=80" }} > < /Animated.Image > We have used transform configuration in the image style, which suggests scaling with the parameter ScaleValue of specified output range defined with the interpolation above. Spring Animation Spring Animation is like what spring looks like when it’s in motion.
  • 23. const Spring= new Animated.Value(0.2) useEffect(()=>{ Animated.spring(Spring,{ toValue:1.1, friction:1, useNativeDriver:true}).start() },[Spring]) Here Animated.spring() animation takes a configuration similar to the Animated.timing() instead of duration it uses friction and tension as the physics spring model.
  • 24. < Animated.Image style={{height:150,width:180,transform: [{scale:Spring}]}} source={{ uri: "https://images.unsplash.com/photo- 1541963463532-d68292c34b19?ixlib=rb- 1.2.1&ixid=MXwxMjA3fDB8MHxleHBsb3JlL WZlZWR8M3x8fGVufDB8fHw%3D&w=100 0&q=80" }} > < /Animated.Image > Parallel Animation Parallel animation takes an array of different Animation type configurations to animinate them parallelly. Here we are taking three different animatable components,i.e., Scale, spring, and rotate, to make them animate parallelly.
  • 25. Initializing animated values with interpolation for three scenario:
  • 26. const Scale = new Animated.Value(0) const ScaleValue = Scale.interpolate({ inputRange: [0, 1], outputRange: [-3, 2] }); const Spin = new Animated.Value(0) const SpinValue = Spin.interpolate({ inputRange: [0, 1], outputRange: ["0deg", "360deg"] }); const SpringVal = new Animated.Value(1) Starting Animation for parallel animation type in use effect: useEffect(() => { Animated.parallel( [ Animated.timing(Scale, { toValue: 1, duration: 3000, useNativeDriver: true }), //scaling
  • 27. Animated.spring(SpringVal, { toValue: 2, friction: 1, tension: 0.5, useNativeDriver: true }), //spring Animated.timing(Spin, { toValue: 1, duration: 3000, useNativeDriver: true }) //spin ]) .start() }, [Scale, SpringVal, Spin]) Rendering Animated components(View and text) on the screen with types:
  • 28. < Animated.View style={{ height: 50, width: 80, backgroundColor: 'red', transform: [{ scaleX: ScaleValue }] }} > What's up Welcome here !! < /Animated.Text > Sequence Animation Sequence Animation takes an array of different Animation type configurations and animates them one by one after the previous one ends.
  • 29. Here we are taking three different animatable components,i.e., Scale, spring, and rotate, to make them animate sequentially; everything else is similar to parallel animation. Initializing steps will be similar to the parallel except the starting of the animation component.It uses Animated.sequence() type in useeffect :
  • 30. useEffect(() => { Animated.sequence( [ Animated.timing(Scale, { toValue: 1, duration: 3000, useNativeDriver: true }), //scaling Animated.spring(SpringValue, { toValue: 2, friction: 1, tension: 0.5, useNativeDriver: true }), //spring Animated.timing(Spin, { toValue: 1, duration: 3000, useNativeDriver: true }) //spin ]).start() }, [Scale, SpringValue, Spin])
  • 32. This was all about how to implement React Native Animations using Animated API scratch. Animations make your application presentable and due to which users enjoy interaction with your app. If you are looking for a helping hand to animate your React Native App within the project, then get in touch with us to leverage our top-notch React native app development expertise. We also let you hire React Native developer at your ease and convenience with your preferable engagement model.