代码之家  ›  专栏  ›  技术社区  ›  Nejib Afdhal

如何在带有类名的react native中使用顺风动画?

  •  0
  • Nejib Afdhal  · 技术社区  · 5 月前

    我在expo-react原生项目中尝试使用Tailwind为视图或按钮等组件制作动画,但没有成功。此外,在网站文档中,我注意到它支持动画和转换 强健的动画-通过react native reanimated完全支持Tailwind的动画类和自定义关键帧动画

    过渡-样式状态之间的平滑过渡,包括暗模式更改和动态更新
    https://www.nativewind.dev/

     <Button title='press' className="transition ease-in-out delay-150 bg-blue-500 hover:-translate-y-1 hover:scale-110 hover:bg-indigo-500 duration-300 ..."/>
    
    1 回复  |  直到 5 月前
        1
  •  0
  •   Victor Rutskin    5 月前
    1. 确保您安装了所需的依赖项:

      npm install nativewind react-native-reanimated

      如果您正在使用expo:

      npm install --save-dev babel-plugin-tailwindcss-react-native

    2. 配置你的babel.config.js,你的babel-config.js应该看起来像这样:

    module.exports = function (api) {
      api.cache(true);
      return {
        presets: [
          ["babel-preset-expo", { jsxImportSource: "nativewind" }],
          "nativewind/babel",
        ],
        plugins: ["react-native-reanimated/plugin",'babel-plugin-dotenv'],
      };
    };
    1. 现在,您还需要为项目配置tailwind.config.js, 如果你没有,那么:

      npx tailwindcss init

      然后将内容更改为:

    /** @type {import('tailwindcss').Config} */
    module.exports = {
      content: [
        "./app/**/*.{js,jsx,ts,tsx}",
        "./components/**/*.{js,jsx,ts,tsx}",
      ],
    ...
    }
    1. 将其导入到root_layout中: import 'react-native-reanimated';

    2. 现在,在确保您已正确配置所有内容后,这是一个如何在tailwind.config.js中定义自己的动画的示例:

    module.exports = {
      theme: {
        extend: {
          animation: {
            'fade-in': 'fadeIn 1s ease-in-out',
          },
          keyframes: {
            fadeIn: {
              '0%': { opacity: 0 },
              '100%': { opacity: 1 },
            },
          },
        },
      },
    };

    然后像这样使用它:

    <View className="animate-fade-in">
      <Text>Hello, world!</Text>
    </View>