滚动视图中的SwiftUI动画错误
我对SwiftUI比较陌生,我正在尝试在我的应用程序中实现动画加载器。它可以正常工作,直到我在scroll view中向下滚动我的内容,然后返回到触发拉刷新。圆圈开始跳跃,尽管它们应该一个接一个地增减大小 我的代码是滚动视图中的SwiftUI动画错误,swift,swiftui,ios-animations,Swift,Swiftui,Ios Animations,我对SwiftUI比较陌生,我正在尝试在我的应用程序中实现动画加载器。它可以正常工作,直到我在scroll view中向下滚动我的内容,然后返回到触发拉刷新。圆圈开始跳跃,尽管它们应该一个接一个地增减大小 我的代码是 struct ActivityIndicator: View { var isShowing: Bool @State private var shouldAnimate = false init(isShowing: Bool) { s
struct ActivityIndicator: View {
var isShowing: Bool
@State private var shouldAnimate = false
init(isShowing: Bool) {
self.isShowing = isShowing
}
var body: some View {
HStack(alignment: .center) {
Circle()
.fill(Color.mainAccent)
.frame(width: 20, height: 20)
.scaleEffect(shouldAnimate ? 1.0 : 0.5)
.animation(Animation.easeInOut(duration: 0.5).repeatForever())
Circle()
.fill(Color.mainAccent)
.frame(width: 20, height: 20)
.scaleEffect(shouldAnimate ? 1.0 : 0.5)
.animation(Animation.easeInOut(duration: 0.5).repeatForever().delay(0.3))
Circle()
.fill(Color.mainAccent)
.frame(width: 20, height: 20)
.scaleEffect(shouldAnimate ? 1.0 : 0.5)
.animation(Animation.easeInOut(duration: 0.5).repeatForever().delay(0.6))
}
.opacity(self.isShowing ? 1 : 0)
.onAppear {
self.shouldAnimate = true
}
}
}
我读了一些与我的案例相关的文章,似乎我可能不得不使用动画(显式)来代替。动画(隐式),但我无法使它正常工作
顺便说一句,我使用加载视图修改器将我的活动指示器连接到scrollView,它如下所示
struct LoadingView: ViewModifier {
@Binding var isShowing: Bool
func body(content: Content) -> some View {
ZStack(alignment: .center) {
content
.disabled(self.isShowing)
.blur(radius: self.isShowing ? 3 : 0)
ActivityIndicator(isShowing: isShowing)
}
}
}
任何想法和建议都很感激,我真的被卡住了。谢谢尝试将所有动画链接到相关状态,如
var body: some View {
HStack(alignment: .center) {
Circle()
.fill(Color.mainAccent)
.frame(width: 20, height: 20)
.scaleEffect(shouldAnimate ? 1.0 : 0.5)
.animation(Animation.easeInOut(duration: 0.5).repeatForever(),
value: shouldAnimate) // << here !!
var主体:一些视图{
HStack(对齐:。中心){
圈()
.fill(颜色.主重音)
.框架(宽度:20,高度:20)
.scaleEffect(应设置动画?1.0:0.5)
.animation(animation.easeInOut(持续时间:0.5).repeatForever(),
值:shouldAnimate)//如果您对上一个答案有任何问题,请尝试删除.animation并将其替换为withAnimation:
这样做的好处是它不会影响其他动画。非常感谢您,它可以正常工作!