下面是 componentWillMount() 方法中 panResponder 的代码。
this.panResponder = PanResponder.create({
onStartShouldSetPanResponder: (e, gesture) => true,
onPanResponderGrant: (evt, gestureState) => {
this.state.pan.setOffset(this.state.pan.__getValue());
this.state.pan.setValue({ x: 0, y: 0 });
Animated.spring(this.state.pan, {toValue: {x:100, y:100}}).start();
}
});
如何从其他方法调用PanResponderGrant方法?
someMethod = () => {
// call the pan responder function here
// this.panResponder.onPanResponderGrant();
}
为什么不把这个函数变成组件上的方法呢?
class MyComponent extends React.Component {
constructor(props){
super(prop)
this.panResponder = PanResponder.create({
onStartShouldSetPanResponder: (e, gesture) => true,
onPanResponderGrant: this.onResponderGrant,
});
}
onResponderGrant = () => {
this.state.pan.setOffset(this.state.pan.__getValue());
this.state.pan.setValue({ x: 0, y: 0 });
Animated.spring(this.state.pan, {toValue: {x:100, y:100}}).start();
}
}
现在您可以随心所欲地四处走动并打电话给onResponderGrant()