RN调用iOS原生UI

首先在我们的Xcode文件中,新建一个 View 用来实现我们要封装的控件功能。我这里取名为 TakePhotoView。然后经过一番编码(具体功能自己实现)之后,TakePhotoView 就可以为我所用。
然后这个时候呢,我们需要再创建一个 View 继承于 RCTViewManager。在我的项目中,我取名为: TakePhotoManager 。这个文件的作用是用来桥接 RN 层于原生层的通信。

TakePhotoManager.h 文件中的代码如下:

#import <React/RCTViewManager.h>

@interface TakePhotoManager : RCTViewManager<RCTBridgeModule>

@end

这个时候,我们需要对 TakePhotoManager.m 文件进行一些编码,来实现我们的桥接功能。 TakePhotoManager.m 页面的代码如下:

#import "TakePhotoManager.h"
#import "TakePhotoView.h"

@implementation TakePhotoManager

// 标记宏(必要)
RCT_EXPORT_MODULE()

- (UIView *)view {
  TakePhotoView *takePhotoView = [[TakePhotoView alloc] init];
  return takePhotoView;
}

@end

然后在 RN 的项目中,我们新建一个页面,用来承接这个 TakePhotoManager。在 RN 层中,我们新建一个页面,叫做 TakePhotoiOS.js。在这个页面中,我们需要引入在 原生层中暴露出来的 TakePhoto 页面。所以, TakePhotoiOS.js的代码如下:

import React, { Component } from 'react';
import {
  AppRegistry,
  StyleSheet,
  Text,
  View,
  requireNativeComponent,
  NativeModules,
} from 'react-native';

// 该方法将 原生层TakePhotoManager 中 return 出来的 View 赋值给 RNTakePhoto。这个时候 RNTakePhoto 就是我们在原生中中的页面了。
// requireNativeComponent() 该方法中有两个参数,第一个是原生层暴露的UIView,另一个是在RN层要承接的 Class。在这里我们可以看到,原生层暴露的UIView的文件叫做 TakePhotoManager,而在这里用的话,只用TakePhoto。这只能说明,原生层的封装需要按照一定的规则来做。
const RNTakePhoto = requireNativeComponent('TakePhoto', TakePhotoiOS);

class TakePhotoiOS extends Component {
  constructor(props) {
    super(props);
  }

  render() {
    return (
      <RNTakePhoto
        style={styles.container}
      />
    );
  }
}
const styles = StyleSheet.create({
  container: {
    flex: 1,
    backgroundColor: 'transparent',
  },
});
module.exports = TakePhotoiOS;

到了这一步看似我们的封装工作就完成了。运行一遍,发现没有报错,页面也是正常跳转的;但是呢在这个时候,我们发现,相机的视图是一片空白,并没有按照我们想象中的,出现预览图。这个时候我们检查了代码之后,发现,原来在页面将要加载的时候,我们需要让我们的相机开始工作。那么这个时候,我们就需要在 TakePhotoiOS.js 文件中增加如下代码:

// 视图加载完成
componentDidMount() {
   //需要启动相机
}

// 视图将要消失
componentWillUnmount() {
 //需要关闭相机
}

这个时候问题就来了,我们该怎么暴露我们的方法给 RN 层调用呢?很简单,这个时候我们先在我们的 TakePhotoView.h 文件中,暴露两个方法。代码如下:

#import <UIKit/UIKit.h>

@interface TakePhotoView : UIView

// 初始化
- (instancetype)init;

// 相机开始工作
- (void)camareStartRunning;

// 相机停止工作
- (void)camareStopRunning;

然后在 TakePhotoView.m 文件中实现这个两个方法,代码如下:

// 相机开始工作
- (void)camareStartRunning{
  [self.captureSession startRunning];
}

// 相机停止工作
- (void)camareStopRunning {
  [self.captureSession stopRunning];
}

这个时候呢,我们 相机开始工作以及停止工作的两个两个方法都实现了,现在就需要将这两个方法暴露给 RN层那边调用。这个时候,我们需要修改我们的 TakePhotoManager.m 文件,增加一些代码,让我们的RN层能调用到我们露出来的方法。 TakePhotoManager.m的代码改为如下

