react-native-reanimated/react-native-gesture-handler动画不响应

        首先需要保证react-native-reanimated按照官方文档配置正确,在2.5.X版之后的配置相对简单,使用

yarn add react-native-reanimated

安装后只需要在babel.config.js中添加一个插件

react-native-reanimated/plugin

即可,如需使用之前版本详见官方文档。

react-native-gesture-handler安装时需要注意版本对应

 然后这里许多同学在测试时就会发现,明明代码和官方文档一摸一样就是动画不能工作。问题可能出现在测试代码的位置,如果测试代码直接写在了新建项目默认的App.js中,或其他使用

export default 方法导出的组件中,就会出现这样的问题。问题并不在reanimated库中,而是在通常会一起使用的react-native-gesture-handler这一库中

参见react-native-gesture-handler官方文档中

 

 这里说到手势在export default 方法导出的组件中不能工作,可以使用gestureHandlerRootHOC函数包裹需要放入组件的节点,不过这里更推荐前文提到的另一种方法。

直接使用

<GestureHandlerRootView>作为页面的根节点,比较简单,可以使用flex:1使根节点填充页面 。

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
react-native-reanimated 是一个 React Native 库,用于实现高性能、流畅的动画和交互效果。在 Android 中,react-native-reanimated 的使用方式与在 iOS 中基本相同。以下是 react-native-reanimated 在 Android 中的用法: 1. 安装 react-native-reanimated 在项目根目录中运行以下命令: ``` npm install react-native-reanimated ``` 或者使用 yarn: ``` yarn add react-native-reanimated ``` 2. 连接 react-native-reanimated 在项目根目录中运行以下命令: ``` react-native link react-native-reanimated ``` 3. 在代码中使用 在需要使用 react-native-reanimated 的组件或页面中,首先导入库: ```javascript import Animated from 'react-native-reanimated'; ``` 然后就可以使用 Animated 中提供的各种动画和交互效果了。例如,以下代码实现了一个在点击时平移的按钮: ```javascript import React, { useState } from 'react'; import { View, TouchableOpacity, StyleSheet } from 'react-native'; import Animated, { useAnimatedStyle, useSharedValue, withSpring } from 'react-native-reanimated'; const App = () => { const [isPressed, setIsPressed] = useState(false); const translateX = useSharedValue(0); const handlePressIn = () => { setIsPressed(true); translateX.value = withSpring(-50); }; const handlePressOut = () => { setIsPressed(false); translateX.value = withSpring(0); }; const animatedStyle = useAnimatedStyle(() => { return { transform: [{ translateX: translateX.value }], }; }); return ( <View style={styles.container}> <TouchableOpacity onPressIn={handlePressIn} onPressOut={handlePressOut} activeOpacity={0.8} > <Animated.View style={[styles.button, animatedStyle]}> <Text style={styles.text}>{isPressed ? 'Pressed' : 'Press me'}</Text> </Animated.View> </TouchableOpacity> </View> ); }; const styles = StyleSheet.create({ container: { flex: 1, alignItems: 'center', justifyContent: 'center', }, button: { width: 120, height: 40, borderRadius: 20, backgroundColor: 'blue', alignItems: 'center', justifyContent: 'center', }, text: { color: 'white', fontWeight: 'bold', }, }); export default App; ``` 在这个例子中,我们使用了 useSharedValue 创建了一个共享变量 translateX,表示按钮在 x 轴方向的平移量。在 handlePressIn 和 handlePressOut 中,我们分别更新了按钮的状态和 translateX 的值,使用 withSpring 让按钮平滑地过渡到新的位置。最后,我们使用 useAnimatedStyle 创建了一个动画样式 animatedStyle,并将其应用到了按钮的样式中。 以上就是 react-native-reanimated 在 Android 中的基本用法。要了解更多关于 react-native-reanimated 的内容,请参考官方文档。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值