反应本机无效本机视图

时间:2020-01-09 06:38:13

标签: android ios react-native react-native-android react-native-ios

我正在处理本机UI组件。我试图弄清楚如何使本机视图无效,以便它重新呈现到屏幕上。我想要实现的是类似HTML画布的工具,该工具将允许动态绘制不同的形状和图像以及随着时间的推移对这些形状进行动画处理。为此,我认为我可以覆盖每个平台上的draw()方法,这将展现出我的内心想象。

但是,当我调用View.invalidate()UIView.setNeedsDisplay()时,不会调用draw方法。此外,即使我调用draw()方法,也不会将任何内容重新呈现到屏幕上。

在Android上,我终于可以通过调用View.invalidateOutline()找到解决方法。无论出于何种原因,此方法都会导致调用draw()并重新渲染屏幕。但是,尽管我已经在阳光下进行了所有尝试,但我仍无法在iOS上找到类似的解决方法。

我什至尝试安装ART Objective-C库来查看它是否可以满足我的要求,但是看到相同的问题(SurfaceView.invalidate())无法调用draw()

我猜想React Native可以控制是否通过某种内部消息传递来重绘事物。我的问题是:是否可以通过某种方式通知本机视图已更改,应重新绘制。否则,这是UIView.setNeedsDisplay()不会重新呈现的错误吗?

反应本机版本: 61.5

复制步骤

iOS(目标C):

- (void)createShape:(int)x y:(int)y
{
    // add a new shape object to be drawn
    [shapes addObject:[NSNumber numberWithInt:x]];

    // drawRect() is not called
    [self setNeedsDisplay];

    // drawRect() is called, but the screen does not update/re-render with the new shape
    [[self layer] setNeedsDisplay];
}

- (void)drawRect:(CGRect)rect
{
    RCTLog(@"drawRect()");
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGRect bounds = self.bounds;
    CGContextClearRect(context, bounds);

    CGContextSetRGBFillColor(context, 1.0, 0.0, 0.0, 1.0);
    // even when this method is called, the rectangle is not drawn
    CGContextFillRect(context, rectangle);
}

Android(kotlin):

  fun createShape(x: Float, y: Float) {
        shapes.add(Drop(x, y, 0F))

        draw(_canvas)

        // screen does not update
       invalidate()

       // updates screen
        invalidateOutline()
    }

还可以链接到当前的整个项目(在尝试所有不同的方法时,有很多注释/临时代码): https://github.com/twalk4821/draw

谢谢。

0 个答案:

没有答案