最近一直在研究react-native技術,發現學習起來不是那麼的難,不過也採坑了很多。以前有基於h5和小程序技術開發過自定義彈窗的經驗,就想着用react-native技術實現msg信息框|alert提示框|confirm確認框|toast弱提示/loading|仿ios、android彈窗。html
◆ RN彈窗插件引入及調用方式
react
import RNPop from '../utils/rnPop/rnPop.js'
<View style={styles.container}>
{/* 引入彈窗模板 */}
<RNPop ref="rnPop" />
...
</View
顯示:this.refs.rnPop.show({...options});
隱藏:this.refs.rnPop.hide();
複製代碼
◆ Toast弱提示可自定義loading | success | info | error四種圖標
android
//Toast演示
handlePress15 = ()=> {
let rnPop = this.refs.rnPop
rnPop.show({
skin: 'toast',
content: '操做成功',
icon: 'success', //success | info | error | loading
shade: false,
time: 3
});
}
複製代碼
/**
* --------- react-native彈窗演示(普通型彈窗) ---------
*/
//msg提示
handlePress01 = ()=> {
let rnPop = this.refs.rnPop
rnPop.show({
anim: 'fadeIn',
content: 'msg消息提示框(5s後窗口關閉)',
shade: true,
shadeClose: false,
time: 5,
xtime: true,
});
}
//msg提示(黑色背景)
handlePress02 = ()=> {
let rnPop = this.refs.rnPop
rnPop.show({
content: '自定義彈窗背景',
shade: false,
style: {backgroundColor: 'rgba(17,17,17,.7)', borderRadius: 6},
contentStyle: {color: '#fff', padding: 10},
time: 2
});
}
複製代碼
//ios底部對話框
handlePress16 = ()=> {
let rnPop = this.refs.rnPop
rnPop.show({
skin: 'footer',
content: 'Apple ID \n 282310962@qq.com',
shadeClose: false,
btns: [
{
text: '註銷帳號',
style: {color: '#30a4fc'},
onPress() {
console.log('您點擊了恢復!');
}
},
{
text: '刪除',
style: {color: '#e63d23'},
onPress() {
console.log('您點擊了刪除!');
//刪除回調提示
rnPop.show({
anim: 'fadeIn',
content: '您點擊了刪除功能',
shade: true,
time: 3
});
}
},
{
text: '取消',
style: {color: '#999'},
onPress() {
console.log('您點擊了取消!');
rnPop.close();
}
}
]
});
}
複製代碼
// android 樣式一
handlePress20 = ()=>{
let rnPop = this.refs.rnPop
rnPop.show({
skin: 'android',
title: '發現新版本',
content: '程序員GG緊急修復了一個閃退bug,給您帶來的不便敬請諒解。\n\n[近期更新]\n 一、新增資訊&話題入口 \n 二、新增詳情頁面長按分享功能',
shadeClose: false,
btns: [
{
text: '知道了',
onPress() {
rnPop.close();
console.log("知道了");
}
},
{
text: '更新',
style: {color: '#4eca33'},
onPress() {
console.log('您點擊了更新!');
}
}
]
});
}
複製代碼
◆ react-native自定義彈窗參數配置
ios
/**
* @Title react-native彈窗插件 rnPop-v1.0 beta (UTF-8)
* @Author andy
* @Create 2019/07/30 10:00:50 GMT+0800 (中國標準時間)
* @AboutMe Q:282310962 wx:xy190310
*/
'use strict'
import React, {Component} from 'react'
import {
StyleSheet, Dimensions, PixelRatio, TouchableHighlight, Modal, View, Text, Image, ActivityIndicator, Alert
} from 'react-native'
const pixel = PixelRatio.get()
const {width, height} = Dimensions.get('window')
export default class RNPop extends Component{
/**************************
* 彈窗配置參數
*/
static defaultProps = {
isVisible: false, //彈窗顯示
id: 'rnPop', //彈窗id標識
title: '', //標題
content: '', //內容
style: null, //自定義彈窗樣式 {object}
contentStyle: null, //內容樣式
skin: '', //自定義彈窗風格
icon: '', //自定義彈窗圖標
shade: true, //遮罩層
shadeClose: true, //點擊遮罩層關閉
opacity: '', //遮罩層透明度
xclose: false, //自定義關閉按鈕
time: 0, //彈窗自動關閉秒數
xtime: false, //顯示關閉秒數
anim: 'scaleIn', //彈窗動畫
follow: null, //跟隨定位(適用於在長按位置定位彈窗)
position: '', //彈窗位置
zIndex: 9999, //層疊等級
btns: null, //彈窗按鈕(不設置則不顯示按鈕)[{...options}, {...options}]
}
...
}複製代碼
◆ react-native基於Modal自定義彈窗模板
程序員
<Modal transparent={true} visible={opt.isVisible} onRequestClose={this.close}>
<View style={styles.rnpop__ui_panel}>
{/* 遮罩 */}
{ opt.shade && <View style={styles.rnpop__ui_mask} onTouchEnd={opt.shadeClose ? this.close : null} /> }
{/* 窗體 */}
<View style={styles.rnpop__ui_main}>
<View style={styles.rnpop__ui_child}>
{/* 標題 */}
{ opt.title ? <View style={[styles.rnpop__ui_tit]}><Text style={[styles.rnpop__ui_titxt]}>{opt.title}</Text></View> : null }
{/* 內容 */}
{ opt.content ? <View style={[styles.rnpop__ui_cnt]}>
...
<Text style={[styles.rnpop__ui_cntxt, opt.contentStyle]}>{opt.content}</Text>
</View> : null }
{/* 按鈕 */}
<View style={[styles.rnpop__ui_btnwrap]}>
...
</View>
</View>
</View>
</View>
</Modal>
複製代碼
◆ 附上h5和微信小程序彈窗小程序
h5移動端彈窗:www.cnblogs.com/xiaoyan2017…微信小程序
h5網頁版彈窗:www.cnblogs.com/xiaoyan2017…react-native
小程序彈窗:www.cnblogs.com/xiaoyan2017…bash