【REACT NATIVE 系列教程之八】不使用NAVIGATOR实现切换(页面)场景的两种形式(逻辑与MODAL)

简介:

在第五篇中介绍了使用导航Navigator进行(页面)场景的切换,但是对于一些需求其实是无法满足,或者说太浪费。例如:

1. 只是想做一个很简单的页面切换,并不需要记录从哪来~也不需要手势滑屏返回等,这时使用导航Navigator太浪费了。

2. 假设:

a) 有个View 包括一个Navigator组件和一个Image组件

b) Navigator 对页面A ,B,C之间进行导航

此时我们有个需求在B页面切换到一个崭新空白的页面场景中(不带Image),此时我们想要通过Navigator实现需求则变得非常麻烦。(为此,Himi考虑尝试了很多方式)

本篇主要介绍除了使用Navigator 之外的两种切换页面的方法:

一:采用逻辑控制绘制

代码段1:

1
2
3
this .state = {
       pageIndex:0
};

代码段2:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
switch ( this .state.pageIndex) {
            case  0:
              return  (
                ......
              );
            case  1:
                return  (
                  ......
                );
            case  2:
                return  (
                  ......
                );
       }

其实此种方式非常容易理解,是利用对变量的操作,进而控制绘制的不同。

 当通过this.setState()函数对pageIndex进行修改,从而this进行重绘时,会根据pageIndex的值不同,绘制出各不相同的内容。

(运行效果放在最后一并展示)

 

二:采用Modal组件

Modal组件,首先给出官方解释:

Modal组件可以用来覆盖包含React Native根视图的原生视图(如UIViewController,Activity)。

在嵌入React Native的混合应用中可以使用Modal。Modal可以使你应用中RN编写的那部分内容覆盖在原生视图上显示。

在从根视图开始就使用RN编写的应用中,你应该使用Navigator来代替Modal。通过一个最顶层的Navigator,你可以通过configureScene属性更加方便的控制如何将模态场景覆盖显示在你App其余的部分上。

简单来说,利用Modal只是模拟感觉切(页面)场景的,Modal只是覆盖到当前视图上而已,且可以任意布局和添加组件,Modal是个崭新空白的(页面)场景。

示例:

1
2
3
4
5
6
7
<Modal
   animated={ this .state.animationType}
   transparent={ this .state.transparent}
   visible={ this .state.modalVisible}
   onRequestClose={() => { this ._setModalVisible( false )}}
                      >
</Modal>


animated: (bool类型)是否带有动画效果

注意:animated是旧版本才有的属性

新版本属性为:animationType enum(‘none’, ‘slide’, ‘fade’) 

  transparent: (bool类型)是否透明

  visible: (bool类型)是否可见

   onRequestClose:( 回调函数 ) 是指Android的返回实体按钮,ios可以不用处理

需要强调的是,Modal 在设置为可见情况下,默认覆盖当前视图之上。

为了更好的演示效果与代码参考,Himi专门写了一个示例,如下两个文件:

Main.js 是入口显示的主要视图

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
import React, {
   AppRegistry,
   Component,
   View,
   Text,
   StyleSheet,
   TouchableHighlight,
   Modal,
} from  'react-native' ;
  
  
import SecondPage from  "./SecondPage" ;
  
export  default  class Main extends Component {
  constructor(props) {
  super (props);
  this .state = {
       animationType:  true ,
       modalVisible:  true ,
       transparent:  true ,
       pageIndex:0
     };
  }
  
   _setModalVisible(visible) {
     this .setState({modalVisible: visible});
   }
   
   replaceScene(){
     this .setState({pageIndex:1});
   }
   addModal(){
     this .setState({pageIndex:2});
   }
  
   render() {
       switch ( this .state.pageIndex) {
            case  0:
              return  (
                <View style={styles.container} >
  
                  <Text style={styles.himiTextStyle}>Himi React Native 教程</Text>
                  <View style={styles.himiViewStyle}>
                    <TouchableHighlight
                      underlayColor= '#f00'
                      onPress={ this .replaceScene.bind( this )}
                      >
                      <Text style={{fontSize:20}}>点击切换页面</Text>
                    </TouchableHighlight>
  
                    <TouchableHighlight
                      underlayColor= '#f00'
                      onPress={ this .addModal.bind( this )}
                      >
                      <Text style={{fontSize:20}}>点击添加Modal</Text>
                     </TouchableHighlight>
                  </View>
                </View>
              );
            case  1:
                return  <SecondPage/>;
            case  2:
                return  (
                  <View style={styles.container} >
                    <Modal
                      animated={ this .state.animationType}
                      transparent={ this .state.transparent}
                      visible={ this .state.modalVisible}
                      onRequestClose={() => { this ._setModalVisible( false )}}
                      >
                        <View style={styles.modalViewStyle}>
                          <TouchableHighlight
                            underlayColor= '#4169e1'
                            onPress={ this ._setModalVisible.bind( this false )}
                            >
                              <Text style={styles.text} >  我是Modal,点击将隐藏 </Text>
                          </TouchableHighlight>
                       </View>
                    </Modal>
  
                    <Text style={styles.himiTextStyle}>Himi React Native 教程</Text>
                    <View style={styles.himiViewStyle}>
                      <TouchableHighlight
                        underlayColor= '#f00'
                        onPress={ this .replaceScene.bind( this )}
                        >
                        <Text style={{fontSize:20}}>点击切换页面</Text>
  
                       </TouchableHighlight>
                    </View>
                  </View>
                );
       }
     }
};
  
