RN-第三方之react-native-root-modal尿瞭、或者modal實現(xiàn)側(cè)欄效果

QQ20170913-133443.gif

本文內(nèi)容闽烙,采用modal方式的側(cè)欄效果

思路

1、點(diǎn)擊按鈕彈出modal(沒有背景色)声搁,在modal上面加了一個leftView黑竞,當(dāng)modal顯示的時候,leftView有一個從左到右的動畫效果
2疏旨、關(guān)于中間漸變的黑色圖層很魂,當(dāng)modal顯示的時候,在頁面上添加了一個opacityView(透明的)檐涝,動畫效果的透明度改變遏匆,和leftView的動畫一起進(jìn)行法挨。
3、點(diǎn)擊黑色的背景隱藏leftView,其實modal上的View有兩個子view幅聘,一個是leftView(寬度 = 屏幕的寬 - 100)凡纳,一個是右側(cè)的透明的TouchableOpacity(寬度 = 100),點(diǎn)擊TouchableOpacity隱藏modal


import React, {Component} from 'react';
import {
    AppRegistry,
    StyleSheet,
    Text,
    View,
    TouchableOpacity,
    Animated,
    Easing,
    Dimensions,
    LayoutAnimation,
} from 'react-native';

import Modal from 'react-native-root-modal';

import {Manager as ModalManager} from 'react-native-root-modal';

const screenWidth = Dimensions.get('window').width;
const screenHeight = Dimensions.get('window').height;
const leftWidth = screenWidth - 100;
const leftSpace = 100;

export default class rootModal extends Component {

    constructor(props) {
        super(props);

        this.marginLeftValue = new Animated.Value(0); // 左側(cè)向右動畫初始值帝蒿,默認(rèn)為0
        this.fadeInAnimated = new Animated.Value(0); // 漸隱動畫初始值荐糜,默認(rèn)為0,透明

        this.state = {
            isShowModal: false,
        };

        this.showModalByFadeIn = this.showModalByFadeIn.bind(this);
        this.hideModalByFadeIn = this.hideModalByFadeIn.bind(this);

    }

    /*顯示浮層*/
    showModalByFadeIn() {
        this.setState({
            isShowModal: true
        },()=>{
            this.marginLeftValue.setValue(0);
            // 組動畫葛超,組內(nèi)動畫同時執(zhí)行
            Animated.parallel([
                // 從左向右的動畫效果
                Animated.timing(
                    this.marginLeftValue,
                    {
                        toValue: 1,
                        duration: 500,
                        easing: Easing.linear
                    }
                ),
                // 透明度變化的動畫效果
                Animated.timing(
                    this.fadeInAnimated, {
                        toValue: 0.7, // 1為不透明
                        duration: 500,
                        easing: Easing.linear
                    }
                )]
            ).start()
        });

    }

    /*隱藏浮層*/
    hideModalByFadeIn() {
        Animated.parallel([
            Animated.timing(
                this.marginLeftValue,
                {
                    toValue: 0,
                    duration: 500,
                    easing: Easing.linear
                }
            ),
            Animated.timing(
                this.fadeInAnimated, {
                    toValue: 0, // 1為不透明
                    duration: 500,
                    easing: Easing.linear
                }
            )
        ]).start(()=> {
            this.setState({
                isShowModal: false
            })
        });

    }

    render() {

        const movingMargin = this.marginLeftValue.interpolate({
            inputRange: [0, 1],
            outputRange: [-leftWidth, 0]
        });

        return (
            <View style={styles.container}>

                <TouchableOpacity
                    style={{marginTop: 20, height: 44, marginHorizontal: 0, alignItems: 'center', backgroundColor: 'red'}}
                    onPress={()=>{
                        this.showModalByFadeIn();
                }}>
                    <Text style={{lineHeight: 44}}>
                        touch me show Modal
                    </Text>

                </TouchableOpacity>

                {
                    // 中間的黑色漸變View
                    (()=>{
                        if (this.state.isShowModal){
                            return (
                                <Animated.View style={[styles.modalStyle,
                                    {backgroundColor: 'black',
                                    width: screenWidth,
                                    height: screenHeight,
                                    position: 'absolute',
                                    opacity: this.fadeInAnimated}]}>
                                </Animated.View>
                            )
                        }
                    })()
                }

                /*
                react-native 自帶的modal組件
               <Modal style={styles.modalStyle}
                       visible={this.state.isShowModal}
                       transparent={true}
                       animationType='fade'
                >*/

                //react-native-root-modal 
                <Modal style={styles.modalStyle}
                       visible={this.state.isShowModal}
                >

                    <View style={{marginTop: 0,
                                marginLeft: 0,
                                width: screenWidth,
                                height: screenHeight,
                                flexDirection: 'row',
                        }}>
                        {/*動畫View*/}
                        <Animated.View style={{marginTop: 0,
                                    marginLeft: movingMargin,
                                    width: leftWidth,
                                    height: screenHeight,
                                    backgroundColor: 'white',
                            }}>

                                <TouchableOpacity style={styles.downViewStyle}
                                                  onPress={()=>{
                                                      this.hideModalByFadeIn();
                                }}>
                                    <Text style={{lineHeight: 50}}>
                                        touch me hide
                                    </Text>
                                </TouchableOpacity>
                        </Animated.View>

                        {/*右側(cè)點(diǎn)擊按鈕*/}
                        <TouchableOpacity style={styles.rightStyle}
                                          onPress={()=>{
                                              this.hideModalByFadeIn();
                                          }}
                                          activeOpacity={1}
                        />

                    </View>

                </Modal>


            </View>
        );
    }
}


