react native實(shí)現(xiàn)model效果/底部彈出框/中間彈框/附代碼

近期項(xiàng)目中用到一些彈框界面限煞,經(jīng)過(guò)幾次優(yōu)化后覺(jué)得挺好用,所以分享給大家员凝。

效果

彈框.gif

思路

實(shí)現(xiàn)方法很簡(jiǎn)單署驻,就做幾個(gè)動(dòng)畫,沒(méi)什么可說(shuō)的健霹⊥希可以根據(jù)自己需求做一些調(diào)整。

導(dǎo)入組件

  1. 新建一個(gè)js文件糖埋,將代碼復(fù)制過(guò)去宣吱。我是放在coverLayer文件中。
  2. 導(dǎo)入該組件
import CoverLayer from '../../../widgets/coverLayer';
  1. 在需要彈框的界面外層添加該組件并添加ref引用(具體位置根據(jù)情況而定瞳别,防止遮擋)
render() {
    return(
        <View>

               ......

              {/* 在合適位置添加組件 */}
              <CoverLayer ref={ref => this.coverLayer = ref}/>
        </view>
    )
}

使用組件

該組件對(duì)外提供3個(gè)可自定義的屬性

coverLayerColor: PropTypes.string, //彈框背景顏色
coverLayerEvent: PropTypes.func, // 點(diǎn)擊背景后的回調(diào)
renderContent: PropTypes.func // 渲染彈框內(nèi)容的方法

提供2個(gè)控制顯示方法和一個(gè)控制隱藏方法

/*** 顯示彈框
   *    displayMode: 彈出方式(從底部/從中間彈出)
   */
show(displayMode);

/*** 顯示彈框
   * renderContent: 內(nèi)容渲染方法
   * coverLayerEvent: 點(diǎn)擊背景的回調(diào)方法
   * displayMode: 彈出方式(從底部/從中間彈出)
   */
showWithOptions(renderContent,coverLayerEvent,displayMode);

/*** 隱藏彈框 ***/
hide();
  

使用方法有兩種征候,建議使用第二種方法

方法一:在組件中添加屬性,然后在合適的地方調(diào)用組件的show和hide方法控制顯示和隱藏祟敛。在一個(gè)界面多個(gè)彈框的情況下需要在renderContent方法中判斷顯示哪一個(gè)彈框疤坝,復(fù)雜度增加,所以建議使用第二種方法馆铁。

<CoverLayer ref={ref => this.coverLayer = ref}
            renderContent={()=>{return <View></View>}}
            coverLayerEvent={()=>console.log("點(diǎn)擊了背景")}
            coverLayerColor="rgba(0,0,0,0.2)"
/>

// 顯示
showCoverLayer() {
    this.coverLayer.show();
}

// 隱藏
hideCoverLayer() {
    this.coverLayer.hide();
}

方法二:調(diào)用組件提供的showWithContent方法跑揉。調(diào)用該方法后會(huì)自動(dòng)顯示彈框,只需要在合適的位置調(diào)用hide隱藏方法

showPopupView() {
    // 根據(jù)傳入的方法渲染并彈出
    this.coverLayer.showWithContent(
                ()=> {
                    return (
                        <View style={{height:100,width:100,backgroundColor:"red"}}>
                        </View>
                    )
                },
            ()=>this.coverLayer.hide(),
            CoverLayer.popupMode.bottom
        )
    }

 /*** 組件提供了一個(gè)類似枚舉的彈出方式選項(xiàng)popupMode埠巨,
    * 可以直接使用導(dǎo)入的組件名.popupMode.center或組件名.popupMode.bottom控制彈出方式历谍。
    * 當(dāng)然直接傳指定字符串也可以
    */

// 隱藏
hideCoverLayer() {
    this.coverLayer.hide();
}

附上完整代碼

import React, { Component , PropTypes } from 'react';

import {
    TouchableOpacity,
    View,
    Animated,
    Dimensions
} from 'react-native';

