Reputation: 3
(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. 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
}
Upvotes: 0
Views: 128
Reputation: 54706
You can move out parts of your View
into separate private functions and just call those from 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
. Once you break up the body
of your View
, the real errors will surface.
Here's the compiling version of your code, using 2 private functions to build parts of the body
of your 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)
}
}
Upvotes: 1