How to enable dragging between two react-native-swiper components?

戏子无情 提交于 2019-12-11 15:02:50

问题


I am trying to enable drag-and-drop between two react-native-swiper components. I used Animated.Text and PanResponder to drag the text in bottom one react-native-swiper and it is working corretly until when the dragging is moved over the above one react-native-swiper componenent. The dragged text is unvisible when dragging over the other react-native-swiper.

Could anyone explain what is happening and how to fix the behaviour?

Here is the simplefied code to reproduce the behaviour:

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',
  }
});

Here is also GitHub repo for the example code https://github.com/henkkasoft/drag-and-drop-example


回答1:


I managed to solve the issue by setting overflow visible for the swiper:

<Swiper style={{ overflow: 'visible' }}>

But unfortunately this works only on iOS. It seems to be quite major issue on Android: https://github.com/facebook/react-native/issues/6802

Any workaround or update to get this to work on Android would be really appreciated.



来源:https://stackoverflow.com/questions/45768925/how-to-enable-dragging-between-two-react-native-swiper-components

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!