下面是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();
}
});
如何从其他方法调用onPanResponderGrant方法?
someMethod = () => {
// call the pan responder function here
// this.panResponder.onPanResponderGrant();
}
答案 0 :(得分:2)
为什么不只是将该函数变成组件上的方法?
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()
并致电