0

I have a Pressable component and inside it I have an icon. I want to press it and rotate it 180 degrees. But the animation only works once. When I click again nothing happens.

const animation = useSharedValue(0);

  const rotation = useDerivedValue(() => {
    return interpolate(animation.value, [0, 180], [0, 65]);
  });

  const animationStyle = useAnimatedStyle(() => {
    return {
      transform: [
        {
          rotate: rotation.value + "deg",
        },
      ],
    };
  });

  const startAnimation = () => {
    setShowDetail(!showDetail);
    animation.value = withTiming(500, {
      duration: 300,
    });
  };

In the component

              <Animated.View style={[animationStyle]}>
                <Icons/>
              </Animated.View>
Yoel
  • 7,555
  • 6
  • 27
  • 59
Hora
  • 162
  • 1
  • 8

1 Answers1

0

That's because it rotates from 0 to 180 again and again. cuz rotation value is fixed. You should update the rotate value with useState.

const [rotationValue, setRotationValue] = useState(0);

const handleClickRotate = () => {
    setRotationValue(p => p + 180); // you can use other deg value
}

And use the rotationValue to animate your component. :D

Dae Hyeon Mun
  • 521
  • 4
  • 7