import "TakePhotoManager.h"

import "TakePhotoView.h"

@interface TakePhotoManager()

@property (nonatomic, strong) TakePhotoView *takePhotoView;

@end

@implementation TakePhotoManager

// 标记宏(必要)
RCT_EXPORT_MODULE()

  • (UIView *)view {
    _takePhotoView = [[TakePhotoView alloc] init];
    return _takePhotoView;
    }

/**

  • 导出方法
  • 相机开始工作
    */
    RCT_EXPORT_METHOD(camareStartRunning) {
    [_takePhotoView camareStartRunning];
    }

/**

  • 导出方法
  • 相机停止工作
    */
    RCT_EXPORT_METHOD(camareStopRunning) {
    [_takePhotoView camareStopRunning];
    }
    @end
    然后我们需要在 RN层中 的文件中,增加如下代码,来实现点击事件的传递,代码如下:
import React, { Component } from 'react';
import {
  AppRegistry,
  StyleSheet,
  Text,
  View,
  requireNativeComponent,
  NativeModules,
  Dimensions,
} from 'react-native';

import NavOutView from '../../../components/NavOutView';
import { Actions } from 'react-native-router-flux';
import { i18n } from "../../../config";

// 该方法将 原生层TakePhotoManager 中 return 出来的 View 赋值给 RNTakePhoto。这个时候 RNTakePhoto 就是我们在原生中中的页面了。
// requireNativeComponent() 该方法中有两个参数,第一个是原生层暴露的UIView,另一个是在RN层要承接的 Class。在这里我们可以看到,原生层暴露的UIView的文件叫做 TakePhotoManager,而在这里用的话,只用TakePhoto。这只能说明,原生层的封装需要按照一定的规则来做。
const RNTakePhoto = requireNativeComponent('TakePhoto', TakePhotoiOS);
// 通过该方法,我们可以拿到 TakePhotoManager.js 中暴露出来的方法
const TakePhotoManager = NativeModules.TakePhotoManager;


class TakePhotoiOS extends Component {
  constructor(props) {
    super(props);
  }

  componentDidMount() {
    /*
    * 这里采用延迟250毫秒后调用相机开启的方法是,因为在
    * 原生层中,TakePhotoView 被创建之后,才能调用 相机开启
    * 也等同于要 RNTakePhoto 被创建之后,才能调用
    */
    this.timeOutReFresh = setTimeout(() => {
      TakePhotoManager.camareStartRunning();
    }, 250);
  }

  componentWillUnmount() {
    // 相机停止工作
    TakePhotoManager.camareStopRunning();
  }

  render() {
    return (
      <RNTakePhoto
        style={styles.container}
      />
    );
  }
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    backgroundColor: 'transparent',
  },
});

module.exports = TakePhotoiOS;

到了这个时候,我们就可以发现我们的相机已经可以工作了。然后这个时候问题又来了,由于我是将相机视图是做成全屏幕的,所以这个时候我也是将返回事件放在了原生的视图中(总之,怎么作死怎么来)。那么问题来了,当我点击了关闭按钮之后,在我的RN层中,要怎么知道我已经点了关闭了呢?这里有两个解决方法:一是直接把关闭按钮做到RN层中,直接在RN层中调用视图返回,关闭相机等方法。二是在原生层中,将关闭按钮的点击事件给暴露出来,然后在RN层中,监听并做相应的处理。这里采用的是第二种方式。于是乎,我们又需要对我们的代码进行改动了。
首先,我们可以先想到,要将 TakePhotoView 中的点击事件方法传出来,可以用到代理,通知,block等方法,这个地方我采用了block。所以在 TakePhotoView.h 文件中,我们需要增加block的声明,代码如下:

#import <UIKit/UIKit.h>

@interface TakePhotoView : UIView

// 关闭按钮的block
typedef void(^onTouchBackBlock)(NSDictionary *dicBlock);

@property (nonatomic, copy) onTouchBackBlock onTouchBackBlock;

// 初始化
- (instancetype)init;

// 相机开始工作
- (void)camareStartRunning;

