I am trying to make pulse animation for an app I am making using swiftui. I want a center gray circle to pulse. Currently, the outside circles scale from 0 but I would like them to start from the middle circle size. This is my current code:
import SwiftUI
struct Pulse: View {
@State var animate = false
@State private var scale: CGFloat = 2
var body: some View {
ZStack
{
Circle()
.fill(Color.black)
.frame(width: 130, height: 130)
Circle()
.stroke(Color.red.opacity(0.1), lineWidth: 5)
.frame(width: 300, height: 300)
.scaleEffect(self.animate ? 1 : 0)
Circle()
.stroke(Color.red.opacity(0.15), lineWidth: 5)
.frame(width: 266, height: 266)
.scaleEffect(self.animate ? 1 : 0)
Circle()
.stroke(Color.red.opacity(0.2), lineWidth: 5)
.frame(width: 232, height: 232)
.scaleEffect(self.animate ? 1 : 0)
Circle()
.stroke(Color.red.opacity(0.3), lineWidth: 5)
.frame(width: 198, height: 198)
.scaleEffect(self.animate ? 1 : 0)
Circle()
.stroke(Color.red.opacity(0.35), lineWidth: 5)
.frame(width: 164, height: 164)
.scaleEffect(self.animate ? 1 : 0)
Circle()
.stroke(Color.red.opacity(0.4), lineWidth: 5)
.frame(width: 130, height: 130)
.scaleEffect(self.animate ? 1 : 0)
Circle()
.fill(Color.gray)
.frame(width: 130, height: 130)
}
.frame(width: 290, height: 290, alignment: .center)
.onAppear(perform: {
self.animate.toggle()
})
.animation(Animation.linear(duration: 6).repeatForever(autoreverses: true))
.ignoresSafeArea()
}
}
The ratio of the size of the inner circle to the outer circle is 130 / 300
. At its smallest, you want the circle with width and height of 300
to be 130
.
Change all of these:
.scaleEffect(self.animate ? 1 : 0)
to:
.scaleEffect(self.animate ? 1 : 130 / 300)