[英]Xcode Compiler error: The compiler is unable to type-check this expression in reasonable time (Xcode 12.0 SwiftUI)
(Xcode 12.0, SwiftUI) This code was working for me for almost 6 months without any problem I just got this error yesterday without any editing. (Xcode 12.0,SwiftUI)这段代码为我工作了将近 6 个月,没有任何问题我昨天刚收到这个错误,没有进行任何编辑。 I tried a lot to clean the code and make it shorter but nothing worked for me.我尝试了很多来清理代码并使其更短,但没有任何效果对我有用。
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
}
You can move out parts of your View
into separate private functions and just call those from body
.您可以将View
移出到单独的私有函数中,然后从body
调用它们。 You had a couple of compiler errors in your code, such as not passing x
to the offset
function or not converting all Int
s to CGFloat
.您的代码中有几个编译器错误,例如未将x
传递给offset
函数或未将所有Int
转换为CGFloat
。 Once you break up the body
of your View
, the real errors will surface.一旦分解了View
的body
,真正的错误就会浮出水面。
Here's the compiling version of your code, using 2 private functions to build parts of the body
of your View
.这是您的代码的编译版本,使用 2 个私有函数来构建View
body
的一部分。
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)
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.