简体   繁体   English

为什么 Animated.View 中的 ScrollView 不起作用?

[英]why ScrollView inside Animated.View not working?

i want to ask question here, i 've made draggable view using pan responder.我想在这里问问题,我已经使用 pan responder 制作了可拖动视图。 i managed it to only move up and down.我让它只能上下移动。 here is my pan responder config这是我的泛响应器配置

this.panResponder = PanResponder.create({
  onStartShouldSetPanResponder: (event, gesture) => true,
  // THIS TRACK MOVEMENT OF OUR FINGER 
  onPanResponderMove: (event, gesture) => {
    if (gesture.dy > 0) {
      this.position.setValue({ y: gesture.dy });
    }
  },
  onPanResponderRelease: (event, gesture) => {
    if (gesture.dy > SWIPE_THRESHOLD) {
      this.forceSwipeDown();
    } else {
       this.resetPosition();
    }
  },
});

i use it in animated.view, here is the code我在 animated.view 中使用它,这是代码

<Animated.View
       style={[modalCard, this.position.getLayout()]}
       {...this.panResponder.panHandlers}
      >
        {
          // XButton
        }
        <TouchableOpacity style={button} onPress={() => this.props.closeModal()}>
          <Icon 
            name='times'
            size={wp(20)}
            color='#A9A9A9'
          />
        </TouchableOpacity>
        <View style={topBorders}>
          <View style={doubeLine} />
        </View>
        {this.props.children}
      </Animated.View>

my question is, when i put ScrollView as children of this component, the scrollview wont scroll.我的问题是,当我将 ScrollView 作为该组件的子级时,滚动视图不会滚动。 i dont know why is it behave like this, thank you.我不知道为什么会这样,谢谢。

On the ScrollView , there is a default JS pan responder.ScrollView ,有一个默认的 JS 平移响应器。 Here, you create the PanResponder in the Animated.View .在这里,您在Animated.View创建PanResponder In the negotiations process, Animated.View become the responder of the touches, while the ScrollView not.在协商过程中, Animated.View成为触摸的响应者,而ScrollView不是。

Solution:解决方案:

You can capture the touch events outside the ScrollView while ignore the touch events inside the ScrollView .您可以捕获ScrollView外部的触摸事件,而忽略ScrollView内部的触摸事件。 By doing this, the ScrollView can scroll.通过这样做, ScrollView可以滚动。

The code may like this:代码可能是这样的:

    _ignoreRegion = {
      x: 0,
      y: 0,
      width: 0,
      height: 0
    }


    _onLayout = ({
      nativeEvent: {
        layout: {
          x,
          y,
          width,
          height
        }
      }
    }) => {
      this._ignoreRegion = nativeEvent.layout;
    };

    _isInsideRegion = (x, y, region) => {
       return x >= region.x && x <= region.x + region.width && y >= region.y && y <= region.y + region.height;
    }

    this._panResponder = PanResponder.create({
        onMoveShouldSetPanResponderCapture: (evt, gestureState) => {
           if (this._isInsideRegion(evt.pageX, evt.pageY, this._ignoreRegion)) {
             return false; 
           } else {
             return true; 
           }
        },
        // .... 
    })

The JSX JSX

 <Animated.View {...this._panResponder.panHandlers}>            
    <ScrollView onLayout={this._onLayout}/>
</Animated.View> 

I was able to include a ScrollView inside Animated.View by configuring when onMoveShouldSetPanResponder (NOT onMoveShouldSetPanResponderCapture as suggested by another answer ) should return true.通过配置onMoveShouldSetPanResponder (不是另一个答案建议的onMoveShouldSetPanResponderCapture )应该返回 true,我能够在Animated.View中包含一个ScrollView When it returns true, Animated.View will capture the sliding event.当它返回 true 时, Animated.View将捕获滑动事件。 When it returns false, the sliding event is ignored by Animated.View and captured by whatever view is underneath.当它返回 false 时,滑动事件将被Animated.View忽略并被下方的任何视图捕获。 Thus, say we know the x-coord range of the ScrollView is [xLo, xHi] and the y-coord range is [yLo, yHi] , we can create a panResponder as such因此,假设我们知道ScrollView的 x 坐标范围是[xLo, xHi]并且 y 坐标范围是[yLo, yHi] ,我们可以这样创建一个panResponder

const panResponder = React.useRef(
  PanResponder.create({
    onMoveShouldSetPanResponder: (_, {dx, dy, moveX, moveY}) => {
      if (xLo <= moveX && moveX <= xHi && yLo <= moveY && moveY <= yHi) {
        return false;
      }
      return true;
  }),
).current;

This panResponder guarantees that Animated.View ignores all sliding event within the boundary set by [xLo, xHi] and [yLo, yHi] , but registers sliding events outside the boundary.这个panResponder保证Animated.View忽略[xLo, xHi][yLo, yHi]设置的边界内的所有滑动事件,但注册边界外的滑动事件。

[xLo, xHi] and [yLo, yHi] can be obtained by the measure callback. [xLo, xHi][yLo, yHi]可以通过measure回调获取。

Below is a demo implemented using this method.下面是使用该方法实现的一个demo。 For more demos and source code, check the documentation .有关更多演示和源代码,请查看文档

Full disclosure, I am the author of @fanchenbao/react-native-sliding-drawer .完全公开,我是@fanchenbao/react-native-sliding-drawer的作者。

在此处输入图像描述

I am using like this, and it's working on my project:我是这样使用的,它正在我的项目中工作:

<Animated.View>
    <ScrollView>
        { this.renderMultimpleItems() }
    </ScrollView>
</Animated.View>

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

相关问题 当Animated.View将Animated.ScrollView作为子项时,PanResponder在Android上无法正常工作 - PanResponder not working properly on Android when Animated.View has an Animated.ScrollView as a child 在 Animated.View 中反应 Native ScrollView 以制作类似 Bumble 的滑动系统 - React Native ScrollView inside an Animated.View to make Bumble-like swipe system Animated.View 仅适用于展开,不适用于折叠 - Animated.View only works on expanding, not on collapsing PanResponder在第二次拖动时将Animated.View捕捉回原始位置 - PanResponder snaps Animated.View back to original position on second drag 如何使 React Native Animated.View 可点击? - How to make React Native Animated.View clickable? 为 React-Native 应用开玩笑测试 Animated.View - Jest test Animated.View for React-Native app React Native - Animated.View 与常规 View 组件之间的性能差异 - React Native - Performance difference between Animated.View vs regular View component 当要设置动画的项目位于可滚动视图前面时,Panresponder 或 Animated.View 不起作用 - Panresponder or Animated.View doesnt work when the item to animate is in front of a scrollable view 动画可折叠内部动画可折叠无法正常工作 - Animated collapsible inside animated collapsible not working 为什么scrollView在我的代码中不能以垂直方式工作? - Why scrollView is not working in vertical manner in my code?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM