【问题标题】:React Native animation - How to move Text from center to left with animation?React Native 动画 - 如何使用动画将文本从中心移动到左侧?
【发布时间】:2023-03-23 14:00:01
【问题描述】:

我需要用动画将文本从中心向左移动,我不能用 screenWidth/2 和 marginLeft 来做到这一点,因为我需要了解文本长度和其他东西才能把它放在中心。

也许有一种方法可以将 Interpolate 与“justifyContent”或“alignItems”一起使用,任何其他方式都将受到欢迎。

   const leftInterpolate = createInterpolate(animatedValue, [0, 1], ['center','flex-start']);
    <Animated.Text style={{justifyContent: leftInterpolate}}>
        {'blabla'}
      </Animated.Text>

更新:

当我尝试第一个答案时出现错误:

问题是我不能将 ref 与 'Animated.View' 一起使用

 <Animated.View ref={ref} />

在 RN 0.61.5

0.61.5 及以下版本的解决方案:

import React from 'react';
import { Animated, Easing, Text, View, StyleSheet } from 'react-native';

export default function App() {
  const ref = React.useRef(View.prototype);
  const animatedValue = React.useRef(new Animated.Value(0)).current;
  const [xPos, setXPos] = React.useState(-1);

  const startAnimation = () => {
    Animated.timing(animatedValue, {
      toValue: 1,
      duration: 1000,
      delay: 1000,
      easing: Easing.linear,
      useNativeDriver: true,
    }).start();
  };

  React.useEffect(() => {
    ref.current?.measure((x, y, w, h, xAbs, yAbs) => {
      setXPos(xAbs);
    });
    startAnimation();
  }, [xPos]);

  const translateX = animatedValue.interpolate({
    inputRange: [0, 1],
    outputRange: [0, -xPos],
  });

  return (
    <View style={styles.container}>
      <Animated.View style={{ transform: [{ translateX }] }}>
        <Text ref={ref}>Some Text</Text>
      </Animated.View>
    </View>
  );
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: 'center',
    alignItems: 'center',
  },
});

【问题讨论】:

    标签: reactjs react-native


    【解决方案1】:

    您可以测量文本的绝对X 位置并相应地进行翻译。试试demo

    import React from 'react';
    import { Animated, Easing, Text, View, StyleSheet } from 'react-native';
    
    export default function App() {
      const ref = React.useRef(View.prototype);
      const animatedValue = React.useRef(new Animated.Value(0)).current;
      const [xPos, setXPos] = React.useState(0);
    
      const startAnimation = () => {
        Animated.timing(animatedValue, {
          toValue: 1,
          duration: 1000,
          delay: 1000,
          easing: Easing.linear,
          useNativeDriver: true
        }).start();
      }
    
      React.useEffect(() => {
        ref.current.measure((x, y, w, h, xAbs, yAbs) => setXPos(xAbs));
        startAnimation();
      }, []);
    
      const translateX = animatedValue.interpolate({
        inputRange: [0, 1],
        outputRange: [0, -xPos]
      })
    
      return (
        <View style={styles.container}>
          <Animated.View ref={ref} style={{ transform: [{ translateX }] }}>
            <Text>Some Text</Text>
          </Animated.View>
        </View>
      );
    }
    
    const styles = StyleSheet.create({
      container: {
        flex: 1,
        justifyContent: 'center',
        alignItems: 'center'
      }
    });
    

    【讨论】:

    • 谢谢,我收到错误“ref.current.measure is undefined”,您使用的是哪个 RN?我使用 RN 0.61.5
    • 我拿了你的代码并试图运行它,我认为你的 RN 版本中的 ref 不一样,因为我使用的是旧版本,这是我在 'ref={ 下得到的 ts 错误ref}' 这里:“类型 '{ children: Element; ref: MutableRefObject; style: { transform: { translateX: AnimatedInterpolation; }[]; }; }' 不能分配给类型 'IntrinsicAttributes & AnimatedProps & { children?: ReactNode; }'。属性 'ref' 不存在于类型 'IntrinsicAttributes & AnimatedProps & { children?: ReactNode; }'。"
    • @barak109 请插入您的代码以找出问题所在。
    • 正如我提到的,我把你的代码从零食复制到一个新文件然后渲染他,所以问题出在 RN 版本中
    • 我更新了我的问题,谢谢你的帮助:),你知道我该如何解决我提到的问题吗?
    猜你喜欢
    • 1970-01-01
    • 2017-01-20
    • 1970-01-01
    • 1970-01-01
    • 2019-08-15
    • 1970-01-01
    • 1970-01-01
    • 2018-03-19
    • 1970-01-01
    相关资源
    最近更新 更多