【问题标题】:how to add and redraw a CALayer from a background task?如何从后台任务添加和重绘 CALayer?
【发布时间】:2015-11-10 07:01:19
【问题描述】:
  • 我有一个 UIView “myuiview”。
  • 我在该视图中添加了一些 CALayers。在我的示例中,它只有一个“calayer”
  • 在后台任务中,我正在创建一个复杂的形状并将其添加到 calayer。
  • 在主线程中,我刷新 calayer 并希望在屏幕上看到新形状。

新的形状只有在移动屏幕并调用CGAffineTransformTranslate(myuiview,x,y);后才会出现

我做错了什么?

dispatch_async(backgroundQueue, {

    var calayer:CALayer=CALayer();  
    myuiview.layer.addSublayer(calayer);
    myshape=createShape(); // takes long, thats why in the background.

    dispatch_async(dispatch_get_main_queue()) {
        calayer.addSublayer(myshape)
        calayer.setNeedsDisplay(); // No update on the screen !!!!
    }
})

【问题讨论】:

    标签: ios multithreading swift calayer


    【解决方案1】:

    这似乎有问题,因为您从非主线程管理层层次结构。在调度主队列块中移动相关代码,它应该可以工作。为了处理性能,您可以在离线位图上下文中的非主线程中执行渲染,生成 CGImage 并将其作为内容提供给主线程中的图层

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-02-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-08
      • 1970-01-01
      • 2023-04-06
      • 1970-01-01
      相关资源
      最近更新 更多