react native仿微信PopupWindow效果的實(shí)例代碼
在原生APP開(kāi)發(fā)中,相信很多開(kāi)發(fā)者都會(huì)見(jiàn)到這種場(chǎng)景:點(diǎn)擊右上角更多的選項(xiàng),彈出一個(gè)更多界面供用戶(hù)選擇。這種控件在原生開(kāi)發(fā)中Android可以用PopupWindow實(shí)現(xiàn),在iOS中可以用CMPopTipView,也可以自己寫(xiě)一個(gè)View實(shí)現(xiàn)。其類(lèi)似的效果如下圖所示:

實(shí)現(xiàn)思路分析:
要實(shí)現(xiàn)上面的視圖,有很多種實(shí)現(xiàn)方式。前面的文章說(shuō)過(guò),要實(shí)現(xiàn)彈框相關(guān)的可以用React Native 提供的 Modal組件(Modal組件),使用Modal組件可以實(shí)現(xiàn)我們?cè)_(kāi)發(fā)中的大多數(shù)效果。
要實(shí)現(xiàn)下拉三角,可以讓美工切一個(gè)帶下拉三角的背景,當(dāng)然也可以自己通過(guò)ART實(shí)現(xiàn)(ART繪制)。對(duì)于選項(xiàng)卡的內(nèi)容,在原生開(kāi)發(fā)中為了適應(yīng)更多的場(chǎng)景,我們一般會(huì)選擇使用ListView組件,然后當(dāng)點(diǎn)擊某個(gè)Item的時(shí)候獲得相應(yīng)的屬性即可。為了控制Modal的顯示與消失,我們可以給Modal內(nèi)置一個(gè)isVisible: this.props.show狀態(tài)。
源碼
要實(shí)現(xiàn)上面的效果,會(huì)這涉及到三個(gè)js文件:MorePopWidows.js、Utils.js、HomeActionBar.js,按照先后順序,代碼如下:
Utils.js
import {Dimensions} from 'react-native'
const deviceH = Dimensions.get('window').height
const deviceW = Dimensions.get('window').width
const basePx = 375
export default function px2dp(px) {
return px * deviceW / basePx
}
MorePopWidows.js
import React from 'react'
import {
StyleSheet,
Platform,
View,
Text,
Image,
TouchableOpacity,
Alert,
Modal,
Dimensions,
} from 'react-native'
import SpacingView from "./SpacingView";
import QRScanPage from "../home/QRScanPage";
const { width, height } = Dimensions.get('window');
import px2dp from '../util/Utils'
const mTop = px2dp(Platform.OS == "ios" ? 64 : 44)
let mwidth = 95;
let mheight = 100;
const marginTop = mTop;
export default class MorePopWidows extends React.Component {
constructor(props) {
super(props);
this.state = {
isVisible: this.props.show,
}
mwidth = this.props.width ;
mheight = this.props.height ;
}
componentWillReceiveProps(nextProps) {
this.setState({ isVisible: nextProps.show });
}
closeModal() {
this.setState({
isVisible: false
});
this.props.closeModal(false);
}
scan() {
this.props.navigator.push({
component: QRScanPage,
})
}
render() {
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()}>
<View style={styles.modal}>
<TouchableOpacity activeOpacity={1} onPress={this.scan.bind(this)} style={styles.itemView}>
<Image style={styles.imgStyle} source={require('../images/ic_scan_code_white.png')} />
<Text style={styles.textStyle}>掃一掃</Text>
</TouchableOpacity>
<SpacingView/>
<TouchableOpacity activeOpacity={1} onPress={() => Alert.alert('點(diǎn)擊了付款碼')} style={styles.itemView}>
<Image style={styles.imgStyle} source={require('../images/ic_code_white.png')} />
<Text style={styles.textStyle}>付款碼</Text>
</TouchableOpacity>
</View>
</TouchableOpacity>
</Modal>
</View>
)
}
}
const styles = StyleSheet.create({
container: {
width: width,
height: height,
},
modal: {
backgroundColor: '#696969',
width: mwidth,
height: mheight,
position: 'absolute',
left: width - mwidth - 10,
top: marginTop,
padding: 5,
justifyContent: 'center',
alignItems: 'center',
borderRadius: 3,
},
itemView: {
flexDirection: 'row',
justifyContent: 'center',
alignItems: 'center',
flex: 1,
},
textStyle: {
color: '#fff',
fontSize: 14,
marginLeft: 2,
},
imgStyle: {
width: 20,
height: 20,
}
});
最后是在代碼中使用MorePopWidows的代碼:
HomeActionBar.js
/**
* https://github.com/facebook/react-native
* @flow 首頁(yè)的標(biāo)題欄
*/
import React, {Component} from 'react';
import {Platform, View, Dimensions, Text, StyleSheet, TouchableOpacity, Image} from 'react-native';
import SelectCityPage from '../home/SelectCityPage'
import MorePopWidows from '../component/MorePopWidows'
import px2dp from '../util/Utils'
const isIOS = Platform.OS == "ios"
const {width, height} = Dimensions.get('window')
const headH = px2dp(isIOS ? 64 : 44)
export default class HomeActionBar extends Component {
constructor(props) {
super(props);
this.state = {
showPop: false,
}
}
city() {
this.props.navigator.push({
component: SelectCityPage,
})
}
renderHeader() {
return (
<View >
<View style={styles.headerStyle}>
<TouchableOpacity style={styles.action} onPress={this.city.bind(this)}>
<Text style={styles.text}>上海</Text>
<Image
source={require('../images/ic_arrow_down.png')}/>
</TouchableOpacity>
<TouchableOpacity style={styles.searchBar}>
<Image source={require('../images/ic_search.png')} style={styles.iconStyle}/>
<Text style={{fontSize: 13, color: "#666", marginLeft: 5}}>輸入商家、商品名稱(chēng)</Text>
</TouchableOpacity>
<TouchableOpacity style={styles.action} onPress={() => { this.setState({ showPop: !this.state.showPop }) }}>
<Image style={styles.scanIcon}
source={require('../images/ic_scan_code_white.png')}/>
<Text style={styles.scanText}>掃碼</Text>
</TouchableOpacity>
</View>
<View style={{ position: 'absolute', top: headH, left: 0, width: width, height: height }}>
<MorePopWidows width={90} height={100} show={this.state.showPop} closeModal={(show) => {
this.setState({showPop: show})
}} {...this.props}/>
</View>
</View>
)
}
render() {
return (
<View>
{this.renderHeader()}
</View>
);
}
}
const styles = StyleSheet.create({
headerStyle: {
backgroundColor: "#06C1AE",
height: headH,
paddingTop: px2dp(isIOS ? 20 : 0),
paddingHorizontal: 16,
flexDirection: 'row',
alignItems: 'center',
},
searchBar: {
width: width * 0.65,
height: 30,
borderRadius: 19,
marginLeft: 10,
flexDirection: 'row',
justifyContent: 'flex-start',
alignItems: 'center',
backgroundColor: 'white',
alignSelf: 'center',
paddingLeft: 10,
},
text: {
fontSize: 16,
color: '#ffffff',
justifyContent: 'center',
},
iconStyle: {
width: 22,
height: 22,
},
action: {
flexDirection: 'row',
justifyContent: 'center',
alignItems: 'center',
},
scanIcon: {
width: 28,
height: 28,
alignItems: 'center',
marginLeft: 10,
},
scanText: {
fontSize: 14,
color: '#ffffff',
justifyContent: 'center',
alignItems: 'center',
},
});
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
react組件封裝input框的防抖處理的項(xiàng)目實(shí)現(xiàn)
本文主要介紹了react組件封裝input框的防抖處理的項(xiàng)目實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-04-04
React過(guò)渡動(dòng)畫(huà)組件基礎(chǔ)使用介紹
在開(kāi)發(fā)中,我們想要給一個(gè)組件的顯示和消失添加某種過(guò)渡動(dòng)畫(huà),可以很好的增加用戶(hù)體驗(yàn)。 當(dāng)然,我們可以通過(guò)原生的CSS來(lái)實(shí)現(xiàn)這些過(guò)渡動(dòng)畫(huà),這篇文章主要介紹了React過(guò)渡動(dòng)畫(huà)組件使用2022-09-09
react實(shí)現(xiàn)瀏覽器自動(dòng)刷新的示例代碼
這篇文章主要介紹了react實(shí)現(xiàn)瀏覽器自動(dòng)刷新的示例代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-04-04
Component與PureComponent對(duì)比解析
這篇文章主要為大家介紹了Component與PureComponent解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-03-03
TS裝飾器bindThis優(yōu)雅實(shí)現(xiàn)React類(lèi)組件中this綁定
這篇文章主要為大家介紹了TS裝飾器bindThis優(yōu)雅實(shí)現(xiàn)React類(lèi)組件中this綁定,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-11-11
Reactjs實(shí)現(xiàn)通用分頁(yè)組件的實(shí)例代碼
這篇文章主要介紹了Reactjs實(shí)現(xiàn)通用分頁(yè)組件的實(shí)例代碼,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友參考下吧2017-01-01

