首页 > 解决方案 > React 本机动画与 Android 崩溃

问题描述

对这个问题非常沮丧。

我正在设计一个需要动画的游戏。在 Expo 中的 Web 部件一切正常。组件如下:

return (  <TouchableWithoutFeedback onPress={selectTarget}>
      <Animated.View
        style={{
          width: 50,
          height: 50,
          backgroundColor: 'yellow',
          zIndex: 2,
          position: 'absolute',
          alignItems: 'center',
          justifyContent: 'center',
          **transform: [
            { translateX: moveAnimation.x },
            { translateY: moveAnimation.y },
          ],**
          elevation: 2,
          left: 0, 
          top: 0,
        }}>
        <Image style={[imageStyle]} source={imagesAssets[color]} />
      </Animated.View>
    </TouchableWithoutFeedback> );

但是它在 Android 上崩溃了(适用于 iOS),错误如下:更新由 RCTView 管理的视图的“转换”时出错

如果我注释掉转换位,它运行良好。

由于性能问题,我在动画配置中添加了 useDeviceDriver:true,所以我必须使用变换。

谁能帮我解决这个问题?

标签: react-nativereact-native-android

解决方案


终于找到问题的原因了:

在 moveAnimation 计算过程中出现问题,未定义的值会使应用程序崩溃,而不是将其视为 0。


推荐阅读