// 相机停止工作
- (void)camareStopRunning;

@end

在 TakePhotoView.m 文件中,我们需要在关闭按钮的点击事件中,添加上我们的block,添加如下代码:

// 关闭按钮的点击事件
- (void)btnCloseAction:(UIButton *)sender {
  //移除所有的通知
  [self removeNotification];
  // 相机停止工作
  [self camareStartRunning];
  // 实现block
  _onTouchBackBlock(@{@"message": @"goBack"});
}

这个时候,我们还需要在 TakePhotoManager 文件中,将我们的 block 暴露过去给我们的 RN 层调用,那么这个时候我们需要在 TakePhotoManager.m 文件中,增加一个RN 层的 block, 用于将我们 TakePhotoView 的点击回调传递过去,代码如下:

#import "TakePhotoManager.h"
#import "TakePhotoView.h"
#import <Photos/PHPhotoLibrary.h>
#import <AVFoundation/AVCaptureDevice.h>
#import <AVFoundation/AVMediaFormat.h>

@interface TakePhotoManager()

@property (nonatomic, strong) TakePhotoView *takePhotoView;

// 点击返回的block
@property (nonatomic, copy) RCTBubblingEventBlock onTouchBackBlock;

@end

@implementation TakePhotoManager

// 标记宏(必要)
RCT_EXPORT_MODULE()

// 事件的导出
RCT_EXPORT_VIEW_PROPERTY(onTouchBackBlock, RCTBubblingEventBlock)

- (UIView *)view {
  _takePhotoView = [[TakePhotoView alloc] init];
  _takePhotoView.onTouchBackBlock = ^(NSDictionary *dicBlock) {
  };
  return _takePhotoView;
}

/**
 *  相机开始工作
 */
RCT_EXPORT_METHOD(camareStartRunning) {
  [_takePhotoView camareStartRunning];
}

/**
 *  相机停止工作
 */
RCT_EXPORT_METHOD(camareStopRunning) {
  [_takePhotoView camareStopRunning];
}

@end

然后我们需要在 RN层中 的文件中,增加如下代码,来实现点击事件的传递,代码如下:

import React, { Component } from 'react';
import {
  AppRegistry,
  StyleSheet,
  requireNativeComponent,
  NativeModules,
} from 'react-native';

import NavOutView from '../../../components/NavOutView';
import { Actions } from 'react-native-router-flux';
import { i18n } from "../../../config";

const RNTakePhoto = requireNativeComponent('TakePhoto', TakePhotoiOS);
const TakePhotoManager = NativeModules.TakePhotoManager;

class TakePhotoiOS extends Component {
  constructor(props) {
    super(props);
  }

  componentDidMount() {
    /*
    * 这里采用延迟250毫秒后调用相机开启的方法是,因为在
    * 原生层中,TakePhotoView 被创建之后,才能调用 相机开启
    * 也等同于要 RNTakePhoto 被创建之后,才能调用
    */
    this.timeOutReFresh = setTimeout(() => {
      TakePhotoManager.camareStartRunning();
    }, 250);
  }

  componentWillUnmount() {
    TakePhotoManager.camareStopRunning();
  }

  render() {
    return (
      <RNTakePhoto
        style={styles.container}
        onTouchBackBlock={(event) => {
          console.log(event.nativeEvent);
          const eventMessage = event.nativeEvent;
          console.log(eventMessage.message);
          if (eventMessage.message === 'goBack') {
            Actions.pop();
          }
        }}
      />
    );
  }
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    backgroundColor: 'transparent',
  },
});
module.exports = TakePhotoiOS;

https://blog.csdn.net/CCYQ1995/article/details/79703381

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 219,366评论 6 508
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 93,521评论 3 395
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 165,689评论 0 356
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,925评论 1 295
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,942评论 6 392
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,727评论 1 305
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,447评论 3 420
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,349评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,820评论 1 317
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,990评论 3 337
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 40,127评论 1 351
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,812评论 5 346
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,471评论 3 331
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 32,017评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 33,142评论 1 272
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,388评论 3 373
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 45,066评论 2 355