-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.tsx
68 lines (61 loc) · 1.61 KB
/
App.tsx
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import React, { useState, useEffect } from 'react';
import { Animated, StyleSheet, Text, TouchableOpacity, View } from 'react-native';
const App = () => {
const [boxColor, setBoxColor] = useState('#F44336');
const boxPosition = useState(new Animated.Value(0))[0];
useEffect(() => {
Animated.loop(
Animated.sequence([
Animated.timing(boxPosition, {
toValue: -50,
duration: 1000,
useNativeDriver: true,
}),
Animated.timing(boxPosition, {
toValue: 0,
duration: 1000,
useNativeDriver: true,
}),
])
).start();
}, [boxPosition]);
const changeBoxColor = () => {
const randomColor = `#${Math.floor(Math.random() * 16777215).toString(16)}`;
setBoxColor(randomColor);
};
return (
<View style={styles.container}>
<View>
<TouchableOpacity style={styles.button} onPress={changeBoxColor}>
<Text style={styles.buttonText}>Change the color</Text>
</TouchableOpacity>
</View>
<Animated.View style={[styles.box, { backgroundColor: boxColor, transform: [{ translateY: boxPosition }] }]} />
</View>
);
};
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#F5FCFF',
justifyContent: "space-evenly",
alignItems: 'center',
},
box: {
width: 100,
height: 100,
borderRadius: 10,
marginBottom: 20,
},
button: {
paddingVertical: 10,
paddingHorizontal: 20,
backgroundColor: '#2196F3',
borderRadius: 5,
marginBottom: 20,
},
buttonText: {
color: '#FFFFFF',
},
});
export default App;