【发布时间】:2017-08-19 07:28:22
【问题描述】:
我正在尝试在两个 react-native-swiper 组件之间启用拖放功能。 我使用 Animated.Text 和 PanResponder 将文本拖动到底部的一个 react-native-swiper 中,并且它正常工作,直到拖动移动到上面的一个 react-native-swiper 组件上。拖动到另一个 react-native-swiper 上时,拖动的文本是不可见的。
谁能解释发生了什么以及如何纠正这种行为?
这里是重现行为的简化代码:
import React from 'react';
import {
StyleSheet,
Text,
View,
PanResponder,
Animated,
Dimensions } from 'react-native';
import Swiper from 'react-native-swiper';
const SCREEN_WIDTH = Dimensions.get('window').width;
export default class App extends React.Component {
constructor(props) {
super(props);
const position = new Animated.ValueXY();
const panResponder = PanResponder.create({
onStartShouldSetPanResponder: () => true,
onPanResponderMove: (event, gesture) => {
position.setValue({ x: gesture.dx, y: gesture.dy });
},
onPanResponderRelease: () => {}
});
this.state = { panResponder, position };
}
renderAnimatedText(text) {
return (
<Animated.Text
style={[this.state.position.getLayout(), styles.text]}
{...this.state.panResponder.panHandlers}
>
{text}
</Animated.Text>
);
}
render() {
return (
<View style={styles.container}>
<View style={styles.swiperContainer}>
<Swiper style={styles.wrapper}>
<View style={styles.slide3}>
<Text style={styles.text}>Hello Stephen</Text>
</View>
<View style={styles.slide2}>
<Text style={styles.text}>Hello Helpers</Text>
</View>
<View style={styles.slide1}>
<Text style={styles.text}>Hello World</Text>
</View>
</Swiper>
</View>
<View style={styles.swiperContainer}>
<Swiper style={styles.wrapper}>
<View style={styles.slide1}>
{this.renderAnimatedText('You are The Best')}
</View>
<View style={styles.slide2}>
<Text style={styles.text}>and Greatest</Text>
</View>
<View style={styles.slide3}>
<Text style={styles.text}>Thank you</Text>
</View>
</Swiper>
</View>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#fff',
alignItems: 'center',
justifyContent: 'center',
},
swiperContainer: {
height: 250,
width: SCREEN_WIDTH
},
wrapper: {
},
slide1: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#9DD6EB',
},
slide2: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#97CAE5',
},
slide3: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#92BBD9',
},
text: {
color: '#ff0',
fontSize: 30,
fontWeight: 'bold',
}
});
这里也是示例代码的 GitHub 存储库 https://github.com/henkkasoft/drag-and-drop-example
【问题讨论】:
标签: react-native drag-and-drop swiper