我正在创建一个圆形旋转木马在设计是有一些项目在圆圈上,当用户单击向上按钮或向下按钮,它将把项目向上或向下。
下面是

现在,我可以让物品只能移动一次使用反应土著动画,但我不知道为什么它只移动一次?如果我再在“向上”按钮上点击一次,它就不再移动了
这是密码
import * as React from 'react';
import { Text, View, StyleSheet, Animated, Button } from 'react-native';
import Constants from 'expo-constants';
function degToRad(deg) {
return (deg * Math.PI) / 180;
}
export default class Circle extends React.Component {
constructor(props) {
super(props);
this.state = {
//usersList: ['A'],
usersList: ['A', 'B', 'C', 'D', 'E', 'F'],
};
this.animations = [];
for (let i = 0; i < this.state.usersList.length; i++) {
this.animations.push(new Animated.ValueXY(this.returnXY(i)));
}
}
returnXY = index => {
let degree = (index * 360) / this.state.usersList.length;
const { size, symbolSize } = this.props;
const angleRad = degToRad(degree); //45
const radius = size / 2;
const center = radius;
let x = radius * Math.cos(angleRad) + center - symbolSize / 2;
let y = radius * Math.sin(angleRad) + center - symbolSize / 2;
return { x, y };
};
constructoCircleElements = () => {
let object = [];
const { symbolSize } = this.props;
for (let i = 0; i < this.state.usersList.length; i++) {
let animation = this.animations[i].getLayout();
object.push(
<Animated.View
style={[
styles.symbol,
{
width: symbolSize * 2,
height: symbolSize,
borderRadius: symbolSize / 2,
},
animation,
]}>
<Text style={{ color: 'white' }}>{this.state.usersList[i]}</Text>
</Animated.View>
);
}
return object;
};
goUp = () => {
console.log('GO UP');
for (let i = 0; i < this.state.usersList.length; i++) {
let { x, y } = this.returnXY(i + 1);
Animated.spring(this.animations[i], {
toValue: { x, y },
}).start();
}
};
render() {
const { size } = this.props;
let displayList = this.constructoCircleElements();
return (
<View>
<View
style={[
styles.circle,
{
width: size,
height: size,
borderRadius: size / 2,
},
]}>
<Text style={styles.circleCaption}>A</Text>
{displayList}
</View>
<Button title={'UP'} color="black" onPress={this.goUp} />
</View>
);
}
}
const styles = StyleSheet.create({
circle: {
backgroundColor: 'transparent',
alignItems: 'center',
justifyContent: 'center',
borderWidth: 3,
borderRadius: 500,
borderStyle: 'dotted',
borderColor: '#fff',
},
circleCaption: {
fontSize: 70,
},
symbol: {
backgroundColor: 'green',
position: 'absolute',
},
});拉奥/同生酸奶在Circle.js中
我认为这是因为我没有将项目x和y更新到最新的状态?但我不知道该怎么办?
谢谢
发布于 2019-12-07 10:07:52
您没有保存循环项的当前索引。我对您的代码做了一些修改:
constructor(props) {
super(props);
this.state = {
//usersList: ['A'],
usersList: ['A', 'B', 'C', 'D', 'E', 'F'],
};
this.animations = [];
for (let i = 0; i < this.state.usersList.length; i++) {
//Here I added index
this.animations.push({
value: new Animated.ValueXY(this.returnXY(i)),
index: i,
});
}
}
returnXY = index => {
let degree = (index * 360) / this.state.usersList.length;
const { size, symbolSize } = this.props;
const angleRad = degToRad(degree); //45
const radius = size / 2;
const center = radius;
let x = radius * Math.cos(angleRad) + center - symbolSize / 2;
let y = radius * Math.sin(angleRad) + center - symbolSize / 2;
return { x, y };
};
constructoCircleElements = () => {
let object = [];
const { symbolSize } = this.props;
for (let i = 0; i < this.state.usersList.length; i++) {
let animation = this.animations[i].value.getLayout();
object.push(
<Animated.View
style={[
styles.symbol,
{
width: symbolSize * 2,
height: symbolSize,
borderRadius: symbolSize / 2,
},
animation,
]}>
<Text style={{ color: 'white' }}>{this.state.usersList[i]}</Text>
</Animated.View>
);
}
return object;
};
goUp = () => {
let temp = [];
for (let i = 0; i < this.state.usersList.length; i++) {
//Here I increment the count
let { x, y } = this.returnXY(++this.animations[i].index);
let animation = Animated.spring(this.animations[i].value, {
toValue: { x, y },
});
temp.push(animation);
}
Animated.parallel(temp).start();
};
render() {
const { size } = this.props;
let displayList = this.constructoCircleElements();
return (
<View>
<View
style={[
styles.circle,
{
width: size,
height: size,
borderRadius: size / 2,
},
]}>
<Text style={styles.circleCaption}>A</Text>
{displayList}
</View>
<Button title={'UP'} color="white" onPress={this.goUp} />
</View>
);
}
}这里是用来测试的零食!
https://stackoverflow.com/questions/59224632
复制相似问题