react native circle transform translate animation
You have to calculate translateX
and translateY
with Trigonometric Function.
translateX
is corresponding to Math.sin()
, and translateY
is corresponding to Math.cos()
.
Although react-native animated.interpolate
doesn't support function callback, you can simulate it by divided into several parts (I picked 50 in my code example):
Full Code:
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,
}
});
Result:
If you are looking for Circular rotation with high PERFORMANCE. (without shivering / complex mathematics)
here it is
https://rn-animiations.web.app/component/circular-animation
Full code:
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.dot, {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
},
dot: {
width: '100%',
height: 20,
backgroundColor: 'red',
position: 'absolute',
alignItems: 'center',
justifyContent: 'center',
},
text: {
color: '#fff',
},
});