react-native-reanimated icon indicating copy to clipboard operation
react-native-reanimated copied to clipboard

useAnimatedStyle not working with @expo/vector-icons

Open carlos3g opened this issue 3 years ago • 9 comments

Description

I created a animated component of Ionicons icon set using Animated.createAnimatedComponent and tried to animate its translateY, but nothing happens

Expected behavior

The Animated Icon change position due to translateX changes from 0 to 100

Actual behavior & steps to reproduce

Nothing happens. The AnimatedIcon stays in the initial position

Snack or minimal code example

import { Ionicons } from '@expo/vector-icons';
import { FC } from 'react';
import { StyleSheet, TouchableOpacity, View } from 'react-native';
import Animated, {
  useAnimatedStyle,
  useSharedValue,
} from 'react-native-reanimated';

const AnimatedIcon = Animated.createAnimatedComponent(Ionicons);

const Home: FC = () => {
  const teste = useSharedValue(0);

  const heartStyle = useAnimatedStyle(() => ({
    transform: [{ translateY: teste.value }],
  }));

  return (
    <View style={styles.container}>
      <AnimatedIcon name="heart" size={40} style={heartStyle} />
      <TouchableOpacity
        style={styles.fab}
        onPress={() => (teste.value = 100)}
      />
    </View>
  );
};

const styles = StyleSheet.create({
  container: {
    flex: 1,
  },
  fab: {
    position: 'absolute',
    bottom: 16,
    right: 16,
    width: 56,
    aspectRatio: 1,
    borderRadius: 28,
    backgroundColor: 'black',
  },
});

export default Home;

Package versions

name version
react-native-reanimated 2.3.1
NodeJS 16.13.2
expo sdk 44

Affected platforms

  • [x] Android
  • [ ] iOS
  • [ ] Web
  • [x] have not tested on others

carlos3g avatar Jan 23 '22 17:01 carlos3g

Got same issue using animatedProps

xD3CODER avatar Feb 09 '22 11:02 xD3CODER

Same issue on IOS too, it works only sometimes, wonder if there's a solution, I need to animate the fontSize.

Video shows:

  • Upon first load the animation does not work
  • When I change something in the code and trigger an update, it works

https://github.com/software-mansion/react-native-reanimated/assets/124635309/6182de0f-fcec-40e3-b2fd-6e90db91de83

const AnimatedIcon = Animated.createAnimatedComponent(MaterialIcons);

const animatedTrashIcon = useAnimatedStyle(() => ({
      fontSize: scaledSharedValue.value,
    }));
<AnimatedIcon
    name="delete"
    color="white"
    size={10}
    style={animatedTrashIcon}
    />

BeyramTaglietti avatar Jan 28 '24 17:01 BeyramTaglietti

hey @tomekzaw, sorry for pinging directly, do you have an update on this? I see that it was part of the Jan 2022 bug bash there's not been an update and this issue still persists

MehediH avatar Apr 12 '24 07:04 MehediH

Hey @MehediH, apparently there's no update on this issue.

As a workaround, have you tried wrapping Ionicon inside an Animated.View?

tomekzaw avatar Apr 12 '24 07:04 tomekzaw

@tomekzaw I have tried wrapping inside an Animated.View and it doesn't work because I need the Icon's fontSize to be animated, but we are not allowed to set fontSize as a style property on View

Currently I have:

import Animated from "react-native-reanimated";

export const AnimatedAntDesign = Animated.createAnimatedComponent(AntDesign);

const maskIconStyle = useAnimatedStyle(() => ({
    fontSize: iconSize.value
  }));

return (
   <AnimatedAntDesign
    style={[maskIconStyle]}
    name="heart"
   />
)

the above doesn't work, the size only changes on re-renders, but not on the initial render.

and I can't do something like this either:

return (
   <Animated.View style={[maskIconStyle]}
        <AnimatedAntDesign
           style=[[maskIconStyle]
           name="heart"
        />
    </Animated.View>
)

Thoughts? 👀

it's really important to be able to animate the icon size so it's a bummer that this isn't working. FYI I also tried with animatedProps (to set the size) but it doesn't work (something about setNativeProps not being defined). Thank you!

MehediH avatar Apr 12 '24 07:04 MehediH

@MehediH Hi, as a workaround you may try wrapping your SVG with Animated.View and applying style using scale property

exploIF avatar May 24 '24 12:05 exploIF

@MehediH Hi, as a workaround you may try wrapping your SVG with Animated.View and applying style using scale property

Doing so works but the icon appears granulated because it's just doing a simple zoom in instead of increasing the svg's font size.

BeyramTaglietti avatar May 24 '24 15:05 BeyramTaglietti

@MehediH Hi, as a workaround you may try wrapping your SVG with Animated.View and applying style using scale property

Doing so works but the icon appears granulated because it's just doing a simple zoom in instead of increasing the svg's font size.

In that scenario you can try using bigger icon by default and in your animation scaling it in range 0-1

exploIF avatar May 24 '24 15:05 exploIF

It does seem like the issue may be with the @expo/vector-icons package for some reason I've not figured out yet.. I had some errors animating the opacity and transform after using the Animated.createAnimatedComponent() method. I switched to the react-native-vector-icons package and that seems to work.

firstChairCoder avatar Jun 03 '24 13:06 firstChairCoder

It does seem like the issue may be with the @expo/vector-icons package for some reason I've not figured out yet.. I had some errors animating the opacity and transform after using the Animated.createAnimatedComponent() method. I switched to the react-native-vector-icons package and that seems to work.

@firstChairCoder can you explain more? I've just switched to react-native-vector-icons and I'm still experiencing no animations.

andrewlpmcneill avatar Jul 13 '24 16:07 andrewlpmcneill