-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBasicAnimation.jsx
More file actions
41 lines (35 loc) · 845 Bytes
/
BasicAnimation.jsx
File metadata and controls
41 lines (35 loc) · 845 Bytes
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
import {Button, StyleSheet, Text, View} from 'react-native';
import React, {useState} from 'react';
export default function BasicAnimation() {
const [value, setValue] = useState(20);
const [intervalId, setIntervalId] = useState(null);
const moveBox = () => {
if (intervalId) return;
let id = setInterval(() => {
setValue(prev => {
if (prev < 300) return prev + 5;
clearInterval(id);
setIntervalId(null);
return 20;
});
}, 1);
setIntervalId(id);
};
return (
<View>
<View style={[styles.box, {left: value}]} />
<Button title="move" onPress={moveBox} />
</View>
);
}
const styles = StyleSheet.create({
box: {
width: 50,
height: 50,
backgroundColor: 'red',
borderRadius: 12,
left: 20,
top: 20,
marginBottom: 50,
},
});