const c_duration = 200;
const c_deviceHeight = Dimensions.get("window").height;
export default class CoverLayer extends Component {

    static propTypes = {
        coverLayerColor:PropTypes.string,
        coverLayerEvent:PropTypes.func,
        renderContent:PropTypes.func
    };

    static popupMode = {
        center:"center",
        bottom:"bottom"
    };

    // 構(gòu)造
    constructor(props) {
        super(props);
        // 初始狀態(tài)
        this.state = {
            isShow:false,
            opacityValue:new Animated.Value(0),
            scaleValue:new Animated.Value(1.1),
            bottom:new Animated.Value(-c_deviceHeight),
            renderContent:this.props.renderContent,
            coverLayerEvent:this.props.coverLayerEvent,
            displayMode:null
        };
        this.showAnimated = null;
        this.hideAnimated = null;
    }

    /**
     * 顯示彈框(該方法是為了簡(jiǎn)化一個(gè)界面有多個(gè)彈框的情況)
     * renderContent: func, 渲染彈框內(nèi)容的方法, 會(huì)覆蓋this.props.renderContent
     * coverLayerEvent: func, 點(diǎn)擊背景觸發(fā)的事件, 會(huì)覆蓋this.props.coverLayerEvent
     **/
    async showWithContent(renderContent,coverLayerEvent,displayMode) {

        if (this.state.isShow) {
            this.hide(async ()=>{
                await this.setState({
                    coverLayerEvent:coverLayerEvent,
                    renderContent:renderContent
                });

                this.show(displayMode);
            })
        } else {
            await this.setState({
                coverLayerEvent:coverLayerEvent,
                renderContent:renderContent
            });

            this.show(displayMode);
        }
    }

    // 顯示彈框
    show(displayMode) {
        this.setState({
            displayMode:displayMode,
            isShow:true
        });

        if (CoverLayer.popupMode.bottom == displayMode) {
            this.showAnimated = this.showFromBottom;
            this.hideAnimated = this.hideFromBottom;
        } else {
            this.showAnimated = this.showFromCenter;
            this.hideAnimated = this.hideFromCenter;
        }

        Animated.parallel([
            Animated.timing(this.state.opacityValue, {
                toValue: 1,
                duration: c_duration
            }),
            this.showAnimated()
        ]).start();
    }


    // 從中間彈出界面
    showFromCenter() {
        return (
            Animated.timing(this.state.scaleValue, {
                toValue: 1,
                duration: c_duration
            })
        )
    }


    // 從底部彈出界面
    showFromBottom() {
        return (
            Animated.timing(this.state.bottom, {
                toValue: 0,
                duration: c_duration
            })
        )
    }


    // 隱藏彈框
    hide(callback) {
        Animated.parallel([
            Animated.timing(this.state.opacityValue, {
                toValue: 0,
                duration: c_duration
            }),
            this.hideAnimated()
        ]).start(async ()=> {
            await this.setState({isShow: false});
            callback && callback();
        });
    }

    //從中間隱藏
    hideFromCenter() {
        return (
            Animated.timing(this.state.scaleValue, {
                toValue: 1.1,
                duration: c_duration
            })
        )
    }

    // 從底部隱藏
    hideFromBottom() {
        return (
            Animated.timing(this.state.bottom, {
                toValue: -c_deviceHeight,
                duration: c_duration
            })
        )
    }

