ios - 重新映射 native 组件属性的正确方法是什么

标签 ios swift react-native

我是 Swift 和 React-Native 的新手。

有一个第三方原生组件,称为 NativeComp,它是 UIView 的子类。

我想将它包装为react-native组件并将js属性重新映射到 native 属性(一些重命名工作)

例如

js

 <RNComponent config={enable:true, size:5}/>

将生成 native 代码。

component.enable=true
component.size=5
  • 方法 1

    RNComponent扩展了NativeComp,并添加了一个新的setConfig,在此函数中配置self。

    它可以工作,但有点奇怪。

  • 方法 2 编写一个独立的 UIView 类 ContainerView,添加 NativeComp 作为 subview 。

    override init(frame: CoreGraphics.CGRect) {
    
        self._component = NativeComp(frame: frame);
    
        super.init(frame: frame);
    
        self.addSubview(_component);
    
    }
    
    override func reactSetFrame(_ frame: CGRect)
    {
        _component.reactSetFrame(frame);
    }
    

    它看起来比方法1干净得多,但是因为_component超出了ContainerView边框,所以触摸事件丢失了。

    如果我写成

    override func reactSetFrame(_ frame: CGRect)
    {
        self.reactSetFrame(frame);
        _component.reactSetFrame(frame);
    }
    

程序崩溃执行错误访问self.reactSetFrame(frame);

我不太明白为什么。

将 js 属性重新映射到现有 native 组件的正确方法是什么?

最佳答案

我的错误。

self.reactSetFrame(frame)会导致无限递归。

更改为 super.reactSetFrame 后,一切正常。

关于ios - 重新映射 native 组件属性的正确方法是什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42490603/

相关文章:

javascript - 如何确定视频是否刚刚完成使用 expo.video? ReactNative/Expo.io

ios - json解析崩溃

html - 如何从另一个 UIWebView 打开 UIWebView?

ios - NSFetchedResultsController 对象在更新时从 UITableView 中删除

ios - 使用 iboutlet tableview Swift 子类化

java - 在react-native run-android期间安装Gradle时出现SSL错误

javascript - 共享目录中的文件

swift - 我无法在我的 iOS 应用程序中将用户添加到 Cloud Firestore

swift - (Swift) 平移和缩放限制为特定尺寸的图像

ios - 如何改变SpriteKit的坐标系