简体   繁体   English

React Native - 创建 N 个动画值

[英]React Native - Create N animated values

I am creating a "Color Palette" component which has a prop "paletteColors".我正在创建一个具有道具“paletteColors”的“调色板”组件。

The prop "paletteColors" is an array of variable length, and contains the representative color value as string.道具“paletteColors”是一个可变长度的数组,并包含代表颜色值作为字符串。

Now, in this component, I am mapping this property in order to render N colors:现在,在此组件中,我正在映射此属性以呈现 N colors:

   {paletteColors.map((paletteColor, index) => (
      <Animated.View
        key={index}
        style={{ transform: [{ scale: getColorAnimatedScale(index) }] }}
      >
        <TouchableOpacity
          onPress={() => handleOnSelectColor(index)}
          style={[
            styles.color,
            {
              backgroundColor: paletteColor,
            },
          ]}
        />
      </Animated.View>
    ))}

Every time the user presses a color, the pressed animated view will scale, so I need to create N (palleteColors.length) animated values...每次用户按下颜色时,按下的动画视图都会缩放,所以我需要创建 N (palleteColors.length) 动画值...

Something like this but adapted to the length of the given "paletteColors" array:像这样但适应给定“paletteColors”数组的长度:

const [firstColorScale] = useState(new Animated.Value(1)); 
const [secondColorScale] = useState(new Animated.Value(1));
const [thirdColorScale] = useState(new Animated.Value(1));
const [fourthColorScale] = useState(new Animated.Value(1));

Any ideas?有任何想法吗?

This code worked for me:这段代码对我有用:

const colorsScales = useRef(
    paletteColors.map(
      (_, index) =>
        new Animated.Value(
          index === defaultSelectedColorIndex
            ? selectedColorScale
            : nonSelectedColorScale
        )
    )
  );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM