我使用glepur: react-native-swipe-gestures
中的滑动手势它适用于iOS,但我遇到了Android问题。它适用于iOS上的每一次尝试,但Android时不时地工作。以下是我如何使用它:
import GestureRecognizer, { swipeDirections } from 'react-native-swipe-gestures';
constructor(props) {
super(props);
this.state = {
gestureName: 'none',
};
this.onSwipe = this.onSwipe.bind(this);
this.onLeftSwipe = this.onLeftSwipe.bind(this);
this.onRightSwipe = this.onRightSwipe.bind(this);
}
onSwipe(gestureName, gestureState) {
const { SWIPE_LEFT, SWIPE_RIGHT } = swipeDirections;
switch (gestureName) {
case SWIPE_LEFT:
this.swipeLeftFn();
break;
case SWIPE_RIGHT:
this.swipeRightFn();
break;
default:
}
}
}
render() {
const config = {
velocityThreshold: 0.3,
directionalOffsetThreshold: 80,
};
return (
<View style={styles.container}>
<GestureRecognizer
onSwipe={(direction, state) => this.onSwipe(direction, state)}
config={config}
>
<View />
</GestureRecognizer>
</View>
)
}
任何帮助将不胜感激。谢谢!
编辑:绑定函数
答案 0 :(得分:1)
修正了它!
滑动手势似乎是随机工作的,但实际上只是在由<GestureRecognizer>
因此,我删除了最初的<View>
,并将其替换为<GestureRecognizer>
。它看起来像这样:
return (
<GestureRecognizer
onSwipe={(direction, state) => this.onSwipe(direction, state)}
config={config}
style={styles.container}
>
<View />
</GestureRecognizer>
)
答案 1 :(得分:0)
对我来说同样的问题,但无法解决 react-native-swipe-gestures
我找到了更简单的解决方案,即使用此库 - 完美运行:
<View
onTouchStart={e=> this.touchY = e.nativeEvent.pageY}
onTouchEnd={e => {
if (this.touchY - e.nativeEvent.pageY > 20)
console.log('Swiped up')
}}
style={{height: 300, backgroundColor: '#ccc'}}
/>