[英]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
不是。
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.