我需要创建一个动画,其中一个图像将环绕另一个图像。我已经尝试使用类似问题的建议,如 Animate a Circle around another circle ,但不幸的是它没有帮助。我尝试研究可提供理想功能的第 3 方模块,但没有找到满足我需要的东西。
我找到了一个有用的 article理解 JavaScript 中的圆周运动,但是我很难在 React Native 动画中复制它。我相信我很难理解 Animated API 的正确用法和 transform
动画圆周运动时的样式属性。
<View style={animationContainer}>
<Image
source={require('./images/image.png')}
style={image}
/>
<Animated.Image
source={require('./images/icon.png')}
style={circlingIcon}
/>
</View>
最佳答案
我已经为问题 react native circle transform translate animation 发布了类似的butter smooth 解决方案
完整代码:
import React, {Component} from 'react';
import {View, Text, Animated, StyleSheet, Easing} from 'react-native';
export default class Circle extends Component {
constructor() {
super();
this.animated = new Animated.Value(0);
var inputRange = [0, 1];
var outputRange = ['0deg', '360deg'];
this.rotate = this.animated.interpolate({inputRange, outputRange});
outputRange = ['0deg', '-360deg'];
this.rotateOpposit = this.animated.interpolate({inputRange, outputRange});
}
componentDidMount() {
this.animate();
}
animate() {
Animated.loop(
Animated.timing(this.animated, {
toValue: 1,
duration: 4000,
useNativeDriver: true,
easing: Easing.linear,
}),
).start();
}
render() {
const transform = [{rotate: this.rotate}];
const transform1 = [{rotate: this.rotateOpposit}];
return (
<View style={styles.container}>
<Animated.View style={[styles.item, {transform}]}>
<Animated.View style={[styles.topItem, {transform: transform1}]}>
<Text style={styles.text}>Test</Text>
</Animated.View>
</Animated.View>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
},
item: {
position: 'absolute',
width: 100,
height: 200, // this is the diameter of circle
},
topItem: {
width: '100%',
height: 20,
backgroundColor: 'red',
position: 'absolute',
alignItems: 'center',
justifyContent: 'center',
},
text: {
color: '#fff',
},
});
希望对你有帮助
关于javascript - React Native 中的循环运动动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60049796/