    render() {
        return(
            this.state.isShow &&
            <Animated.View style={{width:DEVICE_WIDTH,justifyContent:CoverLayer.popupMode.bottom == this.state.displayMode  ? 'flex-end' : 'center',
                                   alignItems:'center',backgroundColor:this.props.coverLayerColor ? this.props.coverLayerColor : 'rgba(0,0,0,0.4)',
                                   position:'absolute',top:0,bottom:0,opacity: this.state.opacityValue}}>
                <TouchableOpacity style={{width:DEVICE_WIDTH,justifyContent:'center',alignItems:'center',position:'absolute',top:0,bottom:0}}
                                  activeOpacity={1}
                                  onPress={()=>{this.state.coverLayerEvent && this.state.coverLayerEvent()}}/>
                <Animated.View style={CoverLayer.popupMode.bottom == this.state.displayMode ? {bottom:this.state.bottom} : {transform: [{scale:this.state.scaleValue}]}}>
                    {this.state.renderContent && this.state.renderContent()}
                </Animated.View>
            </Animated.View>
        );
    }
}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市乖订,隨后出現(xiàn)的幾起案子扮饶,更是在濱河造成了極大的恐慌具练,老刑警劉巖乍构,帶你破解...
    沈念sama閱讀 221,888評(píng)論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異扛点,居然都是意外死亡哥遮,警方通過(guò)查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,677評(píng)論 3 399
  • 文/潘曉璐 我一進(jìn)店門陵究,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)眠饮,“玉大人,你說(shuō)我怎么就攤上這事铜邮∫钦伲” “怎么了寨蹋?”我有些...
    開(kāi)封第一講書人閱讀 168,386評(píng)論 0 360
  • 文/不壞的土叔 我叫張陵,是天一觀的道長(zhǎng)扔茅。 經(jīng)常有香客問(wèn)我已旧,道長(zhǎng),這世上最難降的妖魔是什么召娜? 我笑而不...
    開(kāi)封第一講書人閱讀 59,726評(píng)論 1 297
  • 正文 為了忘掉前任运褪,我火速辦了婚禮,結(jié)果婚禮上玖瘸,老公的妹妹穿的比我還像新娘秸讹。我一直安慰自己,他們只是感情好雅倒,可當(dāng)我...
    茶點(diǎn)故事閱讀 68,729評(píng)論 6 397
  • 文/花漫 我一把揭開(kāi)白布璃诀。 她就那樣靜靜地躺著,像睡著了一般蔑匣。 火紅的嫁衣襯著肌膚如雪文虏。 梳的紋絲不亂的頭發(fā)上,一...
    開(kāi)封第一講書人閱讀 52,337評(píng)論 1 310
  • 那天殖演,我揣著相機(jī)與錄音氧秘,去河邊找鬼。 笑死趴久,一個(gè)胖子當(dāng)著我的面吹牛丸相,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播彼棍,決...
    沈念sama閱讀 40,902評(píng)論 3 421
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼灭忠,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來(lái)了座硕?” 一聲冷哼從身側(cè)響起弛作,我...
    開(kāi)封第一講書人閱讀 39,807評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎华匾,沒(méi)想到半個(gè)月后映琳,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 46,349評(píng)論 1 318
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡蜘拉,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,439評(píng)論 3 340
  • 正文 我和宋清朗相戀三年萨西,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片旭旭。...
    茶點(diǎn)故事閱讀 40,567評(píng)論 1 352
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡谎脯,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出持寄,到底是詐尸還是另有隱情源梭,我是刑警寧澤娱俺,帶...
    沈念sama閱讀 36,242評(píng)論 5 350
  • 正文 年R本政府宣布,位于F島的核電站废麻,受9級(jí)特大地震影響矢否,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜脑溢,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,933評(píng)論 3 334
  • 文/蒙蒙 一僵朗、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧屑彻,春花似錦验庙、人聲如沸。這莊子的主人今日做“春日...
    開(kāi)封第一講書人閱讀 32,420評(píng)論 0 24
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)。三九已至搏恤,卻和暖如春违寿,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背熟空。 一陣腳步聲響...
    開(kāi)封第一講書人閱讀 33,531評(píng)論 1 272
  • 我被黑心中介騙來(lái)泰國(guó)打工藤巢, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人息罗。 一個(gè)月前我還...
    沈念sama閱讀 48,995評(píng)論 3 377
  • 正文 我出身青樓掂咒,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國(guó)和親迈喉。 傳聞我的和親對(duì)象是個(gè)殘疾皇子绍刮,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,585評(píng)論 2 359