问题描述
我正在尝试使用react-native手势处理程序来实现最底层的行为。我刚刚从here的react-native-gesture-handler复制了最下面的示例。但是它不能正常工作。用户拖动底页时,底页不会上升或下降。
但是有趣的是,它可以在零食上正常工作,但不能在我的设备上工作。这是小吃示例snack example
的链接有人可以帮助我吗? 预先谢谢你
这里是例子
bottomsheet.js
import React,{ Component } from 'react';
import { Animated,StyleSheet,Text,View,Dimensions } from 'react-native';
import {
PanGestureHandler,NativeViewGestureHandler,State,TapGestureHandler,} from 'react-native-gesture-handler';
import { LoremIpsum } from './common';
import { USE_NATIVE_DRIVER } from './config';
const HEADER_HEIGHT = 50;
const windowHeight = Dimensions.get('window').height;
const SNAP_POINTS_FROM_TOP = [50,windowHeight * 0.4,windowHeight * 0.8];
export class BottomSheet extends Component {
masterdrawer = React.createRef();
drawer = React.createRef();
drawerheader = React.createRef();
scroll = React.createRef();
constructor(props) {
super(props);
const START = SNAP_POINTS_FROM_TOP[0];
const END = SNAP_POINTS_FROM_TOP[SNAP_POINTS_FROM_TOP.length - 1];
this.state = {
lastSnap: END,};
this._lastScrollYValue = 0;
this._lastScrollY = new Animated.Value(0);
this._onRegisterLastScroll = Animated.event(
[{ nativeEvent: { contentOffset: { y: this._lastScrollY } } }],{ useNativeDriver: USE_NATIVE_DRIVER }
);
this._lastScrollY.addListener(({ value }) => {
this._lastScrollYValue = value;
});
this._dragY = new Animated.Value(0);
this._onGestureEvent = Animated.event(
[{ nativeEvent: { translationY: this._dragY } }],{ useNativeDriver: USE_NATIVE_DRIVER }
);
this._reverseLastScrollY = Animated.multiply(
new Animated.Value(-1),this._lastScrollY
);
this._translateYOffset = new Animated.Value(END);
this._translateY = Animated.add(
this._translateYOffset,Animated.add(this._dragY,this._reverseLastScrollY)
).interpolate({
inputRange: [START,END],outputRange: [START,extrapolate: 'clamp',});
}
_onHeaderHandlerStateChange = ({ nativeEvent }) => {
if (nativeEvent.oldState === State.BEGAN) {
this._lastScrollY.setValue(0);
}
this._onHandlerStateChange({ nativeEvent });
};
_onHandlerStateChange = ({ nativeEvent }) => {
if (nativeEvent.oldState === State.ACTIVE) {
let { veLocityY,translationY } = nativeEvent;
translationY -= this._lastScrollYValue;
const dragToss = 0.05;
const endOffsetY =
this.state.lastSnap + translationY + dragToss * veLocityY;
let destSnapPoint = SNAP_POINTS_FROM_TOP[0];
for (let i = 0; i < SNAP_POINTS_FROM_TOP.length; i++) {
const snapPoint = SNAP_POINTS_FROM_TOP[i];
const distFromSnap = Math.abs(snapPoint - endOffsetY);
if (distFromSnap < Math.abs(destSnapPoint - endOffsetY)) {
destSnapPoint = snapPoint;
}
}
this.setState({ lastSnap: destSnapPoint });
this._translateYOffset.extractOffset();
this._translateYOffset.setValue(translationY);
this._translateYOffset.flattenOffset();
this._dragY.setValue(0);
Animated.spring(this._translateYOffset,{
veLocity: veLocityY,tension: 68,friction: 12,tovalue: destSnapPoint,useNativeDriver: USE_NATIVE_DRIVER,}).start();
}
};
render() {
return (
<TapGestureHandler
maxDurationMs={100000}
ref={this.masterdrawer}
maxDeltaY={this.state.lastSnap - SNAP_POINTS_FROM_TOP[0]}>
<View style={StyleSheet.absoluteFillObject} pointerEvents="Box-none">
<Animated.View
style={[
StyleSheet.absoluteFillObject,{
transform: [{ translateY: this._translateY }],},]}>
<PanGestureHandler
ref={this.drawerheader}
simultaneousHandlers={[this.scroll,this.masterdrawer]}
shouldCancelWhenOutside={false}
onGestureEvent={this._onGestureEvent}
onHandlerStateChange={this._onHeaderHandlerStateChange}>
<Animated.View style={styles.header} />
</PanGestureHandler>
<PanGestureHandler
ref={this.drawer}
simultaneousHandlers={[this.scroll,this.masterdrawer]}
shouldCancelWhenOutside={false}
onGestureEvent={this._onGestureEvent}
onHandlerStateChange={this._onHandlerStateChange}>
<Animated.View style={styles.container}>
<NativeViewGestureHandler
ref={this.scroll}
waitFor={this.masterdrawer}
simultaneousHandlers={this.drawer}>
<Animated.ScrollView
style={[
styles.scrollView,{ marginBottom: SNAP_POINTS_FROM_TOP[0] },]}
bounces={false}
onScrollBeginDrag={this._onRegisterLastScroll}
scrollEventThrottle={1}>
<LoremIpsum />
<LoremIpsum />
<LoremIpsum />
</Animated.ScrollView>
</NativeViewGestureHandler>
</Animated.View>
</PanGestureHandler>
</Animated.View>
</View>
</TapGestureHandler>
);
}
}
export default class Example extends Component {
render() {
return (
<View style={styles.container}>
<BottomSheet />
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,header: {
height: HEADER_HEIGHT,backgroundColor: 'red',});
App.js
import React from 'react';
import 'react-native-gesture-handler';
import Bottom from './components/bottomSheets'
const App = () => {
return (
<>
<Bottom />
</>
);
};
export default App;
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)