问题描述
你算算translateX
,并translate
Y与三角函数功能。
translateX
对应Math.sin()
,并且translateY
是对应于Math.cos()
。
尽管react-native animated.interpolate
不支持函数回调,但是您可以将其分为几部分进行模拟(我在
代码示例中选择了50个):
完整代码:
export class App extends Component {
constructor() {
super()
this.animated = new Animated.Value(0);
var range = 1, snapshot = 50, radius = 100;
/// translateX
var inputRange = [], outputRange = [];
for (var i=0; i<=snapshot; ++i) {
var value = i/snapshot;
var move = Math.sin(value * Math.PI * 2) * radius;
inputRange.push(value);
outputRange.push(move);
}
this.translateX = this.animated.interpolate({ inputRange, outputRange });
/// translateY
var inputRange = [], outputRange = [];
for (var i=0; i<=snapshot; ++i) {
var value = i/snapshot;
var move = -Math.cos(value * Math.PI * 2) * radius;
inputRange.push(value);
outputRange.push(move);
}
this.translateY = this.animated.interpolate({ inputRange, outputRange });
}
animate() {
this.animated.setValue(0)
Animated.timing(this.animated, {
tovalue: 1,
duration: 1000,
}).start();
}
render() {
const transform = [{ translateY: this.translateY }, {translateX: this.translateX}];
return (
<View style={styles.container}>
<Animated.View style={[{ transform }]}>
<TouchableOpacity style={styles.btn}>
<Text>hallo</Text>
</TouchableOpacity>
</Animated.View>
<Button title="Test" onPress={() => {
this.animate()
}} />
</View>
);
}
}
// define your styles
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#2c3e50',
},
btn: {
backgroundColor: 'red',
justifyContent: 'center',
alignItems: 'center',
width: 50,
}
});
解决方法
嗨,我希望animation.view像一个圆圈一样移动。我以
鼻窦想到了这一点,但没有用。有人知道该怎么做吗?我不想旋转
视图。它应该绕圈移动。我是新来的本地人。
如果有人可以帮助我,那就太好了。
//import liraries
import React,{ Component } from 'react';
import { View,Text,StyleSheet,Animated,Button,TouchableOpacity } from 'react-native';
// create a component
class MyClass extends Component {
constructor() {
super()
this.animated = new Animated.Value(0);
}
animate() {
this.animated.setValue(0)
Animated.timing(this.animated,{
toValue: Math.PI *2,duration: 1000,}).start();
}
render() {
const translateY = this.animated.interpolate({
inputRange: [0,Math.PI *2],outputRange: [0,200]
});
const translateX = translateY
const transform = [{ translateY },{translateX}];
return (
<View style={styles.container}>
<Animated.View style={[{ transform }]}>
<TouchableOpacity style={styles.btn}>
<Text>hallo</Text>
</TouchableOpacity>
</Animated.View>
<Button title="Test" onPress={() => {
this.animate()
}} />
</View>
);
}
}
// define your styles
const styles = StyleSheet.create({
container: {
flex: 1,justifyContent: 'center',alignItems: 'center',backgroundColor: '#2c3e50',},btn: {
backgroundColor: 'red',width: 50,}
});
//make this component available to the app
export default MyClass;