首页 > 解决方案 > iOS 15:SwiftUI Canvas/TimelineView 糟糕的性能

问题描述

玩转 iOS 15 的新 Canvas/TimelineView。我尝试使用官方 WWDC 教程创建粒子系统,但无法解决与性能相关的问题。

这是代码:

struct MyView: View {
    @State private var count = 32*32
    var body: some View {
        TimelineView(.animation) { timeline in
            Canvas { context, size in
                let now = timeline.date.timeIntervalSinceReferenceDate
                for i in 0..<count {
                    context.fill(Ellipse().path(in: CGRect(x: size.width*0.01*Double(i), y: size.height*0.01*Double(i), width: 20.0, height: 20.0)), with: .color(.green))
                }

            }
        }
    }
}

它只画了 1024 个圆圈,但已经消耗了大约 20% 的模拟器 CPU 和 50% 的我的 iPhone 8 CPU。考虑到 iPhone 的强大功能和新框架的有效性,这是预期的行为吗?如果我需要超过 1024 个圆圈,我应该如何解决这个问题?

标签: iosswiftswiftuiios15

解决方案


推荐阅读