{"id":528,"date":"2023-02-25T15:49:38","date_gmt":"2023-02-25T07:49:38","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=528"},"modified":"2023-04-29T20:42:01","modified_gmt":"2023-04-29T12:42:01","slug":"react-native-use-modal-components-to-achieve-mask-layer-effects","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/02\/25\/react-native-use-modal-components-to-achieve-mask-layer-effects\/","title":{"rendered":"React Native\u4f7f\u7528Modal\u7ec4\u4ef6\u5b9e\u73b0\u906e\u7f69\u5c42\u6548\u679c"},"content":{"rendered":"<p>Modal \u7ec4\u4ef6\u53ef\u4ee5\u7528\u6765\u8986\u76d6\u5305\u542bReact Native\u6839\u89c6\u56fe\u7684\u539f\u751f\u89c6\u56fe\uff08\u5982UIViewController\uff0cActivity\uff09\u3002\u5728\u5d4c\u5165React Native\u7684\u6df7\u5408\u5e94\u7528\u4e2d\u53ef\u4ee5\u4f7f\u7528Modal\u3002Modal\u53ef\u4ee5\u4f7f\u4f60\u5e94\u7528\u4e2dRN\u7f16\u5199\u7684\u90a3\u90e8\u5206\u5185\u5bb9\u8986\u76d6\u5728\u539f\u751f\u89c6\u56fe\u4e0a\u663e\u793a\u3002<\/p>\n<p>\u539f\u672c\u9879\u76ee\uff1a<a target=\"_blank\" rel=\"noopener\" href=\"https:\/\/github.com\/brentvatne\/react-native-modal\">https:\/\/github.com\/brentvatne\/react-native-modal<\/a><\/p>\n<p><!-- more --><\/p>\n<p>Modal \u7b2c\u4e09\u65b9\u7684\u7ec4\u4ef6\u73b0\u5df2\u88ab\u5b98\u65b9\u5438\u7eb3\uff0c\u65e0\u9700\u5b89\u88c5\u3002<\/p>\n<p>\u5c5e\u6027\u5982\u4e0b\uff1a<\/p>\n<ul>\n<li><code>animationType<\/code>\uff1a\u52a8\u753b\u7c7b\u578b enum(&#8216;none&#8217;, &#8216;slide&#8217;, &#8216;fade&#8217;)<\/li>\n<li><code>onRequestClose<\/code>\uff1a\u5728Android\u5e73\u53f0\u662f\u5fc5\u987b\u7684\uff0c\u5426\u5219\u62a5\u8b66\uff0c\u6bd4\u5982\u6309\u8fd4\u56de\u952e\u65f6\u8c03\u7528<\/li>\n<li><code>onShow<\/code>\uff1a\u663e\u793a\u65f6\u89e6\u53d1<\/li>\n<li><code>transparent<\/code>\uff1a\u662f\u5426\u900f\u660e<\/li>\n<li><code>visible<\/code>\uff1a\u662f\u5426\u663e\u793a<\/li>\n<\/ul>\n<p>\u5b9e\u4f8b\uff1a\u8ba2\u8d2d\u706b\u8f66\u7968\u7684\u7968\u52a1\u4fe1\u606f<\/p>\n<pre><code class=\"language-javascript\">\/**\n * Sample React Native App\n * https:\/\/github.com\/facebook\/react-native\n * @flow\n *\/\n\nimport React, { Component } from &#039;react&#039;;\nimport {\n    AppRegistry,\n    StyleSheet,\n    Text,\n    Modal,\n    PixelRatio,\n    View\n} from &#039;react-native&#039;;\n\nclass ModalDemo extends Component {\n    constructor(props) {\n        super(props);\n        this.state = {\n            animationType: &#039;none&#039;, \/\/none slide fade\n            modalVisible: false, \/\/\u6a21\u6001\u573a\u666f\u662f\u5426\u53ef\u89c1\n            transparent: true, \/\/\u662f\u5426\u900f\u660e\u663e\u793a\n        };\n    }\n\n    render() {\n        let modalBackgroundStyle = {\n            backgroundColor: this.state.transparent ? &#039;rgba(0, 0, 0, 0.5)&#039; : &#039;red&#039;,\n        };\n        let innerContainerTransparentStyle = this.state.transparent ? { backgroundColor: &#039;#fff&#039;, padding: 20 } : null;\n\n        return (\n            &lt;View style={{ alignItems: &#039;center&#039;, flex: 1 }}&gt;\n                &lt;Modal\n                    animationType={this.state.animationType}\n                    transparent={this.state.transparent}\n                    visible={this.state.modalVisible}\n                    onRequestClose={() =&gt; { this._setModalVisible(false) } }\n                    onShow={this.startShow}\n                    &gt;\n                    &lt;View style={[styles.container, modalBackgroundStyle]}&gt;\n                        &lt;View style={[styles.innerContainer, innerContainerTransparentStyle]}&gt;\n                            &lt;Text style={styles.date}&gt;2016-08-11&lt;\/Text&gt;\n                            &lt;View style={styles.row}&gt;\n                                &lt;View&gt;\n                                    &lt;Text style={styles.station}&gt;\u957f\u6c99\u7ad9&lt;\/Text&gt;\n                                    &lt;Text style={styles.mp10}&gt;8: 00\u51fa\u53d1&lt;\/Text&gt;\n                                &lt;\/View&gt;\n                                &lt;View&gt;\n                                    &lt;View style={styles.at}&gt;&lt;\/View&gt;\n                                    &lt;Text style={[styles.mp10, { textAlign: &#039;center&#039; }]}&gt;G888&lt;\/Text&gt;\n                                &lt;\/View&gt;\n                                &lt;View&gt;\n                                    &lt;Text style={[styles.station, { textAlign: &#039;right&#039; }]}&gt;\u5317\u4eac\u7ad9&lt;\/Text&gt;\n                                    &lt;Text style={[styles.mp10, { textAlign: &#039;right&#039; }]}&gt;18: 00\u62b5\u8fbe&lt;\/Text&gt;\n                                &lt;\/View&gt;\n                            &lt;\/View&gt;\n                            &lt;View style={styles.mp10}&gt;\n                                &lt;Text&gt;\u7968\u4ef7\uff1a\uffe5600.00\u5143&lt;\/Text&gt;\n                                &lt;Text&gt;\u4e58\u8f66\u4eba\uff1aJoe.Ye&lt;\/Text&gt;\n                                &lt;Text&gt;\u4e0a\u6d77 \u8679\u6865\u7ad9 \u7f51\u552e&lt;\/Text&gt;\n                            &lt;\/View&gt;\n                            &lt;View style={{ alignItems: &#039;center&#039;, flexDirection: &#039;row&#039;, marginTop: 20 }}&gt;\n                                &lt;View style={[styles.mp10, styles.btn, { alignItems: &#039;center&#039; }, {marginRight: 10}]}&gt;\n                                    &lt;Text style={styles.btn_text}&gt;\u7acb\u5373\u652f\u4ed8&lt;\/Text&gt;\n                                &lt;\/View&gt;\n                                &lt;View style={[styles.mp10, styles.btn, { alignItems: &#039;center&#039; }, {marginLeft: 10}]}&gt;\n                                    &lt;Text style={styles.btn_text} onPress={this._setModalVisible.bind(this, false)}&gt;\u8fd4\u56de&lt;\/Text&gt;\n                                &lt;\/View&gt;\n                            &lt;\/View&gt;\n                        &lt;\/View&gt;\n                    &lt;\/View&gt;\n                &lt;\/Modal&gt;\n\n                &lt;Text style={styles.order} onPress={this._setModalVisible.bind(this, true)}&gt;\u9884\u5b9a\u706b\u8f66\u7968&lt;\/Text&gt;\n                &lt;View style={styles.flex} \/&gt;\n                &lt;Text style={styles.author}&gt;Powered by RNAPP.CC&lt;\/Text&gt;\n            &lt;\/View&gt;\n        );\n    }\n\n    _setModalVisible = (visible) =&gt; {\n        this.setState({ modalVisible: visible });\n    }\n\n    startShow=()=&gt;{\n        \/\/alert(&#039;\u5f00\u59cb\u663e\u793a&#039;);\n    }\n}\n\nconst styles = StyleSheet.create({\n    container: {\n        flex: 1,\n        justifyContent: &#039;center&#039;,\n        padding: 40,\n    },\n    innerContainer: {\n        borderRadius: 10,\n        alignItems: &#039;center&#039;,\n    },\n    row: {\n        alignItems: &#039;center&#039;,\n        flex: 1,\n        flexDirection: &#039;row&#039;,\n        marginBottom: 20,\n    },\n    rowTitle: {\n        flex: 1,\n        fontWeight: &#039;bold&#039;,\n    },\n    button: {\n        borderRadius: 5,\n        flex: 1,\n        height: 44,\n        alignSelf: &#039;stretch&#039;,\n        justifyContent: &#039;center&#039;,\n        overflow: &#039;hidden&#039;,\n    },\n    buttonText: {\n        fontSize: 18,\n        margin: 5,\n        textAlign: &#039;center&#039;,\n    },\n    page: {\n        flex: 1,\n        position: &#039;absolute&#039;,\n        bottom: 0,\n        left: 0,\n        right: 0,\n        top: 0,\n    },\n    zhifu: {\n        height: 150,\n    },\n    flex: {\n        flex: 1,\n    },\n    at: {\n        borderWidth: 1 \/ PixelRatio.get(),\n        width: 80,\n        marginLeft:10,\n        marginRight:10,\n        borderColor: &#039;#18B7FF&#039;,\n        height: 1,\n        marginTop: 10\n    },\n    date: {\n        textAlign: &#039;center&#039;,\n        marginBottom: 5\n    },\n    station: {\n        fontSize: 20\n    },\n    mp10: {\n        marginTop: 5,\n    },\n    btn: {\n        width: 80,\n        height: 30,\n        borderRadius: 3,\n        backgroundColor: &#039;#FFBA27&#039;,\n        padding: 5,\n    },\n    btn_text: {\n        lineHeight: 18,\n        textAlign: &#039;center&#039;,\n        color: &#039;#fff&#039;,\n    },\n    order: {\n        fontSize: 25,\n        color: &#039;white&#039;,\n        backgroundColor: &#039;#3BC1FF&#039;,\n        paddingLeft: 20,\n        paddingRight: 20,\n        marginTop: 50,\n    },\n    author: {\n        justifyContent: &#039;flex-end&#039;,\n        alignItems: &#039;center&#039;,\n        marginBottom: 10,\n        textAlign: &#039;center&#039;,\n        fontSize: 16,\n        color: &#039;#3BC1FF&#039;,\n    },\n});\n\nAppRegistry.registerComponent(&#039;RNAPP&#039;, () =&gt; ModalDemo);<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Modal \u7ec4\u4ef6\u53ef\u4ee5\u7528\u6765\u8986\u76d6\u5305\u542bReact Native\u6839\u89c6\u56fe\u7684\u539f\u751f\u89c6\u56fe\uff08\u5982UIViewController\uff0c [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[163],"tags":[],"class_list":["post-528","post","type-post","status-publish","format-standard","hentry","category-react-native"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/528","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/comments?post=528"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/528\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=528"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=528"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=528"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}