var  styles = StyleSheet.create({
   container: {
     flex: 1,
     flexDirection:  'column' ,
     justifyContent:  'center' ,
     alignItems:  'center' ,
     backgroundColor:  '#F5FCFF' ,
   },
   text: {
     color: '#fff' ,
     fontSize:30,
   },
   himiViewStyle:{
     flex: 1,
     flexDirection:  'column' ,
     justifyContent:  'center' ,
     alignItems:  'center' ,
     backgroundColor:  '#F5FCFF' ,
   },
   modalViewStyle:{
     flex: 1,
     flexDirection:  'column' ,
     justifyContent:  'center' ,
     alignItems:  'center' ,
     backgroundColor:  '#F00' ,
   },
   himiTextStyle:{
     color: '#f00' ,
     fontSize:30,
     marginTop:70,
   },
  
});

SecondPage.js 是用来展示第一种逻辑切页用的

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import React, {
   AppRegistry,
   Component,
   View,
   Text,
   StyleSheet,
   TouchableHighlight,
} from  'react-native' ;
  
import Main from  "./Main" ;
  
export  default  class SecondPage extends React.Component {
  constructor(props) {
  super (props);
     this .state = {
       pageIndex:0
     };
  
  }
   replaceScene(){
     this .setState({pageIndex:1});
   }
  
   render() {
     switch ( this .state.pageIndex) {
       case  0:
         return  (
        <View style={styles.himiViewStyle} >
  
             <View style={styles.himiViewStyle}>
               <TouchableHighlight
                 underlayColor= '#4169e1'
                 onPress={ this .replaceScene.bind( this )}
                 >
                   <Text style={styles.text} >  我是SecondPage,点我切换 </Text>
               </TouchableHighlight>
             </View>
           </View>
         )
       case  1:
           return  <Main/>;
     }
   }
};
  
var  styles = StyleSheet.create({
  
   text: {
     color: '#f00' ,
     fontSize:20,
   },
   himiViewStyle:{
     flex: 1,
     flexDirection:  'column' ,
     justifyContent:  'center' ,
     alignItems:  'center' ,
     backgroundColor:  '#FF0' ,
   },
   himiTextStyle:{
     color: '#f00' ,
     fontSize:30,
     marginTop:70,
   },
  
});

演示效果图:(点击查看动态效果)

user29






本文转自 xiaominghimi 51CTO博客,原文链接:http://blog.51cto.com/xiaominghimi/1785436,如需转载请自行联系原作者

目录
相关文章
|
2月前
|
开发框架 前端开发 JavaScript
探索前端开发中的跨平台框架React Native
本文将介绍前端开发中一种备受关注的跨平台框架React Native,通过比较原生应用与React Native的优缺点,探讨其在实际项目中的应用以及未来发展趋势。
|
2月前
|
开发框架 前端开发 JavaScript
从零开始学习React Native开发
React Native是一种基于React框架的移动端开发框架,使用它可以快速地构建出高性能、原生的移动应用。本文将从零开始,介绍React Native的基础知识和开发流程,帮助读者快速入门React Native开发,并实现一个简单的ToDo应用程序。
|
3月前
|
前端开发 JavaScript Android开发
跨端技术栈综合考察:深入剖析 UniApp、Flutter、Taro 和 React Native 的优势与限制
跨端技术栈综合考察:深入剖析 UniApp、Flutter、Taro 和 React Native 的优势与限制
|
2月前
|
存储 前端开发 JavaScript
从零开始学习React Native开发
【2月更文挑战第1天】React Native是一种跨平台的移动应用程序框架,可以使用JavaScript和React来构建Android和iOS应用程序。本文将带您从零开始学习React Native开发,涵盖了基础知识、组件、样式、布局、API等方面。
|
2月前
|
前端开发 IDE 小程序
【社区每周】React Native 初探;应用中支持添加应用管理员(2月第一期)
【社区每周】React Native 初探;应用中支持添加应用管理员(2月第一期)
34 0
|
3月前
|
移动开发 前端开发 JavaScript
探究移动端混合开发技术:React Native、Weex、Flutter的比较与选择
移动端混合开发技术在移动应用开发领域日益流行,为开发者提供了更高效的跨平台开发方案。本文将比较三种主流混合开发技术:React Native、Weex和Flutter,从性能、生态系统和开发体验等方面进行评估,以帮助开发者在选择适合自己项目的技术时做出明智的决策。
|
3月前
|
移动开发 前端开发 weex
React Native、Weex、Flutter 混合开发技术的比较与选择
移动应用已经成为人们日常生活中不可或缺的一部分,而混合开发技术也随之崛起并逐渐成为主流。本文将比较 React Native、Weex 和 Flutter 三种混合开发技术,并探讨它们各自的优缺点,以及如何根据项目需求做出选择。
49 1
|
3月前
|
开发框架 Dart 前端开发
Flutter vs React Native:跨平台移动应用开发的终极对决
随着移动应用的普及,跨平台移动应用开发成为了一种趋势。Flutter和React Native是当前最受欢迎的跨平台开发框架之一,但它们各自有着不同的特点和优势。本文将对Flutter和React Native进行全方位比较,以帮助开发者了解两个框架的差异,从而选择适合自己的开发工具。
38 3
|
3月前
|
开发框架 前端开发 JavaScript
react native是什么,怎么用
react native是什么,怎么用
44 0
|
3月前
|
移动开发 前端开发 weex
移动端混合开发技术:React Native、Weex、Flutter 之争
在移动应用开发领域,React Native、Weex 和 Flutter 是备受关注的混合开发技术。本文将对它们进行全面比较与评估,以帮助开发者做出明智的选择。我们将从开发生态、性能、跨平台能力和易用性等方面进行比较,为读者提供全面的参考和指导。