React Native如何为视图打开->向下动画

时间:2018-08-23 15:45:09

标签: reactjs react-native react-native-animatable

请考虑以下组件,用户可以在其中从列表中选择一个选项:

import React, { Component } from "react";
import PropTypes from "prop-types";

import {
    View,
    Text,
    StyleSheet,
    Platform,
    FlatList,
    TouchableNativeFeedback,
    TouchableOpacity,
    PLatform
} from "react-native";

import Icon from "react-native-vector-icons/Ionicons";

import { colors, metrics } from "../../themes";

const Touchable =
    Platform.OS === "android" ? TouchableNativeFeedback : TouchableOpacity;

class MenuSelector extends Component<{
    onSelect: () => any,
    config: object,
    selected: string
}> {
    state = {
        listOpened: false
    };

    handlePress = id => {
        this.props.onPress(id);
    };

    handleSelect = id => {
        if (this.props.onSelect) this.props.onSelect(id);

        this.setState({
            listOpened: false
        });
    };

    handleMenu = () => {
        this.setState({
            listOpened: !this.state.listOpened
        });
    };

    render = () => {
        let title = "";

        if (this.props.config) {
            title = this.props.config[0].title;

            if (this.props.selected) {
                let found = this.props.config.find(item => {
                    return item.id === this.props.selected;
                });

                if (found) title = found.title;
            }
        }

        let top = (
            <View style={styles.header}>
                <Text style={styles.text}>{title}</Text>
                <Touchable>
                    <Text style={styles.text}>
                        <Icon
                            name={"ios-menu"}
                            size={20}
                            onPress={this.handleMenu}
                        />
                    </Text>
                </Touchable>
            </View>
        );

        let list = null;
        if (this.state.listOpened === true) {
            list = (
                <FlatList
                    data={this.props.config}
                    renderItem={({ item }) => (
                        <Touchable onPress={this.handleSelect}>
                            <Text style={[styles.text, styles.listItem]}>{item.title}</Text>
                        </Touchable>
                    )}
                />
            );
        }

        return (
            <View style={styles.container}>
                {top}
                {list}
            </View>
        );
    };
}

export default MenuSelector;

const styles = StyleSheet.create({
    container: {
        flex: -1,
        flexDirection: "column"
    },
    header: {
        flex: -1,
        flexDirection: "row",
        justifyContent: "space-between",
        padding: 10,
        backgroundColor: "blue"
    },
    text: {
        fontSize: 16,
        color: "white",
        textAlign: "center",
        fontWeight: "bold"
    },
    listItem: {
        padding: 10,
        backgroundColor: "blue"
    }
});

该组件在以下上下文中使用:

let config = [
   {
       title="Option 1",
       id="option1"
   },
   {
       title="Option 2",
       id="option2"
   },
   {
       title="Option 3",
       id="option3"
   },
   {
       title="Option 4",
       id="option4"
   },
];

return (
  <View style={styles.container}>
    <MenuSelector
      config={config.options}
      selected={this.state.mode}
      onPress={this.handleButtonListPress}
    />
    <FlatList
      data={this.props.data}
      keyExtractor={(item, index) => index.toString()}
      renderItem={({ item }) => (
        <Text style={styles.text}>{item.name}</Text>
      )}
    />
  </View>
);

实际上,<MenuSelector>组件在屏幕上“立即”出现。我需要在<MenuSelector>上添加滑动效果,当出现在屏幕上时“按下”数据FlatList ...

enter image description here

在关闭时,行为相同,但动画从上到下

如何将此类动画行为添加到我的MenuSelector组件中?

0 个答案:

没有答案