React-Native 之 Modal介绍与使用

前端之家收集整理的这篇文章主要介绍了React-Native 之 Modal介绍与使用前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

前言


  • 遗漏的常用组件,刚发现官方有提供,这边也来介绍一下。
  • 如本文有错或理解偏差欢迎联系我,会尽快改正更新!
  • 如有什么问题,也可直接通过邮箱 277511806@qq.com 联系我。

本章涉及资源下载:

链接: https://pan.baidu.com/s/1o84o6JS 密码: htx6

属性


  • animationType(动画类型) PropTypes.oneOf(['none','slide','fade'])
    • none:没有动画
    • slide:从底部滑入
    • fade:淡入视野
  • onRequestClose(被销毁时会调用函数)Platform.OS ==='android'?PropTypes.func.isrequired:PropTypes.func
    • 在 'Android' 平台,必需使用此函数
  • onShow(模态显示的时候被调用)function

  • transparent (透明度) bool
    • true时,使用透明背景渲染模态。
  • visible(可见性) bool
    • 决定模态是否可见
  • onOrientationChange(方向改变时调用)PropTypes.func
    • 在模态方向变化时调用,提供的方向只是 '' 或 ''。在初始化渲染的时候也会调用,但是不考虑当前方向。
  • supportedOrientations(允许模态旋转到任何指定取向)PropTypes.arrayOf(PropTypes.oneOf(['portrait','portrait-upside-down','landscape','landscape-left','landscape-right']))
    • 在iOS上,模态仍然受 info.plist 中的 UISupportedInterfaceOrientations字段中指定的限制。

  • modal的使用很广泛,这边我们来看看怎么让视图以模态的形式展示:

    1. export default class One extends Component {
    2.  
    3. // 构造
    4. constructor(props) {
    5. super(props);
    6. // 初始状态
    7. this.state = {
    8. isModal:false
    9. };
    10. }
    11.  
    12. showModal() {
    13. this.setState({
    14. isModal:true
    15. })
    16. }
    17.  
    18. onRequestClose() {
    19. this.setState({
    20. isModal:false
    21. });
    22. }
    23.  
    24. render() {
    25. return(
    26. <View style={styles.container}>
    27. {/* 初始化Modal */}
    28. <Modal
    29. animationType='slide' // 从底部滑入
    30. transparent={false} // 不透明
    31. visible={this.state.isModal} // 根据isModal决定是否显示
    32. onRequestClose={() => {this.onRequestClose()}} // android必须实现
    33. >
    34. <View style={styles.modalViewStyle}>
    35. {/* 关闭页面 */}
    36. <TouchableOpacity
    37. onPress={() => {{
    38. this.setState({
    39. isModal:false
    40. })
    41. }}}
    42. >
    43. <Text>关闭页面</Text>
    44. </TouchableOpacity>
    45. </View>
    46. </Modal>
    47.  
    48. {/* 返回按钮 */}
    49. <TouchableOpacity
    50. onPress={() => {{
    51. this.props.navigator.pop()
    52. }}}
    53. >
    54. <Text>返回</Text>
    55. </TouchableOpacity>
    56.  
    57. {/* 模态跳转 */}
    58. <TouchableOpacity
    59. onPress={() => this.showModal()}
    60. >
    61. <Text>模态跳转</Text>
    62. </TouchableOpacity>
    63. </View>
    64. );
    65. }
    66. }

  • 这边我们再来做一个经常使用的功能 —— 指示器

    1. export default class One extends Component {
    2.  
    3. // 构造
    4. constructor(props) {
    5. super(props);
    6. // 初始状态
    7. this.state = {
    8. isModal:false
    9. };
    10. }
    11.  
    12. showModal() {
    13. this.setState({
    14. isModal:true
    15. })
    16.  
    17. setTimeout(() => {
    18. this.setState({
    19. isModal:false
    20. });
    21. },1500)
    22. }
    23.  
    24. onRequestClose() {
    25. this.setState({
    26. isModal:false
    27. });
    28. }
    29.  
    30. render() {
    31. return(
    32. <View style={styles.container}>
    33. {/* 初始化Modal */}
    34. <Modal
    35. animationType='fade' // 淡入淡出
    36. transparent={true} // 透明
    37. visible={this.state.isModal} // 根据isModal决定是否显示
    38. onRequestClose={() => {this.onRequestClose()}} // android必须实现
    39. >
    40. <View style={styles.modalViewStyle}>
    41. <View style={styles.hudViewStyle}>
    42. <ActivityIndicator style={styles.chrysanthemumStyle}></ActivityIndicator>
    43. <Text style={styles.hudTextStyle}>加载中…</Text>
    44. </View>
    45. </View>
    46. </Modal>
    47.  
    48. {/* 返回按钮 */}
    49. <TouchableOpacity
    50. onPress={() => {{
    51. this.props.navigator.pop()
    52. }}}
    53. >
    54. <Text>返回</Text>
    55. </TouchableOpacity>
    56.  
    57. {/* 显示指示器 */}
    58. <TouchableOpacity
    59. onPress={() => this.showModal()}
    60. >
    61. <Text>显示指示器</Text>
    62. </TouchableOpacity>
    63. </View>
    64. );
    65. }
    66. }

总结


  • modal 的源码可以看出,modal 其实就是使用了 绝对定位,所以当 modal 无法满足我们的需求的时候,我们就可以通过 绝对定位 自己来封装一个 modal 了,对吧,时间关系,这边就不讲了,大伙自己试试就可以了。

猜你在找的React相关文章