-
Notifications
You must be signed in to change notification settings - Fork 14
/
RouletteItem.js
48 lines (39 loc) · 1.2 KB
/
RouletteItem.js
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
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import { View } from 'react-native';
class RouletteItem extends Component {
constructor(props) {
super(props);
this.state = {
coordX: props.radius,
coordY: props.radius
};
}
getCoordinates({ width, height }) {
const { radius, index, step, distance } = this.props;
const coordX = Math.round(radius / 2 + distance * Math.cos((index) * step) - width / 2);
const coordY = Math.round(radius / 2 + distance * Math.sin((index) * step) - height / 2);
this.setState({ coordX, coordY });
}
render() {
const { item, rouletteRotate } = this.props;
const { coordX, coordY } = this.state;
return (
<View
style={{ position: 'absolute', left: coordX, top: coordY, transform: [{ rotate: `${-rouletteRotate}deg` }] }}
onLayout={(event) => this.getCoordinates(event.nativeEvent.layout)}
>
{item}
</View>
);
}
}
RouletteItem.propTypes = {
step: PropTypes.number,
index: PropTypes.number,
radius: PropTypes.number,
distance: PropTypes.number,
rouletteRotate: PropTypes.number,
item: PropTypes.element.isRequired
};
export default RouletteItem;