前言
這次要給大家分享的是基於ReactNative開發的聊天APP實戰專案RN_ChatRomm,運用react-native+react-navigation+react-redux+react-native-image-picker+rnPop 等技術進行框架搭建開發,開發期間採坑了不少,不過 ReactNative 社群比較完善,很多問題都在網上找到了解決方案。
技術實現
- RN腳手架:react-native + react-native-cli
- 狀態管理:react-redux + redux
- 頁面導航器:react-navigation
- RN模態框元件:rnPop.js
- 打包工具:webpack 2.0
- 滑動元件:react-native-swiper
- 圖片選取:react-native-image-picker
{
"name": "RN_ChatRoom",
"aboutMe": "QQ:282310962、wx:xy190310",
"dependencies": {
"react": "16.8.6",
"react-native": "0.60.4"
},
"devDependencies": {
"@babel/core": "^7.5.5",
"@babel/runtime": "^7.5.5",
"@react-native-community/async-storage": "^1.6.1",
"@react-native-community/eslint-config": "^0.0.5",
"babel-jest": "^24.8.0",
"eslint": "^6.1.0",
"jest": "^24.8.0",
"metro-react-native-babel-preset": "^0.55.0",
"react-native-gesture-handler": "^1.3.0",
"react-native-image-picker": "^1.0.2",
"react-native-swiper": "^1.5.14",
"react-navigation": "^3.11.1",
"react-redux": "^7.1.0",
"react-test-renderer": "16.8.6",
"redux": "^4.0.4",
"redux-thunk": "^2.3.0"
}
}
RN建立公共樣式style
新建style.js公共樣式庫,通過RN 中提供的global全域性變數,在app.js中一次引入,然後所有頁面均可以呼叫。
<View style={[GStyle.borT, GStyle.bg_45cff5, GStyle.mt_10]}></View>
global.GStyle = {
pixel: 1 / PixelRatio.get(),
// 邊框
borT: {borderTopWidth: 1 / PixelRatio.get(), borderTopColor: '#dedede',},
borB: {borderBottomWidth: 1 / PixelRatio.get(), borderBottomColor: '#dedede',},
/* __ 佈局控制 */
align_l: {textAlign: 'left'},
align_c: {textAlign: 'center'},
align_r: {textAlign: 'right'},
pos_rel: {position: 'relative'},
pos_abs: {position: 'absolute'},
/* __ 顏色(背景、文字) */
bg_fff: {backgroundColor: '#fff'},
bg_45cff5: {backgroundColor: '#45cff5'},
c_fff: {color: '#fff'},
c_999: {color: '#999'},
c_45cff5: {color: '#45cff5'},
/* __ 字號 */
fs_14: {fontSize: 14},
fs_16: {fontSize: 16},
fs_20: {fontSize: 20},
fs_24: {fontSize: 24},
/* __ 字型 */
ff_ic: {fontFamily: 'iconfont'},
ff_ar: {fontFamily: 'arial'},
iconfont: {fontFamily: 'iconfont', fontSize: 16,},
/* __ 間距( 5/10/15/20/25/30/50 ) */
mt_10: {marginTop: 10}, mt_15: {marginTop: 15}, mt_20: {marginTop: 20},
mb_10: {marginBottom: 10}, mb_15: {marginBottom: 15}, mb_20: {marginBottom: 20},
/* __ 行高 */
lh_20: {lineHeight: 20},
lh_25: {lineHeight: 25},
lh_30: {lineHeight: 30},
lh_35: {lineHeight: 35},
lh_40: {lineHeight: 40},
flex1: {flex: 1},
flex2: {flex: 2},
flex_alignC: {alignItems: 'center'},
flex_alignT: {alignItems: 'flex-start'},
flex_alignB: {alignItems: 'flex-end'},
}
RN製作全屏APP啟動頁
reactNative如何實現沉浸式效果,只需把 StatusBar 設定為透明,這樣狀態列和背景頁面一體了。<statusbar backgroundcolor="transparent" barstyle="light-content" translucent="{true}"></statusbar>
export default class Splash extends Component{
constructor(props){
super(props)
this.state = {
animFadeOut: new Animated.Value(1),
}
}
render(){
return (
<Animated.View style={[GStyle.flex1DC_a_j, {backgroundColor: '#1a4065', opacity: this.state.animFadeOut}]}>
<StatusBar backgroundColor='transparent' barStyle='light-content' translucent={true} />
<View style={GStyle.flex1_a_j}>
<Image source={require('../assets/img/ic_default.jpg')} style={{borderRadius: 100, width: 100, height: 100}} />
</View>
<View style={[GStyle.align_c, {paddingVertical: 20}]}>
<Text style={{color: '#dbdbdb', fontSize: 12, textAlign: 'center',}}>RN-ChatRoom v1.0.0</Text>
</View>
</Animated.View>
)
}
componentDidMount(){
storage.get('hasLogin', (err, object) => {
setTimeout(() => {
Animated.timing(
this.state.animFadeOut, {duration: 300, toValue: 0}
).start(()=>{
this.props.navigation.navigate('Index')
})
}, 1500);
})
}
}
RN自定義頂部條headerBar
export default class HeaderBar extends Component {
constructor(props){
super(props)
this.state = {
searchInput: ''
}
}
render() {
/**
* 更新
* @param { navigation | 頁面導航 }
* @param { title | 標題 }
* @param { center | 標題是否居中 }
* @param { search | 是否顯示搜尋 }
* @param { headerRight | 右側 Icon 按鈕 }
*/
let{ navigation, title, bg, center, search, headerRight } = this.props
return (
<View style={GStyle.flex_col}>
<StatusBar backgroundColor={bg ? bg : GStyle.headerBackgroundColor} barStyle='light-content' translucent={true} />
<View style={[styles.rnim__topBar, GStyle.flex_row, {backgroundColor: bg ? bg : GStyle.headerBackgroundColor}]}>
{/* 返回 */}
<TouchableOpacity style={[styles.iconBack]} activeOpacity={.5} onPress={this.goBack}><Text style={[GStyle.iconfont, GStyle.c_fff, GStyle.fs_18]}></Text></TouchableOpacity>
{/* 標題 */}
{ !search && center ? <View style={GStyle.flex1} /> : null }
{
search ?
(
<View style={[styles.barSearch, GStyle.flex1, GStyle.flex_row]}>
<TextInput onChangeText={text=>{this.setState({searchInput: text})}} style={styles.barSearchText} placeholder='搜尋' placeholderTextColor='rgba(255,255,255,.6)' />
</View>
)
:
(
<View style={[styles.barTit, GStyle.flex1, GStyle.flex_row, center ? styles.barTitCenter : null]}>
{ title ? <Text style={[styles.barCell, {fontSize: 16, paddingLeft: 0}]}>{title}</Text> : null }
</View>
)
}
{/* 右側 */}
<View style={[styles.barBtn, GStyle.flex_row]}>
{
!headerRight ? null : headerRight.map((item, index) => {
return(
<TouchableOpacity style={[styles.iconItem]} activeOpacity={.5} key={index} onPress={()=>item.press ? item.press(this.state.searchInput) : null}>
{
item.type === 'iconfont' ? item.title : (
typeof item.title === 'string' ?
<Text style={item.style ? item.style : null}>{`${item.title}`}</Text>
:
<Image source={item.title} style={{width: 24, height: 24, resizeMode: 'contain'}} />
)
}
{/* 圓點 */}
{ item.badge ? <View style={[styles.iconBadge, GStyle.badge]}><Text style={GStyle.badge_text}>{item.badge}</Text></View> : null }
{ item.badgeDot ? <View style={[styles.iconBadgeDot, GStyle.badge_dot]}></View> : null }
</TouchableOpacity>
)
})
}
</View>
</View>
</View>
)
}
goBack = () => {
this.props.navigation.goBack()
}
}
RN自定義Modal彈窗元件(仿微信/ios效果)
專案中用到的彈窗是自己基於Modal開發的自定義彈窗元件|dialog對話方塊|toast提示,支援多種呼叫方式,具體的可以去看看這篇文章介紹 https://www.cnblogs.com/xiaoyan2017/p/1129...
static defaultProps = {
isVisible: false, //彈窗顯示
title: '', //標題
content: '', //內容
style: null, //自定義彈窗樣式 {object}
contentStyle: null, //內容樣式
skin: '', //自定義彈窗風格
icon: '', //自定義彈窗圖示
shade: true, //遮罩層
shadeClose: true, //點選遮罩層關閉
opacity: '', //遮罩層透明度
time: 0, //彈窗自動關閉秒數
xtime: false, //顯示關閉秒數
end: null, //銷燬彈窗時回撥函式
anim: 'scaleIn', //彈窗動畫( scaleIn / fadeIn / top / bottom / left / right )
follow: null, //跟隨定位(適用於在長按位置定位彈窗)
position: '', //彈窗位置
btns: null, //彈窗按鈕(不設定則不顯示按鈕)[{...options}, {...options}]
}
RN聊天表情處理
方法一: 通過特殊符處理,[哭泣] (:88 類似這樣的,處理起來比較麻煩,而且圖片多了會影響頁面效能。
方法二: 使用 emoj 表情符,這種方式處理比較簡單,網上很多表情符可用,而且不需要特殊處理,效能也還不錯。如果要求不高,推薦這種方式。
好了,今天的分享就到這裡,希望能給到你們些許幫助~~~