ios - 在平移手势处理程序中禁用滚动视图的滚动不会触发本机反应中的平移手势事件
问题描述
所以我有这个模态底部表,里面有一个滚动视图。当滚动 y 偏移量为 0(滚动到顶部)时,应禁用滚动并触发平移手势。平移手势基本上沿 y 轴平移模态。这是代码,
import React, { useState, useEffect, useRef } from "react"
import { View, Text, TouchableOpacity } from "react-native"
import { PanGestureHandler, ScrollView } from "react-native-gesture-handler"
import Animated, {
useAnimatedGestureHandler,
useAnimatedStyle,
useSharedValue,
withTiming
} from "react-native-reanimated"
const TestComponent: React.FC = () => {
const panRef = useRef<PanGestureHandler>(null)
const scrollRef = useRef<ScrollView>(null)
const [scrollEnabled, setScrollEnabled] = useState(true)
const y = useSharedValue(400)
const animateY = useAnimatedStyle(() => {
return {
transform: [{ translateY: y.value }]
}
})
const swipe = useAnimatedGestureHandler({
onStart: (e, ctx: { startY: number }) => {
ctx.startY = y.value
},
onActive: (e, ctx) => {
y.value = ctx.startY + e.translationY
}
})
useEffect(() => {
y.value = withTiming(0)
}, [])
return (
<View
style={{ paddingVertical: 50, flex: 1, backgroundColor: "black" }}>
<TouchableOpacity onPress={() => (y.value = 0)}>
<Text style={{ color: "white" }}>Reset</Text>
</TouchableOpacity>
<PanGestureHandler ref={panRef} onGestureEvent={swipe}>
<Animated.View
style={[
{
position: "absolute",
bottom: 0,
backgroundColor: "green",
height: 400,
width: "100%"
},
animateY
]}>
<ScrollView
ref={scrollRef}
scrollEventThrottle={16}
scrollEnabled={scrollEnabled}
onScrollBeginDrag={e => {
const offset = e.nativeEvent.contentOffset
if (offset.y <= 0) {
setScrollEnabled(false)
scrollRef.current?.scrollTo({ x: 0, y: 0 })
}
}}
waitFor={scrollEnabled ? scrollRef : panRef}>
{new Array(30).fill(1).map((x, i) => {
return (
<View key={i.toString()}>
<Text>#Item {i.toString()}</Text>
</View>
)
})}
</ScrollView>
</Animated.View>
</PanGestureHandler>
</View>
)
}
export default TestComponent
但是,即使我在第一次尝试禁用滚动时也不会触发平移手势。但是在第二次尝试时它可以工作。
解决方案
推荐阅读
- r - dismo 包中的 MESS 地图是如何计算的?
- python - 如何缩短此代码以使日期 SQL 兼容?
- reactjs - React 输入验证在表单中不起作用
- node.js - 用于 NodeJS CPU 分析的 Chrome DevTools 中缺少记录按钮
- python - Openpyxl TypeError:尝试加载现有工作簿时值必须是序列
- android - 发布模式下 Flutter android 灰屏即使在调试模式下没有错误或红屏
- python - 如何使用 google.colab 导入文件上传文本文件,然后将其转换为一组单词?
- docusignapi - 为什么附件对所有收件人都可见,即使链接的文档也不可见
- javascript - 通过 MongoDB 的 Node.js 登录表单
- batch-file - 为什么“移动”命令在我的批处理文件中不起作用?