React Native【实战范例】同步跟随滚动

最终效果

实现原理

主动滚动区触发滚动事件,原生监听滚动值的变化,并用动画的方式实时同步到跟随滚动区

技术要点

  • 使用 Animated.ScrollView

  • 使用动画变量

    c 复制代码
    const scrollY = useRef(new Animated.Value(0)).current;
  • 主动滚动触发 onScroll,用 Animated.event 实现原生绑定

    c 复制代码
              onScroll={Animated.event(
                [
                  {
                    nativeEvent: {
                      contentOffset: { y: scrollY },
                    },
                  },
                ],
                { useNativeDriver: true }
              )}
  • 跟随滚动

    c 复制代码
    transform: [{ translateY: Animated.multiply(-1, scrollY) }],

范例代码

c 复制代码
import React, { useRef } from "react";
import { Animated, StyleSheet, View } from "react-native";
const colors = ["red", "green", "blue", "yellow", "orange"];
export default function Demo() {
  const scrollY = useRef(new Animated.Value(0)).current;
  const viewList = () => {
    const array = [
      1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
    ];
    return (
      <>
        {array.map((item, index) => (
          <View
            key={item}
            style={{
              width: 60,
              height: 100,
              backgroundColor: colors[index % 5],
            }}
          />
        ))}
      </>
    );
  };
  return (
    <View style={styles.root}>
      <View style={styles.leftLayout}>
        <Animated.View
          style={{
            width: 60,
            transform: [{ translateY: Animated.multiply(-1, scrollY) }],
          }}
        >
          {viewList()}
        </Animated.View>
      </View>
      <View style={styles.rightLayout}>
        <Animated.ScrollView
          showsVerticalScrollIndicator={false}
          onScroll={Animated.event(
            [
              {
                nativeEvent: {
                  contentOffset: { y: scrollY },
                },
              },
            ],
            { useNativeDriver: true }
          )}
        >
          {viewList()}
        </Animated.ScrollView>
      </View>
    </View>
  );
}
const styles = StyleSheet.create({
  root: {
    width: "100%",
    height: "100%",
    flexDirection: "row",
    justifyContent: "center",
  },
  leftLayout: {
    width: 60,
    backgroundColor: "#00FF0030",
    flexDirection: "column",
  },
  rightLayout: {
    width: 60,
    height: "100%",
    backgroundColor: "#0000FF30",
    marginLeft: 100,
  },
});
相关推荐
冰冷的bin9 小时前
【React Native】点赞特效动画组件FlowLikeView
react native·react.js·typescript
懒人村杂货铺16 小时前
[特殊字符] 跨端视频通话实战:腾讯云 TRTC + IM(React Native & Web)
react native·音视频·腾讯云
木西1 天前
React Native DApp 开发全栈实战·从 0 到 1 系列(流动性挖矿-前端部分)
react native·web3·智能合约
小妖怪的夏天2 天前
react native 出现 FATAL EXCEPTION: OkHttp Dispatcher
react native·react.js·okhttp
qczg_wxg2 天前
高阶组件介绍
开发语言·javascript·react native·ecmascript
qczg_wxg4 天前
React Native的动画系统
javascript·react native·react.js
qczg_wxg4 天前
React Native常用的API
react native
Winson℡4 天前
在 React Native 层禁止 iOS 左滑返回(手势返回/手势退出)
react native·react.js·ios
卸任4 天前
从0到1搭建react-native自动更新(OTA和APK下载)
前端·react native·react.js
wayne2144 天前
「原生 + RN 混合工程」一条命令启动全攻略:解密 react-native.config.js
android·react native