Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React Native Animated. You must specify exactly one property per transform object

Tags:

react-native

I get this error trying to translate a View using translateX and translateY

ExceptionsManager.js:173 JSON value '( { translateX = 0; translateY = 120; } )' of type NSMutableArray cannot be converted to a CATransform3D. You must specify exactly one property per transform object.

Here is my component:

  const BounceInUp: FC<IAnimatedView> = ({ children, teddyBear }) => {
  const bounceValue = new Animated.Value(120);
  const shakeValue = new Animated.Value(0);

  const animate = useCallback(() => {
    Animated.sequence([
      Animated.delay(300),
      Animated.spring(bounceValue, {
        useNativeDriver: true,
        toValue: -10,
        velocity: 3,
        friction: 8,
      }),
      Animated.sequence([
        Animated.timing(shakeValue, { toValue: 10, duration: 100, useNativeDriver: true }),
        Animated.timing(shakeValue, { toValue: -10, duration: 100, useNativeDriver: true }),
        Animated.timing(shakeValue, { toValue: 10, duration: 100, useNativeDriver: true }),
        Animated.timing(shakeValue, { toValue: 0, duration: 100, useNativeDriver: true }),
      ]),
    ]).start();
  }, [bounceValue, shakeValue]);

  useEffect(() => {
    animate();
  }, [teddyBear.id]);

  return (
    <Animated.View style={[styles.subView, { transform: [{ translateY: bounceValue, translateX: shakeValue }] }]}>
      {children}
    </Animated.View>
  );
};

How can I apply one animation on X and Y without getting this error?

like image 954
Kevin Amiranoff Avatar asked May 28 '20 12:05

Kevin Amiranoff


People also ask

How animations work in React Native?

Animations allow you to convey physically believable motion in your interface. React Native provides two complementary animation systems: Animated for granular and interactive control of specific values, and LayoutAnimation for animated global layout transactions.

How do I stop loop animation in React Native?

If an animation is in process of being animated and, for any particular reason, you need to stop it, you can call stopAnimation . The stopAnimation call also takes a callback with the value that the animation was stopped on.

How do you check if animation is finished React Native?

start() ​ Animations are started by calling start() on your animation. start() takes a completion callback that will be called when the animation is done or when the animation is done because stop() was called on it before it could finish.


2 Answers

You should put each transformation (translateX and translateY) in an object inside trasform array.

transform: [{ translateY: bounceValue }, { translateX: shakeValue }]
like image 89
Mahdi N Avatar answered Oct 12 '22 08:10

Mahdi N


const bounceValue = new Animated.Value(120); I think you might want to try initialising value like this: const valueXY = new Animated.ValueXY({ x: 0, y: 120 }); because you want to animate value x and y.

Then:

Animated.spring(valueXY, {
        useNativeDriver: true,
        toValue: { x: 0, y: 0 },
        velocity: 3,
        friction: 8,
      }),

and in your sequence:

Animated.timing(valueXY, { toValue: { x: 10, y: 0 }, duration: 100, useNativeDriver: true })

Also in style <Animated.View style={[styles.subView, { transform: valueXY.getTranslateTransform() }]}>

You can check here for more info https://animationbook.codedaily.io/animated-value-xy/

like image 1
Anita Avatar answered Oct 12 '22 08:10

Anita