基礎篇章:關于 React Native 之 Modal 組件的講解
Modal是模態視圖,它的作用是可以用來覆蓋 React Native中根視圖的原生視圖,Modal模態視圖是一種覆蓋包圍當前內容視圖的一個簡單方法。
注意:如果你需要如何在您的應用程序的其余部分呈現模態的更多控制,那么可以考慮使用頂級導航(top-level Navigator)。
Modal 屬性
照例,我想大家都知道我的習慣了,畢竟官網也是這個順序,那就是在用人之前,先要了解人,畢竟疑人不用,用人不疑嘛,要想相信一個人,首先得了解一個人嘛。來,看看 Modal 的相關屬性。
- animated bool 是否有動畫效果,不過這個屬性已經被拋棄了,取之代替的是:animationType
-
animationType ([‘none’, ‘slide’, ‘fade’]) 這個animationType屬性作用就是如何控制模態動畫,有一下三個類型:
- none 出現的時候不帶動畫效果
- fade 帶有淡入動畫的效果
- slide 從底部滑動出來的動畫效果
-
onRequestClose Platform.OS === ‘android’ ? PropTypes.func.isRequired : PropTypes.func 這是一個 Android 平臺需要的屬性,它的作用是當這個模態視圖取消或者關閉消失的時候回調這個函數
- onShow function 當模態視圖顯示的時候調用此函數
- transparent bool 布爾值,是否透明,true 將使得在一個透明背景的模式
- visible bool 布爾值,是否可見
- onOrientationChange func ios 當在顯示模態的方向變化時回調此函數
- supportedOrientations ios ([‘portrait’, ‘portrait-upside-down’, ‘landscape’, ‘landscape-left’, ‘landscape-right’]))
實例演示
來,我們大家一起看看這個效果的實現,看完效果就更加直觀的能夠感受到這個組件的作用和功能了。動態效果圖如下:
實例代碼
import React, { Component } from 'react';
import {
AppRegistry,
StyleSheet,
Modal,
Picker,
Switch,
TouchableHighlight,
Text,
View
} from 'react-native';
classButtonextendsComponent{
state = {
active: false,
};
_onHighlight = ()=> {
this.setState({active: true});
};
_onUnhighlight = ()=> {
this.setState({active: false});
};
render() {
var colorStyle = {
color: this.state.active ? '#fff' : '#000',
};
return (
<TouchableHighlight
onHideUnderlay={this._onUnhighlight}
onPress={this.props.onPress}
onShowUnderlay={this._onHighlight}
style={[styles.button, this.props.style]}
underlayColor="#a9d9d4">
<Textstyle={[styles.buttonText,colorStyle]}>{this.props.children}</Text>
</TouchableHighlight>
);
}
}
export default classModalDemoextendsComponent{
state = {
animationType: 'none',
modalVisible: false,
transparent: false,
};
_setModalVisible = (visible) => {
this.setState({modalVisible: visible});
};
_setAnimationType = (type) => {
this.setState({animationType: type});
};
_toggleTransparent = ()=> {
this.setState({transparent: !this.state.transparent});
};
render() {
var modalBackgroundStyle = {
backgroundColor: this.state.transparent ? 'rgba(0, 0, 0, 0.5)' : '#f5fcff',
};
var innerContainerTransparentStyle = this.state.transparent
? {backgroundColor: '#fff', padding: 20}
: null;
var activeButtonStyle = {
backgroundColor: '#ddd'
};
return (
<View>
<Modal
animationType={this.state.animationType}
transparent={this.state.transparent}
visible={this.state.modalVisible}
onRequestClose={() => this._setModalVisible(false)}
>
<Viewstyle={[styles.container,modalBackgroundStyle]}>
<Viewstyle={[styles.innerContainer,innerContainerTransparentStyle]}>
<Text>This modal was presented {this.state.animationType === 'none' ? 'without' : 'with'} animation.</Text>
<Button
onPress={this._setModalVisible.bind(this, false)}
style={styles.modalButton}>
Close
</Button>
</View>
</View>
</Modal>
<Viewstyle={styles.row}>
<Textstyle={styles.rowTitle}>Animation Type</Text>
<ButtononPress={this._setAnimationType.bind(this,'none')}style={this.state.animationType==='none'?activeButtonStyle:{}}>
none
</Button>
<ButtononPress={this._setAnimationType.bind(this,'slide')}style={this.state.animationType==='slide'?activeButtonStyle:{}}>
slide
</Button>
<ButtononPress={this._setAnimationType.bind(this,'fade')}style={this.state.animationType==='fade'?activeButtonStyle:{}}>
fade
</Button>
</View>
<Viewstyle={{marginTop:50,flexDirection:'row',alignItems:'center',justifyContent:'center'}}>
<Textstyle={{color:'grey',fontWeight:'bold',marginRight:20}}>Transparent</Text>
<Switchvalue={this.state.transparent}onValueChange={this._toggleTransparent}/>
</View>
<ButtononPress={this._setModalVisible.bind(this,true)}>
Present
</Button>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
padding: 20,
},
innerContainer: {
borderRadius: 10,
alignItems: 'center',
},
row: {
alignItems: 'center',
flex: 1,
flexDirection: 'row',
marginBottom: 20,
},
rowTitle: {
flex: 1,
fontWeight: 'bold',
},
button: {
borderRadius: 5,
flex: 1,
height: 44,
alignSelf: 'stretch',
justifyContent: 'center',
overflow: 'hidden',
},
buttonText: {
fontSize: 18,
margin: 5,
textAlign: 'center',
},
modalButton: {
marginTop: 10,
},
pickerItem: {
fontSize: 16,
},
});
AppRegistry.registerComponent('ModalDemo', () => ModalDemo);
這個例子內容比較多,大家仔細看看,最好動手實踐一下,這樣才能掌握的更加熟練。
來自:http://godcoder.me/2017/01/04/基礎篇章:關于 React Native 之 Modal 組件的講解/
本文由用戶 qcpp4096 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!