const styles = StyleSheet.create({
    container: {
        flex: 1,
    },
    textStyle: {
        marginTop: 200,
        marginLeft: 100,
        textAlign: 'center',
        backgroundColor: 'red',
        height: 44,
        lineHeight: 44
    },
    modalStyle: {
        top: 0,
        right: 0,
        bottom: 0,
        left: 0,
    },
    downViewStyle: {
        height: 50,
        marginHorizontal: 0,
        backgroundColor: 'green',
        marginTop: screenHeight - 50,
        alignItems: 'center',
    },
    rightStyle: {
        marginTop: 0,
        marginRight: 0,
        width: leftSpace,
        height: screenHeight,
    }

});

AppRegistry.registerComponent('RNProjectTestApp', () => rootModal);

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末暴氏,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子绣张,更是在濱河造成了極大的恐慌答渔,老刑警劉巖,帶你破解...
    沈念sama閱讀 217,657評論 6 505
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件侥涵,死亡現(xiàn)場離奇詭異研儒,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)独令,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,889評論 3 394
  • 文/潘曉璐 我一進(jìn)店門端朵,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人燃箭,你說我怎么就攤上這事冲呢。” “怎么了招狸?”我有些...
    開封第一講書人閱讀 164,057評論 0 354
  • 文/不壞的土叔 我叫張陵敬拓,是天一觀的道長。 經(jīng)常有香客問我裙戏,道長乘凸,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,509評論 1 293
  • 正文 為了忘掉前任累榜,我火速辦了婚禮营勤,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘壹罚。我一直安慰自己葛作,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,562評論 6 392
  • 文/花漫 我一把揭開白布猖凛。 她就那樣靜靜地躺著赂蠢,像睡著了一般。 火紅的嫁衣襯著肌膚如雪辨泳。 梳的紋絲不亂的頭發(fā)上虱岂,一...
    開封第一講書人閱讀 51,443評論 1 302
  • 那天玖院,我揣著相機(jī)與錄音,去河邊找鬼第岖。 笑死司恳,一個胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的绍傲。 我是一名探鬼主播扔傅,決...
    沈念sama閱讀 40,251評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼烫饼!你這毒婦竟也來了猎塞?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,129評論 0 276
  • 序言:老撾萬榮一對情侶失蹤杠纵,失蹤者是張志新(化名)和其女友劉穎荠耽,沒想到半個月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體比藻,經(jīng)...
    沈念sama閱讀 45,561評論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡铝量,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,779評論 3 335
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了银亲。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片慢叨。...
    茶點(diǎn)故事閱讀 39,902評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖务蝠,靈堂內(nèi)的尸體忽然破棺而出拍谐,到底是詐尸還是另有隱情,我是刑警寧澤馏段,帶...
    沈念sama閱讀 35,621評論 5 345
  • 正文 年R本政府宣布轩拨,位于F島的核電站,受9級特大地震影響院喜,放射性物質(zhì)發(fā)生泄漏亡蓉。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,220評論 3 328
  • 文/蒙蒙 一喷舀、第九天 我趴在偏房一處隱蔽的房頂上張望砍濒。 院中可真熱鬧,春花似錦元咙、人聲如沸梯影。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,838評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至简识,卻和暖如春赶掖,著一層夾襖步出監(jiān)牢的瞬間感猛,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 32,971評論 1 269
  • 我被黑心中介騙來泰國打工奢赂, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留陪白,地道東北人。 一個月前我還...
    沈念sama閱讀 48,025評論 2 370
  • 正文 我出身青樓膳灶,卻偏偏與公主長得像咱士,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子轧钓,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,843評論 2 354

推薦閱讀更多精彩內(nèi)容

  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 172,116評論 25 707
  • WebSocket-Swift Starscream的使用 WebSocket 是 HTML5 一種新的協(xié)議序厉。它實...
    香橙柚子閱讀 23,860評論 8 183
  • 說到石榴石,芙迷們必定不生疏毕箍,石榴石宗族是寶石界大名鼎鼎的大宗族弛房,最常見的即是赤色的石榴石,我周圍的女性朋友簡直是...
    珠寶大課堂閱讀 718評論 0 0
  • 孩子而柑,你爸爸是個無賴文捶。 他抽煙喝酒賭博亂搞女人。 他打我打你亂摔東西媒咳。 他騙我騙你騙朋友粹排。 他欠錢不還亂偷東西。 ...
    鹿繽紛閱讀 266評論 0 1
  • 一涩澡、 “周皮皮恨搓,你又欺負(fù)我慕慕” 打開微信,簡書青春群里又是一副熱鬧喧騰的景象筏养。 加入青春群斧抱,源于一次機(jī)緣巧合,本...
    木月十閱讀 720評論 43 18