首页 > 解决方案 > 在视图中滚动时禁用滚动反应本机

问题描述

我在滚动视图中有几个视图组件,但是一个视图在画布上具有绘图功能,我想在画布上绘图时禁用滚动,但在其他视图上不这样做。我一无所知,任何帮助将不胜感激。

<ScrollView>
<View>
    //allow scrolling while pulling on component
  </View>
  <View>
    <Canvas />
    //dont allow scrolling while pulling on component
  </View>
<View>
    //allow scrolling while pulling on component
  </View>
<View>
    //allow scrolling while pulling on component
  </View>
</ScrollView>

标签: react-native

解决方案


export default function Component (){
  const [scrollEnabled, setScrollEnabled] = useState(true);
  return (
    <ScrollView scrollEnabled={scrollEnabled}>
      <View onPress={() => setScrollEnabled(false)} />
    </ScrollView>
  );
}

触摸组件时禁用滚动,不触摸时重新启用滚动。

参考:https ://reactnative.dev/docs/scrollview#scrollenabled


推荐阅读