ReactNative PanResponder极限X位置 [英] ReactNative PanResponder limit X position
本文介绍了ReactNative PanResponder极限X位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在构建音乐播放器,而我专注于进度栏. 我能够对滑动手势做出反应,但是我无法限制该手势进行的程度.
I'm building a Music Player and I'm focusing on the progress bar. I was able to react to swipe gestures, but I cant limit how far that gesture goes.
这是我到目前为止所做的.我已将所有内容减至最少:
This is what I've done so far. I've reduced everything to the minumal:
constructor(props) {
super(props);
this.state = {
pan: new Animated.ValueXY()
};
}
componentWillMount() {
this._panResponder = PanResponder.create({
onMoveShouldSetResponderCapture: () => true,
onMoveShouldSetPanResponderCapture: () => true,
onPanResponderGrant: (e, gestureState) => {
// Set the initial value to the current state
let x = (this.state.pan.x._value < 0) ? 0 : this.state.pan.x._value;
this.state.pan.setOffset({ x, y: 0 });
this.state.pan.setValue({ x: 0, y: 0 });
},
onPanResponderMove: Animated.event([
null, { dx: this.state.pan.x, dy: 0 },
]),
onPanResponderRelease: (e, { vx, vy }) => {
this.state.pan.flattenOffset();
}
});
}
render() {
let { pan } = this.state;
// Calculate the x and y transform from the pan value
let [translateX, translateY] = [pan.x, pan.y];
// Calculate the transform property and set it as a value for our style which we add below to the Animated.View component
let imageStyle = { transform: [{ translateX }, { translateY }] };
return (
<View style={styles.container}>
<Animated.View style={{imageStyle}} {...this._panResponder.panHandlers} />
</View>
);
}
这里有一张显示问题所在的图像.
Here there is an image showing what the problem is.
因此,想法是一旦达到极限(左和右),就停止继续移动.我尝试检查是否_value < 0
,但由于它似乎是偏移量而不是位置,所以它不起作用.
So the idea is to stop keeping moving once the limit (left as well as right) is reached. I tried checking if _value < 0
, but it didn't work since It seems to be an offset, not a position.
我们将不胜感激.
推荐答案
onPanResponderMove: (e, gestureState)=> {
this.state.pan.x._value > 0 ? null : Animated.event([
null,
{dx: this.state.pan.x, dy: this.state.pan.y},
])(e, gestureState)
},
这篇关于ReactNative PanResponder极限X位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文