react-native 封装选择弹出框示例(试用ios&android)
发布时间 - 2026-01-11 02:16:43 点击率:次在开发 App 的时候,经常会使用到对话框(又叫消息框、提示框、告警框)。 在web开发中经常会用得到。今天就来介绍了一下react-native 封装弹出框

之前看到react-native-image-picker中自带了一个选择器,可以选择拍照还是图库,但我们的项目中有多处用到这个选择弹出框,所以就自己写了一下,最最重要的是ios和Android通用。先上动态效果图~
一、封装要点
1.使用动画实现弹框布局及显示隐藏效果
2.通过一个boolean值控制组件的显示隐藏
3.弹框选项数组通过调用的js传到弹框组件
4.组件选项的字体颜色通过调用js传到组件,实现可拓展;
5.选择选项回调方法
二、代码实现
新建alertSelected.js
/**
* Created by sybil052 on 2017/6/19.
*/
import React, {Component} from 'react';
import {
StyleSheet,
View,
Image,
Text,
TouchableHighlight,
Animated,
Easing,
Dimensions,
Platform,
TouchableOpacity
} from 'react-native';
const {width, height} = Dimensions.get('window');
const [aWidth] = [width-20];
const [left, top] = [0, 0];
const [middleLeft] = [(width - aWidth) / 2];
export default class AlertSelected extends Component {
constructor(props) {
super(props);
this.state = {
offset: new Animated.Value(0),
opacity: new Animated.Value(0),
title: "",
choose0: "",
choose1: "",
hide: true,
tipTextColor: '#333333',
aHeight: 236,
};
this.entityList = [];//数据源
this.callback = function () {
};//回调方法
}
render() {
if (this.state.hide) {
return (<View />)
} else {
return (
<View style={styles.container}>
<Animated.View style={styles.mask}>
</Animated.View>
<Animated.View style={[{
width: aWidth,
height: this.state.aHeight,
left: middleLeft,
...Platform.select({
ios:{
bottom: - 20,
},
}),
alignItems: "center",
justifyContent: "space-between",
}, {
transform: [{
translateY: this.state.offset.interpolate({
inputRange: [0, 1],
outputRange: [height, (height - this.state.aHeight - 34)]
}),
}]
}]}>
<View style={styles.content}>
<View style={styles.tipTitleView}>
<Text style={styles.tipTitleText}>{this.state.title}</Text>
</View>
{
this.entityList.map((item, i) => this.renderItem(item, i))
}
</View>
<TouchableHighlight
style={styles.button}
underlayColor={'#f0f0f0'}
onPress={this.cancel.bind(this)}
>
<Text style={styles.buttonText}>取消</Text>
</TouchableHighlight>
</Animated.View>
</View>
);
}
}
renderItem(item, i) {
return (
<View style={styles.tipContentView}>
<View style={{height: 0.5, backgroundColor: '#a9a9a9', width: aWidth}}/>
<TouchableOpacity
key={i}
onPress={this.choose.bind(this, i)}
>
<View style={styles.item}>
<Text style={{
color: this.state.tipTextColor,
fontSize: 17,
textAlign: "center",
}}>{item}</Text>
</View>
</TouchableOpacity>
</View>
);
}
componentDidMount() {
}
componentWillUnmount() {
// 如果存在this.timer,则使用clearTimeout清空。
// 如果你使用多个timer,那么用多个变量,或者用个数组来保存引用,然后逐个clear
this.timer && clearTimeout(this.timer);
this.chooseTimer && clearTimeout(this.chooseTimer);
}
//显示动画
in() {
Animated.parallel([
Animated.timing(
this.state.opacity,
{
easing: Easing.linear,//一个用于定义曲线的渐变函数
duration: 200,//动画持续的时间(单位是毫秒),默认为200。
toValue: 0.8,//动画的最终值
}
),
Animated.timing(
this.state.offset,
{
easing: Easing.linear,
duration: 200,
toValue: 1,
}
)
]).start();
}
//隐藏动画
out() {
Animated.parallel([
Animated.timing(
this.state.opacity,
{
easing: Easing.linear,
duration: 200,
toValue: 0,
}
),
Animated.timing(
this.state.offset,
{
easing: Easing.linear,
duration: 200,
toValue: 0,
}
)
]).start((finished) => this.setState({hide: true}));
}
//取消
cancel(event) {
if (!this.state.hide) {
this.out();
}
}
//选择
choose(i) {
if (!this.state.hide) {
this.out();
this.chooseTimer = setTimeout(()=>{
this.callback(i);
}, 200);
}
}
/**
* 弹出控件,最多支持3个选项(包括取消)
* titile: 标题
* entityList:选择项数据 数组
* tipTextColor: 字体颜色
* callback:回调方法
*/
show(title: string, entityList: Array, tipTextColor: string, callback: Object) {
this.entityList = entityList;
this.callback = callback;
if (this.state.hide) {
if (entityList && entityList.length > 0) {
let len = entityList.length;
if (len === 1) {
this.setState({title: title, choose0: entityList[0], hide: false, tipTextColor: tipTextColor, aHeight: 180}, this.in);
} else if (len === 2) {
this.setState({title: title, choose0: entityList[0], choose1: entityList[1], hide: false, tipTextColor: tipTextColor, aHeight: 236}, this.in);
}
}
}
}
}
const styles = StyleSheet.create({
container: {
position: "absolute",
width: width,
height: height,
left: left,
top: top,
},
mask: {
justifyContent: "center",
backgroundColor: "#000000",
opacity: 0.3,
position: "absolute",
width: width,
height: height,
left: left,
top: top,
},
// 提示标题
tipTitleView: {
height: 56,
flexDirection: 'row',
alignItems: 'center',
justifyContent: 'center',
backgroundColor: '#fff',
marginLeft: 10,
marginRight: 10
},
// 提示文字
tipTitleText: {
color: "#999999",
fontSize: 14,
},
// 分割线
tipContentView: {
width: aWidth,
height: 56,
backgroundColor:'#fff',
borderBottomLeftRadius: 5,
borderBottomRightRadius: 5,
},
item:{
width: aWidth,
height: 56,
backgroundColor:'#fff',
justifyContent: 'center',
borderRadius: 5,
},
button: {
height: 57,
backgroundColor: '#fff',
alignSelf: 'stretch',
justifyContent: 'center',
borderRadius: 5,
},
// 取消按钮
buttonText: {
fontSize: 17,
color: "#0084ff",
textAlign: "center",
},
content: {
backgroundColor: '#fff',
borderRadius: 5,
}
});
三、使用方法
新建demo.js
const selectedArr = ["拍照", "图库"];
class Demo extends Component {
constructor(props) {
super(props);
this.showAlertSelected = this.showAlertSelected.bind(this);
this.callbackSelected = this.callbackSelected.bind(this);
}
showAlertSelected(){
this.dialog.show("请选择照片", selectedArr, '#333333', this.callbackSelected);
}
// 回调
callbackSelected(i){
switch (i){
case 0: // 拍照
this.takePhoto();
break;
case 1: // 图库
this.pickMultiple();
break;
}
}
render() {
return (
<View style={stylesCommon.container}>
<TouchableOpacity onPress={() => {this.showAlertSelected();}}>
<View style={styles.imageBorder}>
<Text style={styles.photoText}></Text>
</View>
</TouchableOpacity>
<DialogSelected ref={(dialog)=>{
this.dialog = dialog;
}} />
</View>
);
}
}
再来一张其他界面调用该组件的效果图~
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
# react
# native
# 弹出框
# reactnative
# 弹出菜单
# React实现动态调用的弹框组件
# 使用react+redux实现弹出框案例
# React封装全屏弹框的方法
# react封装Dialog弹框的方法
# React封装弹出框组件的方法
# react封装全局弹框的方法
# react使用antd表单赋值
# 用于修改弹框的操作
# 解决react-native软键盘弹出挡住输入框的问题
# React Native实现进度条弹框的示例代码
# React实现pc端的弹出框效果
# 回调
# 弹出
# 多个
# 如果你
# 最多
# 中有
# 再来
# 写了
# 请选择
# 会使
# 带了
# 就来
# 可以选择
# 最重要的是
# 对话框
# 多处
# 又叫
# 用得
# 大家多多
# 清空
相关栏目:
【
网站优化151355 】
【
网络推广146373 】
【
网络技术251813 】
【
AI营销90571 】
相关推荐:
如何在IIS中新建站点并配置端口与IP地址?
Java Adapter 适配器模式(类适配器,对象适配器)优缺点对比
Laravel怎么防止CSRF攻击_Laravel CSRF保护中间件原理与实践
Laravel策略(Policy)如何控制权限_Laravel Gates与Policies实现用户授权
BootStrap整体框架之基础布局组件
canvas 画布在主流浏览器中的尺寸限制详细介绍
Win11怎么设置虚拟桌面 Win11新建多桌面切换操作【技巧】
Android仿QQ列表左滑删除操作
独立制作一个网站多少钱,建立网站需要花多少钱?
Laravel怎么集成Log日志记录_Laravel单文件与每日日志配置及自定义通道【详解】
Laravel如何使用Spatie Media Library_Laravel图片上传管理与缩略图生成【步骤】
JS中对数组元素进行增删改移的方法总结
Win11怎么恢复误删照片_Win11数据恢复工具使用【推荐】
晋江文学城电脑版官网 晋江文学城网页版直接进入
C++用Dijkstra(迪杰斯特拉)算法求最短路径
javascript中对象的定义、使用以及对象和原型链操作小结
瓜子二手车官方网站在线入口 瓜子二手车网页版官网通道入口
如何生成腾讯云建站专用兑换码?
如何基于云服务器快速搭建网站及云盘系统?
网站建设要注意的标准 促进网站用户好感度!
Laravel中的Facade(门面)到底是什么原理
标准网站视频模板制作软件,现在有哪个网站的视频编辑素材最齐全的,背景音乐、音效等?
Laravel如何为API编写文档_Laravel API文档生成与维护方法
如何快速选择适合个人网站的云服务器配置?
如何确保FTP站点访问权限与数据传输安全?
百度浏览器ai对话怎么关 百度浏览器ai聊天窗口隐藏
大学网站设计制作软件有哪些,如何将网站制作成自己app?
详解阿里云nginx服务器多站点的配置
如何快速打造个性化非模板自助建站?
Android okhttputils现在进度显示实例代码
车管所网站制作流程,交警当场开简易程序处罚决定书,在交警网站查询不到怎么办?
php485函数参数是什么意思_php485各参数详细说明【介绍】
如何快速查询网址的建站时间与历史轨迹?
香港服务器网站测试全流程:性能评估、SEO加载与移动适配优化
Swift中循环语句中的转移语句 break 和 continue
微信小程序 HTTPS报错整理常见问题及解决方案
魔毅自助建站系统:模板定制与SEO优化一键生成指南
详解Android图表 MPAndroidChart折线图
如何用JavaScript实现文本编辑器_光标和选区怎么处理
Python正则表达式进阶教程_复杂匹配与分组替换解析
魔方云NAT建站如何实现端口转发?
西安市网站制作公司,哪个相亲网站比较好?西安比较好的相亲网站?
zabbix利用python脚本发送报警邮件的方法
Laravel怎么清理缓存_Laravel optimize clear命令详解
如何将凡科建站内容保存为本地文件?
Laravel表单请求验证类怎么用_Laravel Form Request分离验证逻辑教程
javascript基本数据类型及类型检测常用方法小结
网站制作报价单模板图片,小松挖机官方网站报价?
创业网站制作流程,创业网站可靠吗?
Laravel怎么连接多个数据库_Laravel多数据库连接配置

