因此,我试图通过在Reaction Native中以相反的方向将两个图像滑出屏幕来设置它们的动画.这两个图像并排放置,with each image's parent component taking up 50% of the screen width.我希望图像被放置在彼此相邻的位置,它们之间没有任何间隙,并且它们还必须使用resizeMode="contain"来确保图像scale proportionally适合它们各自的容器while preserving their aspect ratios.

我已经try 了我能想到的所有方法,将左侧的Image组件定位到其父组件的最末端,将右侧的Image定位到其父组件的最开始处,以便它们可以彼此接触,但似乎都不起作用.图像始终位于各自容器的中心,如下图所示.

  1. left side上,我有图像的左半部分,我想将其定位在blue container的最后.
  2. right side上,我有图像的右半部分,我想要将其定位在red container的最开始.

enter image description here

以下是我的JSX代码:

return (
      <View
        style={{
          flexDirection: 'row',
        }}
      >
        <Animated.View
          style={{
            width: width / 2,
            height: '100%',
            justifyContent: 'flex-end',
            alignItems: 'flex-end',
            transform: [{ translateX: leftTranslateX }],
            backgroundColor: 'blue',
          }}
        >
          <Image
            source={leftSplash}
            style={{ width: '100%', height: '100%', alignSelf: 'flex-end' }}
            resizeMode="contain"
          />
        </Animated.View>
        <Animated.View
          style={{
            width: width / 2,
            height: '100%',
            justifyContent: 'flex-start',
            alignItems: 'flex-start',
            transform: [{ translateX: rightTranslateX }],
            backgroundColor: 'red',
          }}
        >
          <Image
            source={rightSplash}
            style={{ width: '100%', height: '100%', alignSelf: 'flex-start' }}
            resizeMode="contain"
          />
        </Animated.View>
      </View>
  );

推荐答案

因此,如果理解正确,这就是动画开始前的屏幕外观,并且您希望蓝色容器中的图像接触红色容器中的图像.

您的问题是您在图像组件中使用了width: '100%'.在组件加载时动态修复问题you'll have to calculate the width of your images in pixels.然后,您将用上述像素值替换两个图像中的‘width: '100%'%’值.

下面是一个代码示例:

// Calculate the width of each image based on its aspect ratio.
const leftImageWidthScaled = containerHeight * (leftSplashImage.width / leftSplashImage.height);
const rightImageWidthScaled = containerHeight * (rightSplashImage.width / rightSplashImage.height);

<Image
   source={leftSplashImage}
   style={{ width: leftImageWidthScaled, height: '100%', alignSelf: 'flex-end' }}
   resizeMode="contain"
/>

<Image
   source={leftSplashImage}
   style={{ width: leftImageWidthScaled, height: '100%', alignSelf: 'flex-end' }}
   resizeMode="contain"
/>

React-native相关问答推荐

我们如何才能将我们自己的应用程序(IOS)排除在共享表中?

如何渲染一个 svg 以在不同的窗口大小下动态地看起来相同?

构建 iOS Expo 应用时出现 React-Native xcodebuild 错误 65

每当我在 React Native Tab Navigator 中切换标签时触发 UseEffect

RNMK - 超级表达式必须为空或函数

React Native 白色黑屏问题

No component found for view with name "ARTShape"

TextInput 防止 ScrollView 滚动

无法访问函数内部的状态

未找到 xcode 'boost/config/user.hpp' 文件

如何判断 goBack() 函数在react-navigation 中是否可行?

Fragment片段内的react-native

react-native createbottomtabnavigator 隐藏标签栏标签

如何使用 Stripe (stripe.js) 和 react-native

iOS 模拟器如何确认alert信息

Unexpected token on "export default const"

React本机IOS,当TextInput获得焦点时,按钮按下不注册

在 React Native 中将props传递给外部样式表?

如何在 react-native 中设置 Alert 元素的样式?

React 在任何根目录中都找不到入口文件