[英]Animating a ProgressViewStyle inside NavigationView
Let's make a simple custom "infinite" spinner.让我们制作一个简单的自定义“无限”微调器。
SwiftUI provides ProgressView
as the prototypical container for this with ProgressViewStyle
as a means of customizing the way to present the progress. SwiftUI 提供ProgressView
作为原型容器, ProgressViewStyle
作为自定义呈现进度方式的手段。
To make our spinner infinitely spin, we'll use a linear animation, repeat it forever, start it at a rotation effect of 0 and move it to 360 degrees, as soon as the spinner appears.为了使我们的微调器无限旋转,我们将使用线性 animation,永远重复它,以 0 的旋转效果开始,并在微调器出现后立即将其移动到 360 度。
Simple!简单的!
Now let's put it inside a NavigationView
:现在让我们把它放在NavigationView
中:
We see the spinner bouncing up and down as it spins.我们看到微调器在旋转时上下弹跳。 Any clue what's going on or how to address the issue (while preserving a respect for the fundamentals, eg. ProgressView
to display progress and ProgressViewStyle
to customize it)?任何线索发生了什么或如何解决问题(同时保留对基本原理的尊重,例如ProgressView
显示进度和ProgressViewStyle
自定义它)?
struct SimplePreview: PreviewProvider, View {
static var previews = Self()
@State
private var isAnimating = false
var body: some View {
NavigationView {
ProgressView().progressViewStyle(SymbolicStyle())
}
}
public struct SymbolicStyle: ProgressViewStyle {
@State
private var isAnimating = false
public func makeBody(configuration: Configuration) -> some View {
Image(systemName: "circle.hexagonpath.fill")
.rotationEffect(self.isAnimating ? .degrees(360) : .zero)
.onAppear {
withAnimation(.linear(duration: 1).repeatForever(autoreverses: false)) {
self.isAnimating = true
}
}
}
}
}
You can replace .onAppear
with .task
in order to start the animation in a separate context where it does not get mixed up with other unreleated layout events.您可以将.onAppear
替换为.task
,以便在单独的上下文中启动 animation,这样它就不会与其他不相关的布局事件混淆。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.