|
| 1 | +#React Native学习总结 |
| 2 | +##React Native是什么? |
| 3 | +React Native是一个框架,一个中间层,通过这套框架我们可以完成IOS和Android的开发。 |
| 4 | +##布局FlexBox |
| 5 | +问题1:使用top时在iOS设备上布局没有问题,在Andorid上会出现位置偏移错位 |
| 6 | + |
| 7 | +解决:使用margin-top就没有问题,因为margin的相对定位,是指相对相邻元素的定位。top这些在绝对定位的前提下,这个绝对定位,相对父级元素的绝对定位。 |
| 8 | + |
| 9 | +问题2:设置不同的flexDirection,居中总是不确定使用alignItems还是justifyContent |
| 10 | + |
| 11 | +解决:justifyContent 根据flexDirection而居中,比如row,就是水平居中,column就是垂直居中,alignItems就是相反 |
| 12 | +___ |
| 13 | +___ |
| 14 | +##图片加载 |
| 15 | +网络资源 |
| 16 | + |
| 17 | +`<Image source={{uri:'http://xxxxx/png'}} />` |
| 18 | + |
| 19 | +本地静态资源 |
| 20 | + |
| 21 | +`<Image source={require('image!lealogo')} />` |
| 22 | + |
| 23 | +问题1:需要使用共能的方法或组件,然后根据不同的图片传人相应的参数,遇到加载本地图片的格式问题 |
| 24 | + |
| 25 | +解决:先定义一个如:type的变量,在使用组件的时候 |
| 26 | + |
| 27 | +`<cell source={这里就是你的本地图片路径比如 require('image!lealogo')}></cell>` |
| 28 | + |
| 29 | +注意:等于号后面要使用{} |
| 30 | + |
| 31 | +在方法内部这样写:`<Image source={this.props.source}/>` |
| 32 | + |
| 33 | +例子: |
| 34 | + |
| 35 | +``` |
| 36 | +//调用 |
| 37 | +<Cell title='客服电话' source={require('./img/personal_service.png')}></Cell> |
| 38 | +<Cell title='设置' source={require('./img/personal_setting.png')}></Cell> |
| 39 | +
|
| 40 | +var Cell = React.createClass({ |
| 41 | + render: function(){ |
| 42 | + return ( |
| 43 | + <View style={styles.cell_item}> |
| 44 | + <Image source={this.props.source} style={{marginLeft:10}}/> |
| 45 | + <Text style={{flex:1,left:10}}>{this.props.title}</Text> |
| 46 | + <Image style={{marginRight:10}} source={require('./img/personal_more.png')}/> |
| 47 | + </View> |
| 48 | + ); |
| 49 | + } |
| 50 | +}); |
| 51 | +``` |
| 52 | +___ |
| 53 | +___ |
| 54 | +##第三方库 |
| 55 | +问题:在个人界面,头像选择弹出一个alerView,并且实现跳转相机或相册然后选择图片回调等,如果自己实现会很麻烦而且耗时,这里我们使用第三方react-native-image-picker是一个不错的选择 |
| 56 | + |
| 57 | +解决:首先安装 npm install react-native-image-picker@latest --save,然后将node_modules中的RNImagePicker.xcodeproj添加到xcode工程中,在添加依赖等,然后运行,这里集成的时候遇到问题就是报找不文件,路径不对,最后把node_modules删除,重新npm install解决问题,使用的时候根据Options参数结合需求相应的配置即可 |
| 58 | + |
| 59 | +github地址:[react-native-image-picker](https://github.com/marcshilling/react-native-image-picker) |
| 60 | +___ |
| 61 | +___ |
| 62 | +##React Native原生模块调用(iOS) |
| 63 | +问题:在项目中遇到地图,拨打电话,清除缓存等iOS与Andiorid机制不同的功能,就需要调用原生的界面或功能,这里说下React Native与iOS混编,Andiorid也是大同小异 |
| 64 | + |
| 65 | +解决: |
| 66 | + |
| 67 | +1 创建原生模块,实现“RCTBridgeModule”协议 |
| 68 | + |
| 69 | +`#import <Foundation/Foundation.h>` |
| 70 | + |
| 71 | +` #import "RCTBridgeModule.h"` |
| 72 | + |
| 73 | +`@interface KSDMapManager : NSObject <RCTBridgeModule>` |
| 74 | + |
| 75 | +`@end` |
| 76 | + |
| 77 | +2 导出模块,导出方法 |
| 78 | + |
| 79 | +`@implementation KSDMapManager` |
| 80 | + |
| 81 | +//导出模块 |
| 82 | + |
| 83 | +`RCT_EXPORT_MODULE();` |
| 84 | + |
| 85 | +``` |
| 86 | +RCT_EXPORT_METHOD(gotoIM:(RCTResponseSenderBlock)callback) |
| 87 | +{ |
| 88 | + __weak typeof(self) weakSelf = self; |
| 89 | + self.callback = callback; |
| 90 | + UIViewController *controller = (UIViewController*)[[[UIApplication sharedApplication] keyWindow] rootViewController]; |
| 91 | + KSDMapLocationViewController *mapVc = [[KSDMapLocationViewController alloc] init]; |
| 92 | + mapVc.handle = ^(NSString *address) { |
| 93 | + weakSelf.itemValue = address; |
| 94 | + NSArray *events = [[NSArray alloc] initWithObjects:self.itemValue, nil]; |
| 95 | + callback(events); |
| 96 | + }; |
| 97 | + [controller presentViewController:mapVc animated:YES completion:nil]; |
| 98 | +} |
| 99 | +``` |
| 100 | +3 js文件中调用 |
| 101 | + |
| 102 | +//创建原生模块实例 |
| 103 | + |
| 104 | +var KSDMapManager = NativeModules.KSDMapManager; |
| 105 | + |
| 106 | +//方法调用 |
| 107 | + |
| 108 | +``` |
| 109 | +KSDMapManager.gotoIM( |
| 110 | + (events)=>{ |
| 111 | + this._inputReceiveAddress(events); |
| 112 | + console.log(events); |
| 113 | + }) |
| 114 | +``` |
| 115 | +___ |
| 116 | +___ |
| 117 | +##热部署 |
| 118 | +###配置 |
| 119 | +H5的一大特点就是热部署,React Native同样也可以,本项目使用的是CodePush |
| 120 | + |
| 121 | +首先安装客户端,创建CodePush账户,然后会调到浏览器让选择,这里我选择的是GitHub的账号,然后授权以后就会给一个秘钥,将此秘钥复制粘贴到命令行,最后`code-push app add <MyAppName>`到这里账号就配置完毕 |
| 122 | + |
| 123 | +接下来安装CodePush插件,然后将react-native-code-push文件夹中CodePush.xcodeproj直接拉入项目中Libraries中,添加依赖等,然后在AppDelegate中添加 |
| 124 | + |
| 125 | +``` |
| 126 | +#ifdef DEBUG |
| 127 | +// [RCTBundleURLProvider sharedSettings].jsLocation = @"192.168.1.120"; |
| 128 | + // [RCTBundleURLProvider sharedSettings].jsLocation = @"192.168.7.120"; |
| 129 | + jsCodeLocation = [[RCTBundleURLProvider sharedSettings] jsBundleURLForBundleRoot:@"index.ios" fallbackResource:nil]; |
| 130 | +#else |
| 131 | + jsCodeLocation = [CodePush bundleURL]; |
| 132 | +#endif |
| 133 | +``` |
| 134 | + |
| 135 | +在命令行下使用code-push deployment ls <AppName> --displayKeys 查出Staging的值,在info.plist文件中添加CodePushDeploymentKey并且把Staging的值填入。 注意在Info.plist中将Bundle versions string, short的值修改为1.0.0,到此所有配置完毕 |
| 136 | + |
| 137 | +###调用 |
| 138 | +在需要改变的js文件中引入import codePush from "react-native-code-push"; |
| 139 | + |
| 140 | +在componentDidMount调用sync方法,当你的App启动的时候会在后台更新 |
| 141 | + |
| 142 | +``` |
| 143 | +componentDidMount(){ |
| 144 | + codePush.sync(); |
| 145 | + } |
| 146 | +``` |
| 147 | +到此位置,所有的基本配置已经全部完成了,可以运行起你的程序啦,不过注意是Release不是Debug |
| 148 | + |
| 149 | +###发布更新 |
| 150 | +将你修改的js文件打包为二进制文件,放入指定的地方(当前位置为根目录)。 |
| 151 | + |
| 152 | +`react-native bundle --platform ios --entry-file index.ios.js --bundle-output codepush.js --dev false` |
| 153 | + |
| 154 | +将二进制文件push到staging 环境中 |
| 155 | + |
| 156 | +`code-push release ksd_geren_rn index.ios.js 1.0.0` |
| 157 | + |
| 158 | +`code-push release ksd_geren_rn codepush.js 1.0.0` |
| 159 | + |
| 160 | +###热部署成功---------------[完整配置链接点击这里](http://blog.csdn.net/u011151353/article/details/50688681) |
| 161 | +___ |
0 commit comments