RACT本地动画API:我可以正确移动元素,并在使用React本机动画API时在元素之间留出空间

发布于 2025-02-04 17:48:14 字数 3792 浏览 4 评论 0原文

我正在尝试在React Native

并使用React Native Animation以实现它。 我按照预期的方式编写了代码的

     const itemSize = AppDevice.width / 4;
     const marginValue = 5;

   const HomeScreen = ({navigation}) => {
   let videoRef = useRef();
   const scrollX = useRef(new Animated.Value(0)).current;
   const marginX = useRef(new Animated.Value(0)).current;
   let carouselRef = useRef();
   let [loading, setLoading] = useState(true);
   let [MainCategory, setMainCategory] = useState();
   let [currentIndex, setcurrentIndex] = useState();

    useEffect(() => {
    const renderCarousel = () => {
     return (
   <Animated.FlatList
    data={MainCategory}
    horizontal
    showsHorizontalScrollIndicator={false}
    snapToInterval={itemSize + marginValue * 2}
    bounces={false}
    decelerationRate={0}
    style={{backgroundColor: 'red'}}
    scrollEventThrottle={16}
    onScroll={Animated.event(
      [{nativeEvent: {contentOffset: {x: scrollX}}}],
      {useNativeDriver: true},
    )}
    onMomentumScrollEnd={e =>
      setcurrentIndex(
        Math.ceil(
     e.nativeEvent.contentOffset.x /(itemSize + marginValue * 2),//to find active item
        ),
      )
    }
      renderItem={({item, index}) => {
      const inputRange = [
        (index - 2) * (itemSize + marginValue * 2), //Before 2 items of the active item
        (index - 1) * (itemSize + marginValue * 2), //Before 1 items of the active item
        index * (itemSize + marginValue * 2),       //active item
        (index + 1) * (itemSize + marginValue * 2),//after 1 items of the active item
        (index + 2) * (itemSize + marginValue * 2),//after 2 items of the active item
      ];

      const translateY = scrollX.interpolate({   //To change the size of items
        inputRange,
        outputRange: [0.6, 0.9, 1.25, 0.9, 0.6],  
      });

      const inputRange2 = [   
        (index - 2) * (itemSize + marginValue),
        (index - 1) * (itemSize + marginValue),
        index * (itemSize + marginValue),
        (index + 1) * (itemSize + marginValue),
        (index + 2) * (itemSize + marginValue),
      ];
      const margin = marginX.interpolate({   //to add margine to items  ((Here it does 
                                                not work as expectedcorrectly
        inputRange,
        outputRange: [-20, 10, 0, 10, -20],   // 
      });
      return (
        <Animated.View
          style={{
            marginTop: 50,
            width: itemSize,
            height: itemSize,
            justifyContent: 'space-between',
            marginHorizontal:
              index != 0 && index + 1 != MainCategory.length    //to center first item
                ? marginValue
                : 0,
            marginStart:
              index == 0
                ? itemSize * 2 - itemSize / 2 + marginValue
                : marginValue,
            marginEnd:                                          //to center last item
              index + 1 == MainCategory.length
                ? itemSize + itemSize / 2 - marginValue
                : marginValue,
            padding: 20,
            borderRadius: 20,
            backgroundColor: '#FFF',
            transform: [{scale: translateY}, {translateX: margin}],
          }}>
          <Image
            style={{width: '100%', height: '100%', resizeMode: 'contain'}}
            source={require('../assets/images/open-box.png')}
          />
        </Animated.View>
      );
    }}
  />
);
 };
   return (
   <View style={{flex: 1}}>
   {renderCarousel()}
   </View>
 );
 };

大小变化,但是问题在于我想在元素之间留出空间,但是它对我的预期不起作用

是否有人可以帮助我, 提前致谢

I am trying to implement this carousel in React Native

enter image description here

and use React Native Animated to implement it.
I wrote the code as follows

     const itemSize = AppDevice.width / 4;
     const marginValue = 5;

   const HomeScreen = ({navigation}) => {
   let videoRef = useRef();
   const scrollX = useRef(new Animated.Value(0)).current;
   const marginX = useRef(new Animated.Value(0)).current;
   let carouselRef = useRef();
   let [loading, setLoading] = useState(true);
   let [MainCategory, setMainCategory] = useState();
   let [currentIndex, setcurrentIndex] = useState();

    useEffect(() => {
    const renderCarousel = () => {
     return (
   <Animated.FlatList
    data={MainCategory}
    horizontal
    showsHorizontalScrollIndicator={false}
    snapToInterval={itemSize + marginValue * 2}
    bounces={false}
    decelerationRate={0}
    style={{backgroundColor: 'red'}}
    scrollEventThrottle={16}
    onScroll={Animated.event(
      [{nativeEvent: {contentOffset: {x: scrollX}}}],
      {useNativeDriver: true},
    )}
    onMomentumScrollEnd={e =>
      setcurrentIndex(
        Math.ceil(
     e.nativeEvent.contentOffset.x /(itemSize + marginValue * 2),//to find active item
        ),
      )
    }
      renderItem={({item, index}) => {
      const inputRange = [
        (index - 2) * (itemSize + marginValue * 2), //Before 2 items of the active item
        (index - 1) * (itemSize + marginValue * 2), //Before 1 items of the active item
        index * (itemSize + marginValue * 2),       //active item
        (index + 1) * (itemSize + marginValue * 2),//after 1 items of the active item
        (index + 2) * (itemSize + marginValue * 2),//after 2 items of the active item
      ];

      const translateY = scrollX.interpolate({   //To change the size of items
        inputRange,
        outputRange: [0.6, 0.9, 1.25, 0.9, 0.6],  
      });

      const inputRange2 = [   
        (index - 2) * (itemSize + marginValue),
        (index - 1) * (itemSize + marginValue),
        index * (itemSize + marginValue),
        (index + 1) * (itemSize + marginValue),
        (index + 2) * (itemSize + marginValue),
      ];
      const margin = marginX.interpolate({   //to add margine to items  ((Here it does 
                                                not work as expectedcorrectly
        inputRange,
        outputRange: [-20, 10, 0, 10, -20],   // 
      });
      return (
        <Animated.View
          style={{
            marginTop: 50,
            width: itemSize,
            height: itemSize,
            justifyContent: 'space-between',
            marginHorizontal:
              index != 0 && index + 1 != MainCategory.length    //to center first item
                ? marginValue
                : 0,
            marginStart:
              index == 0
                ? itemSize * 2 - itemSize / 2 + marginValue
                : marginValue,
            marginEnd:                                          //to center last item
              index + 1 == MainCategory.length
                ? itemSize + itemSize / 2 - marginValue
                : marginValue,
            padding: 20,
            borderRadius: 20,
            backgroundColor: '#FFF',
            transform: [{scale: translateY}, {translateX: margin}],
          }}>
          <Image
            style={{width: '100%', height: '100%', resizeMode: 'contain'}}
            source={require('../assets/images/open-box.png')}
          />
        </Animated.View>
      );
    }}
  />
);
 };
   return (
   <View style={{flex: 1}}>
   {renderCarousel()}
   </View>
 );
 };

The size changes as expected, but the problem is that I want to leave spaces between the elements, but it does not work for me as expected

enter image description here

Is there anyone who can help me, thanks in advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

·深蓝 2025-02-11 17:48:14

我终于发现我刚刚替换了参考文献,从marginx scrollx

I finally found I just replaced the ref, from marginX to scrollX

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文