React-Native自定义PopupWindow实现
转载请注明出处:王亟亟的大牛之路自从泰国回来就忙的不行,各种开会,各种方案,各种报告。学习进度有些受阻,回家时间也比较晚整个人的状态也不是很高效,不如好好休息。 安利地址: Useful-Open-Source-React-Native React-Native收纳”裤” 虽然手头事情比较多,但是我还是尽量保持每日一更,每天都要有所收获! 运行效果:实现思路:外观: 外观实现: 事件: 使用与分析:源码地址:react-native-popupWindow 团队开源内容地址(我们会继续努力):https://github.com/PacteraOpenSourceGroup 控件产出:wwl901215 ok,流程走完 我们读一下这个弹窗控件的源码(标注是我发文前加的,给新手学习理解用,如果影响高端玩家读源码抱歉) 如何使用(第一版,暂不考虑发布npm) import MenuPopWindow from '你存放的位置'
实际使用 <MenuPopWindow width={60} height={100} show={this.state.showPop} closeModal={(show) => { this.setState({ showPop: show }) }} dataArray={['第一!!','第二!!','第三!!']} />
import React from 'react'
import {
StyleSheet,View,Text,Image,TouchableOpacity,Alert,//弹窗
Modal,Dimensions,//用于获取设备屏幕的宽高
ART,//绘图,Android默认就包含ART库,IOS需要单独添加依赖库
} from 'react-native'
const { width,height } = Dimensions.get('window');
let mwidth = 70;
let mheight = 100;
const bgColor = '#2d2d2d';//背景色,没有设置外部传入
const top = 50;
let dataArray;//列表数据源
export default class MenuModal extends React.Component {
constructor(props) {
super(props);
this.state = {
isVisible: this.props.show,}
//数据传递
mwidth = this.props.width || 70;
mheight = this.props.height || 100;
dataArray = this.props.dataArray;
}
componentWillReceiveProps(nextProps) {
this.setState({ isVisible: nextProps.show });
}
//处理状态
closeModal() {
this.setState({
isVisible: false
});
this.props.closeModal(false);
}
render() {
//绘制路径
const path = ART.Path();
path.moveTo(width - 10 - mwidth * 1 / 3 + 3,top);
path.lineTo(width - 10 - mwidth * 1 / 3 + 9,top - 7);
path.lineTo(width - 10 - mwidth * 1 / 3 + 15,top);
path.close();
return (
<View style={styles.container}>
<Modal
transparent={true}
visible={this.state.isVisible}
//动画效果类型
animationType={'fade'}
onRequestClose={() => this.closeModal()}>
<TouchableOpacity style={styles.container} activeOpacity={1} onPress={() => this.closeModal()}>
<ART.Surface width={width} height={100} >
<ART.Shape d={path} fill={bgColor} />
</ART.Surface>
<View style={styles.modal}>
<TouchableOpacity activeOpacity={1} onPress={() => Alert.alert('点击了第1个')} style={styles.itemView}>
<Image style={styles.imgStyle} source={require('../res/icon_qr.png')} />
<Text style={styles.textStyle}>{dataArray[0]}</Text>
</TouchableOpacity>
<TouchableOpacity activeOpacity={1} onPress={() => Alert.alert('点击了第2个')} style={[styles.itemView,{ borderColor: '#999',borderTopWidth: 1,borderBottomWidth: 1 }]}>
<Image style={styles.imgStyle} source={require('../res/icon_qr.png')} />
<Text style={styles.textStyle}>{dataArray[1]}</Text>
</TouchableOpacity>
<TouchableOpacity activeOpacity={1} onPress={() => Alert.alert('点击了第3个')} style={styles.itemView}>
<Image style={styles.imgStyle} source={require('../res/icon_qr.png')} />
<Text style={styles.textStyle}>{dataArray[2]}</Text>
</TouchableOpacity>
</View>
</TouchableOpacity>
</Modal>
</View>
)
}
}
//样式链
const styles = StyleSheet.create({
container: {
width: width,height: height,},modal: {
backgroundColor: bgColor,// opacity:0.8,width: mwidth,height: mheight,position: 'absolute',left: width - mwidth - 10,top: top,padding: 5,justifyContent: 'center',alignItems: 'center',borderRadius: 3,itemView: {
flexDirection: 'row',flex: 1,textStyle: {
color: '#fff',fontSize: 14,marginLeft: 2,imgStyle: {
width: 12,height: 12,}
});
有问题可以微信联系我,基佬勿扰哦(活体,非公众号)! (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |