ios - Xcode 编译器错误:编译器无法在合理的时间内对该表达式进行类型检查(Xcode 12.0 SwiftUI)
问题描述
(Xcode 12.0,SwiftUI)这段代码为我工作了将近 6 个月没有任何问题我昨天刚收到这个错误,没有任何编辑。我尝试了很多来清理代码并使其更短,但对我没有任何帮助。
GeometryReader { (geometry: GeometryProxy) in
ForEach(0..<5) { index in
Group {
Circle()
.foregroundColor(Color("GreyOne"))
.frame(width: geometry.size.width / 5, height: geometry.size.height / 5)
.scaleEffect(!self.isAnimating ? 1 - CGFloat(index) / 5 : 0.2 + CGFloat(index) / 5)
.offset(y: geometry.size.width / 10 - geometry.size.height / 2)
}.frame(width: geometry.size.width, height: geometry.size.height)
.rotationEffect(!self.isAnimating ? .degrees(0) : .degrees(360))
.animation(Animation
.timingCurve(0.5, 0.15 + Double(index) / 5, 0.25, 1, duration: 1.5)
.repeatForever(autoreverses: false))
}
}.aspectRatio(1, contentMode: .fit)
.onAppear {
self.isAnimating = true
}
解决方案
您可以将您的部分内容移出View
到单独的私有函数中,然后从body
. 您的代码中有几个编译器错误,例如未传递x
给offset
函数或未将所有Int
s 转换为CGFloat
. 一旦你打破了body
你的View
,真正的错误就会浮出水面。
这是您的代码的编译版本,使用 2 个私有函数来构建body
您的View
.
struct YourView: View {
@State private var isAnimating = false
var body: some View {
GeometryReader { geometry in
ForEach(0..<5) { index in
Group {
circle(geometry: geometry, index: index)
}
.frame(width: geometry.size.width, height: geometry.size.height)
.rotationEffect(!self.isAnimating ? .degrees(0) : .degrees(360))
.animation(animation(index: index))
}
}
.aspectRatio(1, contentMode: .fit)
.onAppear {
self.isAnimating = true
}
}
private func animation(index: Int) -> Animation {
Animation
.timingCurve(0.5, 0.15 + Double(index) / 5, 0.25, 1, duration: 1.5)
.repeatForever(autoreverses: false)
}
private func circle(geometry: GeometryProxy, index: Int) -> some View {
let width = geometry.size.width
let height = geometry.size.height
let yOffset = width / 10 - height / 2
let nonAnimatingScale = 1 - CGFloat(index) / CGFloat(5)
let animatingScale = 0.2 + CGFloat(index) / CGFloat(5)
return Circle()
.foregroundColor(Color("GreyOne"))
.frame(width: width / 5, height: height / 5)
.scaleEffect(self.isAnimating ? animatingScale : nonAnimatingScale)
.offset(x: 0, y: yOffset)
}
}
推荐阅读
- python - 根据某些条件计算值并将其分配给新变量
- javascript - 类中数组的 Javascript 总和
- go - 如何在 Go 代码覆盖率计算中包含所有文件?
- r - 找到 Prophet 模型中变量的指定重要性?
- amazon-web-services - 如何计算/配置 EMR 上 Spark 的内存?
- sql-server - SQL 代理无法使用 WMI 事件观察器任务运行包
- javascript - 如何使 javascript 形状看起来与图像中的相同,以及如何在动画结束后清除 javascript 画布形状?
- php - 为什么会有 php session.gc_probability 和 session.gc_divisor 指令?
- javascript - 任何按键的 Javascript 反应时间测量(不是用鼠标点击)
- sql - 在 Postgres 上的关系表上聚合 Json 对象