问题描述
我在 react-native 中的 TouchableOpacity 组件出现间歇性问题。
我基本上点击元素,它正常执行不透明度动画,但我的console.log随机运行。在 iOS 中正常工作。
我的代码:
<TouchableOpacity style={{backgroundColor:'red'}} onPress={async () => {
console.log('Button '+item.tag+' pressed.');
}}>
解决方法
我找到了解决方案!
PanResponder 通过简单的触摸检测微动作。在 onMoveShouldSetPanResponder
参数中,我定义了以下函数:
panResponder = PanResponder.create({
onMoveShouldSetPanResponder: (e,g) => {
return Math.abs(g.dx) > 2 || Math.abs(g.dy) > 2;
},...
});