简体   繁体   English

React Native ScrollView 防止/允许在滚动开始事件上滚动

[英]React Native ScrollView prevent/allow scrolling on scroll start event

I have a ScrollView (actually Animated.ScrollView ) that I'd like to decide whether to allow vertically scroll or not on scroll start, based on some states of inner components.我有一个 ScrollView (实际上是Animated.ScrollView ),我想根据内部组件的某些状态来决定是否允许在滚动开始时垂直滚动。 I know there is a scrollEnabled property which works but the scroll view contains some components that that a few frames to render and changing scrollEnabled property based on inner views' interactions cause a re-render, which drops a few frames and because of the nature of my app needing to change this property based on some animation/smooth scrolling of the inner views, those dropped frames cause a negative user experience.我知道有一个scrollEnabled属性可以工作,但是滚动视图包含一些组件,这些组件可以渲染几帧,并且基于内部视图的交互更改scrollEnabled属性会导致重新渲染,这会丢失一些帧,并且由于我的应用需要根据内部视图的一些动画/平滑滚动来更改此属性,这些丢帧会导致负面的用户体验。

I've also tried preventDefault of the scroll begin drag event but nothing changed (it allows scrolling regardless of I call it or not).我也尝试了滚动开始拖动事件的preventDefault但没有任何改变(无论我是否调用它,它都允许滚动)。

How can I decide whether or not to allow scrolling of scroll view dynamically on scroll start without a state/props update (which causes a re-render of an expensive view tree, dropping frames)?我如何决定是否允许在滚动开始时动态滚动滚动视图而无需状态/道具更新(这会导致重新渲染昂贵的视图树,丢帧)? Something like pan responder's onMoveShouldSetPanResponder[Capture] events might be of great help but returning false from them just don't work (it captures and scrolls regardlessly even though they are called).像 pan 响应者的onMoveShouldSetPanResponder[Capture]事件可能会有很大帮助,但从它们返回false是行不通的(即使它们被调用,它也会捕获和滚动)。 (I'm on React Native 0.64) (我在 React Native 0.64 上)

After reading the post in https://codedaily.io/courses/Master-React-Native-Animations/Using-and-Understanding-setNativeProps I solved it by setting native props on scroll view (that I reference directly using a ref ) instead of updating state, preventing a re-render:在阅读https://codedaily.io/courses/Master-React-Native-Animations/Using-and-Understanding-setNativeProps中的帖子后,我通过在滚动视图上设置本机道具(我直接使用ref )来解决它更新 state,防止重新渲染:

this.state.scrollView.current?.setNativeProps({
    scrollEnabled: false
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM