![](/img/trans.png)
[英]How do I make a secondary <View> enter a main view with the PanResponder?
[英]How do I call a custom function in a parent class when panResponder starts moving?
如果我在react-native中有一個名為<Draggable />
的導入組件,
當onPanResponderGrant
確定手勢已開始時,如何在父組件中調用自定義函數?
您會看到我正在將id
'A'
和'B'
傳遞給<Draggable />
,我希望能夠通過觸摸回叫它們,並將它們顯示在底部的信息infoBox
。主要View
?
// App
import React, { Component } from 'react';
import { View, Text, } from 'react-native';
import styles from './cust/styles';
import Draggable from './cust/draggable';
export default class Viewport extends Component {
constructor(props){
super(props);
this.state = {
dID : null,
};
}
render(){
return (
<View style={styles.mainContainer}>
<View style={styles.draggableContainer}>
<Text>Draggable Container</Text>
<Draggable id='A' />
<Draggable id='B' />
</View>
<View style={styles.infoBar}>{this.infoBarData()}</View>
</View>
);
}
infoBarData(){
if (this.state.dID) {
return(
<Text>{this.state.dID}</Text>
)
}
}
}
和
// Draggable
import React, { Component } from 'react';
import { Text, PanResponder, Animated, } from 'react-native';
import styles from './styles';
class Draggable extends Component {
constructor(props) {
super(props);
this.state = {
pan : new Animated.ValueXY(),
};
this.panResponder = PanResponder.create({
onStartShouldSetPanResponder : () => true,
onPanResponderMove : Animated.event([null,{
dx : this.state.pan.x,
dy : this.state.pan.y,
}]),
onPanResponderRelease : () => {
Animated.spring(this.state.pan,{toValue:{x:0, y:0}}).start();
}
});
}
render() {
return (
<Animated.View
{...this.panResponder.panHandlers}
style={[this.state.pan.getLayout(), styles.circleAlt, styles.position]}>
<Text style={styles.textAlt}>Drag me!</Text>
<Text style={styles.textNum}>{this.props.id}</Text>
</Animated.View>
)
}
}
export default Draggable;
編輯
我已將以下內容添加到父類中
// Object
<Draggable
onPanResponderMove={this.onStopMove}
onPanResponderRelease={this.onMove}
id='A' />
// Methods
onMove = (dID) => {
this.setState({ dID });
}
onStopMove = () => {
this.setState({ dID: null });
}
並將以下內容添加到Draggable
類中。
// Methods
_handleOnPanResponderMove(evt, gestureState) {
Animated.event([null,{
// These animate movement on the X/Y axis
dx : this.state.pan.x,
dy : this.state.pan.y,
}]);
this.props.onPanResponderRelease(this.props.id);
}
但是當我將動畫事件移出PanResponder.create({})
通過執行以下操作,它失去了被拖動的能力。 我認為這與
PanResponder.create({
...,
onPanResponder : this._handleOnPanResponder.bind(this),
...,
})
沒有返回值?
編輯2
我也嘗試添加以下內容,但再次失敗了。
PanResponder.create({
...,
onPanResponder : (evt ,gesture) => {
Animated.event([null,{
// These animate movement on the X/Y axis
dx : this.state.pan.x,
dy : this.state.pan.y,
}]);
this.props.onPanResponderRelease(this.props.id);
}
...,
})
您需要使用Draggable組件傳遞回調處理程序,例如
<Draggable id="A" gestureHandler={(data) => {
/*
do whatever you want to do with the data, like store it into state
*/
}}
在您的Draggable組件中,根據需要調用此處理程序(確定手勢已開始),例如
if (this.props.gestureHandler instanceof Function) {
this.props.gestureHandler.call(this, this.props.id);
}
好,
因此,這里的代碼適用於可能遇到此問題的任何人。
// App
import React, { Component } from 'react';
import {
View,
Text,
} from 'react-native';
import styles from './cust/styles';
import Draggable from './cust/draggable';
export default class Viewport extends Component {
constructor(props){
super(props);
this.state = {
dID : null,
};
}
render(){
return (
<View style={styles.mainContainer}>
<View style={styles.draggableContainer}>
<Text>Draggable Container</Text>
<Draggable
onPanResponderGrant={this.onMove}
onPanResponderRelease={this.onStopMove}
id='A' />
<Draggable
onPanResponderGrant={this.onMove}
onPanResponderRelease={this.onStopMove}
id='B' />
</View>
<View style={styles.infoBar}>{this.printInfo()}</View>
</View>
);
}
onMove = (dID) => {
this.setState({ dID });
}
onStopMove = () => {
this.setState({ dID: null });
}
printInfo(){
if (this.state.dID) {
return(
<Text>{this.state.dID}</Text>
);
}
}
}
// Draggable
import React, { Component } from 'react';
import {
Text,
PanResponder,
Animated,
} from 'react-native';
import styles from './styles';
class Draggable extends Component {
constructor(props) {
super(props);
this.state = {
pan : new Animated.ValueXY(),
};
this.panResponder = PanResponder.create({
onStartShouldSetPanResponder : () => true,
onPanResponderGrant : () => {
this.props.onPanResponderGrant(this.props.id)
},
onPanResponderMove : Animated.event([null,{
dx : this.state.pan.x,
dy : this.state.pan.y,
}]),
onPanResponderRelease : () => {
Animated.spring(
this.state.pan,
{toValue:{x:0, y:0}}
).start();
this.props.onPanResponderRelease();
},
});
}
render() {
return (
<Animated.View
{...this.panResponder.panHandlers}
style={[this.state.pan.getLayout(), styles.circleAlt, styles.position]}>
<Text style={styles.textAlt}>Drag me!</Text>
<Text style={styles.textNum}>{this.props.id}</Text>
</Animated.View>
)
}
}
export default Draggable;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.