【问题标题】:How to enable dragging between two react-native-swiper components?如何启用两个 react-native-swiper 组件之间的拖动?
【发布时间】: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


    【解决方案1】:

    我设法通过设置滑动器可见的溢出来解决这个问题:

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

    但不幸的是,这只适用于 iOS。在Android上这似乎是一个相当大的问题: https://github.com/facebook/react-native/issues/6802

    非常感谢任何解决方法或更新以使其在 Android 上运行。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-11-15
      • 1970-01-01
      • 1970-01-01
      • 2019-02-21
      • 2017-03-21
      • 2020-12-27
      • 1970-01-01
      相关资源
      最近更新 更多