首页 > 解决方案 > React Native - Animated.View 内的可触摸不透明度是背景列表视图的触发事件

问题描述

我有一个带有滚动视图的列表,我正在尝试为其添加过滤器选项。单击过滤器图标时,position:absolute将在 Animated.View 内显示一个覆盖。我在覆盖视图中有按钮TouchableOpacity

过滤器.js

    export default class FilterFade extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
          visible: props.visible,
        };
    };

    componentWillMount() {
      this._visibility = new Animated.Value(this.props.visible ? 1 : 0);
    }

    componentWillReceiveProps(nextProps) {

        if (nextProps.visible) {
          this.setState({ visible: true });
        }
        Animated.timing(this._visibility, {
          toValue: nextProps.visible ? 1 : 0,
          duration: 300,
        }).start(() => {
          this.setState({ visible: nextProps.visible });
        });
    }

    render() {
      const { visible, style, children, ...rest } = this.props;

      const containerStyle = {
        opacity: this._visibility.interpolate({
          inputRange: [0, 1],
          outputRange: [0, 1],
        }),
        transform: [
          {
            scale: this._visibility.interpolate({
              inputRange: [0, 1],
              outputRange: [1.1, 1],
            }),
          },
        ],
      };

      const combinedStyle = [containerStyle, style];
      return (
        <Animated.View style={combinedStyle} {...rest}>
          {children}
        </Animated.View>
      );
    }
  }

查看.js

<FilterFade visible={this.state.isFilterVisible}>
          <View style={styles.filterView}>
              <TouchableOpacity onPress={() => this.getFilteedStories}>
                <Text style={styles.filterOption}> My Stories </Text>
              </TouchableOpacity>
                <TouchableOpacity onPress={() => this.getFilteedStories}>
              <Text style={styles.filterOption}> All Stories </Text>
              </TouchableOpacity>
          </View>
 </FilterFade>

风格

 filterView :{
      position: 'absolute',
      top: 0,
      right: 5,
      backgroundColor: #CCC,
      width: 150,
      paddingTop: 15,
      paddingBottom: 15,
      zIndex: 999,
    },
    filterOption: {
      color: "#FFF",
      fontSize: 15

    }

现在,当我在 Filter 中单击 TouchableOpacity Text 时,会在 FadeView 后面的 Listview 中触发单击事件。

有人可以告诉我如何在动画绝对视图中添加新闻事件。

提前致谢。

标签: react-native

解决方案


使用来自 'react-native-gesture-handler' 而不是来自 'react-native' 的 TouchableOpacity。

从“react-native-gesture-handler”导入 { TouchableOpacity };

关注这篇文章使用 React native 无法在动画视图中单击 TouchableOpacity


推荐阅读