Fac*_*cca 7 android gestures react-native
我正在建立一个音乐播放器,我专注于进度条.我能够对滑动手势做出反应,但我无法限制手势的移动距离.
这就是我到目前为止所做的.我把所有东西都减少到了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>
);
}
Run Code Online (Sandbox Code Playgroud)
这里有一个图像显示问题所在.
因此,一旦达到极限(左侧和右侧),该想法就是停止保持运动.我试过检查是否_value < 0,但它不起作用,因为它似乎是一个偏移,而不是一个位置.
那么任何帮助将不胜感激.
小智 5
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)
},
Run Code Online (Sandbox Code Playgroud)
您可以使用 y=x 插入您的 Animated.Value,而不是让您的动画在边界处消失,但将其限制在您的宽度上。
return (
<View style={styles.container}>
<Animated.View
style={{
transform: [{
translateX: this.state.pan.x.interpolate({
inputRange: [0, trackWidth ],
outputRange: [0, trackWidth ],
extrapolate: 'clamp'
})
}],
}}
{...this._panResponder.panHandlers}
/>
</View>
);
Run Code Online (Sandbox Code Playgroud)
这是一个更深入的示例:https : //github.com/olapiv/expo-audio-player/blob/master/src/AudioSlider.js
| 归档时间: |
|
| 查看次数: |
2932 次 |
| 最近记录: |