ScrollView component has a event named as onScroll which will execute when user scrolls the ScrollView. In today’s tutorial we would learn about Add Background Animation on ScrollView component in react native. We would use React Native own Animated component library to perform animation while user scrolls the ScrollView. We would define 2 range of colors in outputRange with inputRange of 0 to 3000. The changing of background color would look like Gradient effect with smooth animation. So in this tutorial we would make tutorial on React Native Add Background Animation on ScrollView Scroll Event in Android iOS example tutorial.
Live Screenshot of App:
Contents in this project React Native Add Background Animation on ScrollView Scroll Event Android iOS Example Tutorial:
1. Open your project’s main App.js file and import useState, View, StyleSheet, Animated and ScrollView component.
1
2
3
|
import React, { useState } from ‘react’;
import { View, StyleSheet, Animated, ScrollView } from ‘react-native’;
|
2. Create our main export default App functional component. This is our Root View component for app.
1
2
3
4
5
|
export default function App() {
}
|
3. Create a Constant State named as animationValue with default Animated.Value(0). Using this we would set the Animated value Zero.
1
|
const [animationValue] = useState(new Animated.Value(0));
|
4. Create a Constant named as backgroundInterpolate. We would use this constant with animationValue.interpolate({}) method to define animation input range and output range. Here we would also define the Colors which we want to animate on Scrolling.
1
2
3
4
|
const backgroundInterpolate = animationValue.interpolate({
inputRange : [0, 3000],
outputRange : [“#FF1744” , “#D500F9”]
})
|
5. Create a Constant named as backgroundStyle. Here we would assign the backgroundInterpolate as backgroundColor. We would use this Style of Animated.View component.
1
2
3
|
const backgroundStyle = {
backgroundColor : backgroundInterpolate
}
|
6. Creating Return() block and here we would first make a Root View component -> ScrollView -> Animated.View component. We would set Animated.event on onScroll event to perform animation on Y axis scrolling.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
return (
<View style={styles.MainContainer}>
<ScrollView
scrollEventThrottle={16}
onScroll={Animated.event([{ nativeEvent : { contentOffset: { y : animationValue } }}],
{useNativeDriver: false} )}>
<Animated.View style={[styles.scrollViewStyle, backgroundStyle]} />
</ScrollView>
</View>
);
|
7. Creating Style.
1
2
3
4
5
6
7
8
9
10
11
12
|
const styles = StyleSheet.create({
MainContainer: {
flex: 1,
},
scrollViewStyle:
{
height: 2500
}
});
|
8. Complete Source Code for App.js file:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
|
import React, { useState } from ‘react’;
import { View, StyleSheet, Animated, ScrollView } from ‘react-native’;
export default function App() {
const [animationValue] = useState(new Animated.Value(0));
const backgroundInterpolate = animationValue.interpolate({
inputRange : [0, 3000],
outputRange : [“#FF1744” , “#D500F9”]
})
const backgroundStyle = {
backgroundColor : backgroundInterpolate
}
return (
<View style={styles.MainContainer}>
<ScrollView
scrollEventThrottle={16}
onScroll={Animated.event([{ nativeEvent : { contentOffset: { y : animationValue } }}],
{useNativeDriver: false} )}>
<Animated.View style={[styles.scrollViewStyle, backgroundStyle]} />
</ScrollView>
</View>
);
}
const styles = StyleSheet.create({
MainContainer: {
flex: 1,
},
scrollViewStyle:
{
height: 2500
}
});
|
Screenshots: