在 SwiftUI 图像上重复动画 [英] Repeating animation on SwiftUI Image
本文介绍了在 SwiftUI 图像上重复动画的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
给定以下 struct
:
struct PeopleList : View {
@State var angle: Double = 0.0
@State var isAnimating = true
var foreverAnimation: Animation {
Animation.linear(duration: 2.0)
.repeatForever()
}
var body: some View {
Button(action: {}, label: {
Image(systemName: "arrow.2.circlepath")
.rotationEffect(Angle(degrees: self.isAnimating ? self.angle : 0.0))
.onAppear {
withAnimation(self.foreverAnimation) {
self.angle += 10.0
}
}
})
}
}
我希望 Image
会顺时针旋转并重复直到 self.isAnimating
为 false
虽然它只动画一次.
I was hoping that the Image
would rotate clockwise and repeat until self.isAnimating
is false
although it's only animated once.
推荐答案
这里是出现和持续进步的可能解决方案开始/停止.使用 Xcode 11.4/iOS 13.4 测试.
Here is possible solution for continuous progressing on appear & start/stop. Tested with Xcode 11.4 / iOS 13.4.
struct PeopleList : View {
@State private var isAnimating = false
@State private var showProgress = false
var foreverAnimation: Animation {
Animation.linear(duration: 2.0)
.repeatForever(autoreverses: false)
}
var body: some View {
Button(action: { self.showProgress.toggle() }, label: {
if showProgress {
Image(systemName: "arrow.2.circlepath")
.rotationEffect(Angle(degrees: self.isAnimating ? 360 : 0.0))
.animation(self.isAnimating ? foreverAnimation : .default)
.onAppear { self.isAnimating = true }
.onDisappear { self.isAnimating = false }
} else {
Image(systemName: "arrow.2.circlepath")
}
})
.onAppear { self.showProgress = true }
}
}
这篇关于在 SwiftUI 图像上重复动画的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文