【发布时间】:2016-10-30 21:18:20
【问题描述】:
我正在尝试制作指南针,但我不知道如何使用磁力计的数据。
这是我的班级指南针:
class Compass extends Component {
constructor(props) {
super(props);
}
componentWillMount(){
this._animeRotation = new Animated.Value(0);
}
componentDidMount() {
this.startAnimation();
}
startAnimation() {
Animated.timing(this._animeRotation, {
toValue: this.props.magn, //<-- What put here?
duration: 1000,
}).start(() => {
this.startAnimation();
});
}
render() {
var interpolatedRotateAnimation = this._animeRotation.interpolate({
inputRange: [0, 100],
outputRange: ['0deg', '360deg']
});
return (
<View>
<Animated.View style={[styles.box, {transform: [{rotate: interpolatedRotateAnimation}]}]}>
<Image style={styles.box} source={require('./arrow.png')}></Image>
</Animated.View>
</View>
);
}
}
这是类App:
class App extends Component {
constructor(props) {
super(props);
this.state = {
magn: {
x: 0,
y: 0,
z: 0
}
};
}
componentDidMount() {
//I can get the gyroscope if is necessary
SensorManager.startMagnetometer(500);
// magn is a object with axis z,y and x
DeviceEventEmitter.addListener('Magnetometer', (magn) => this.setState({magn}));
}
render() {
return (
<View>
<Compass magn={this.state.magn.x}></Compass>
</View>
);
}
}
使用此代码,箭头会旋转,但不会旋转。我必须做什么?
【问题讨论】:
标签: javascript react-native ecmascript-6 react-jsx