My Animated.View
has the following style:
{
transform: [
{
scale: this.animatedValue.interpolate({
inputRange: [0, 1],
outputRange: [initialScale, 1]
})},
{
translateX: this.animatedValue.interpolate({
inputRange: [0, 1],
outputRange: [startX, endX]
})},
{
translateY: this.animatedValue.interpolate({
inputRange: [0, 1],
outputRange: [startY, endY]
})},
]
}
When initialScale is 1 and the animation starts, I see the expected behavior: Animated.View starts at (startX, startY) and linearly moves to (endX, endY).
However, when initialScale is 0.5 for example, the starting point of the view is not (startX, startY), the movement is not linear (a bit spheric) and the end point is still as expected - (endX, endY).
How can I scale my View while keeping a linear movement and expected start position?
Enabling usage of native driver is the easiest way of improving your animations performance quickly. However, the subset of style props that can be used together with native driver is limited. You can use it with non-layout properties like transforms and opacity. It will not work with colors, height, and others.
Working with animationsAnimations are started by calling start() on your animation. start() takes a completion callback that will be called when the animation is done.
In React Native, you can interpolate animated values using . interpolate which takes an inputRange, that interpolates and maps the values to an outputRange. Great thing is that you can interpolate numbers not just to other numbers but also to colours and degrees.
The transition animation is one of the easiest ways to easily animate a React Native application. It provides a way to create a seamless change between various states in your application. In this tutorial, we will be exploring the Transition API and a use-case where the transition animation can be employed; Dark mode.
Like the user @ArneHugo pointed out in the comments, the non-linear movement can be solved by positioning the full-size container element and scaling another element within it.
The position of the element is not as expected, because the origin for the scale transform is the center point of the element. React Native doesn't (yet) support specifying the transform origin, but if the width and height of the scaled element are known in advance, it's easy to calculate the offset as follows:
const width = 100;
const height = 20;
const scale = {
transform: [
{
scale: this.animatedValue.interpolate({
inputRange: [0, 1],
outputRange: [initialScale, 1]
})
}
]
};
const position= {
transform: [
{
translateX: this.animatedValue.interpolate({
inputRange: [0, 1],
outputRange: [startX - (width / 2) - (width * initialScale / 2), endX]
})
},
{
translateY: this.animatedValue.interpolate({
inputRange: [0, 1],
outputRange: [startY - (height / 2) - (height * initialScale / 2), endY]
})
}
]
};
return (
<Animated.View style={position}>
<Animated.View style={[styles.thing, scale]} />
</Animated.View>
);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With