我是 React Native 的新手。我有一个应用程序,其中包含一个 ScrollView 组件和几个自定义组件。我想从 ScrollView 触发一个 onPressOut 事件,以便当用户滚动和释放时,ScrollView 内的子组件更新它们的状态。但是,ScrollView 文档不包括任何新闻事件:https://facebook.github.io/react-native/docs/scrollview.html

我尝试过的其他事情:

  • 我尝试将滚动 View 包装在 TouchableWithoutFeedback 组件中,当我这样做时,我在尝试滚动时立即收到错误消息:



  • 不知道是什么意思...
    有任何想法吗?谢谢!

    这是我的代码:
    <View ref="theWrapperView" style={styles.theWrapperView}>
                <ScrollView
                    onScroll={this.onScroll.bind(this)}
                    onMomentumScrollEnd={this.onScrollAnimationEnd.bind(this)}
    
                    style={styles.scrollView}
                    contentContainerStyle={styles.scrollViewContentContainer}
    
                    showsHorizontalScrollIndicator={true}
                    scrollEventThrottle={10}
                    pagingEnabled={true}
                    horizontal={true}
                    automaticallyAdjustContentInsets={false}
                    snapToAlignment={'center'}
                    snapToInterval={20}
                    zoomScale={1.5}
                    centerContent = {true}
    
                >
                    {cards}
                </ScrollView>
            </View>
    

    当我将 View 元素更改为 TouchableWithoutFeedback 元素时,我收到了错误消息。

    最佳答案

    我在研究 React Native 源代码后发现了这一点:https://github.com/facebook/react-native/blob/master/Libraries/Components/ScrollResponder.js

    事实证明,实际上还有许多其他事件可通过 ScrollResponder mixin 使用,但在线文档中并未提及这些事件。不需要包含 mixin 来访问这些事件,因为它们已经是 ScrollView 组件的一部分。对我有用的是 onResponderRelease,它会在您释放 ScrollView 时触发,如下所示:

    <View ref="theWrapperView" style={styles.theWrapperView}>
                <ScrollView
                    onScroll={this.onScroll.bind(this)}
                    onMomentumScrollEnd={this.onScrollAnimationEnd.bind(this)}
                    onResponderRelease = {this.onResponderReleaseHandler.bind(this)}
                >
                    {cards}
                </ScrollView>
            </View>
    

    并在类中定义一个处理程序:
    onResponderRelease(){
    //do stuff
    
    }
    

    关于ios - 如何从 React Native 中的 ScrollView 组件获取 onPress 事件,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/33350556/

    10-12 00:46
    查看更多