首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >LazyVStack滚动停止动画

LazyVStack滚动停止动画
EN

Stack Overflow用户
提问于 2020-10-02 09:04:47
回答 2查看 778关注 0票数 1

这段代码列出了带有刷新图标动画的100行。当使用LazyVStack而不是VStack时,在向下滚动到此列表的底部后,所有动画都会停止,包括列表顶部的动画。VStack没有此类问题。对于为什么会发生这种情况,有什么想法吗?有没有解决办法?使用Xcode12.0.1、iOS 14、Swift 5

代码语言:javascript
复制
struct RefreshingList : View {
    @State var isAnimating = false

    var body: some View {
        ScrollView {
            LazyVStack { // <- change to VStack to have animations not stop
                ForEach(0..<100, id: \.self) { i in
                    HStack {
                        Text("\(i) -> ")
                        self.button()
                    }
                }
            }
        }
        .onAppear {
            self.isAnimating=true
        }
    }
    
    func button() -> some View {
        Button(action: {}, label: {
            Image(systemName: "arrow.2.circlepath")
                .rotationEffect(Angle(degrees: self.isAnimating ? 360.0 : 0.0))
                .animation(Animation.linear(duration: 2.0)
                            .repeatForever(autoreverses: false))
        })
    }
}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2020-10-02 11:59:51

当观察到屏幕上视图的变化时,就会发生动画。在VStack中,所有视图都是立即创建的,因此SwiftUI能够观察到所有视图的旋转值的变化。

LazyVStack中,视图只在需要时创建,所以当.onAppear中的旋转值发生变化时,屏幕底部的视图不会出现。当它们出现在屏幕上时,它具有已经更改的值,因此不会发生动画。

解决此问题的一种方法是让按钮拥有.isAnimating @State变量。然后,任何时候创建一个,它将是动画。

代码语言:javascript
复制
struct RefreshingList : View {

    var body: some View {
        ScrollView {
            LazyVStack {
                ForEach(0..<100, id: \.self) { i in
                    HStack {
                        Text("\(i) -> ")
                        AnimatedButton()
                    }
                }
            }
        }
    }
}

struct AnimatedButton: View {
    @State var isAnimating = false
    
    var body: some View {
        Button(action: {
            
        }, label: {
            Image(systemName: "arrow.2.circlepath")
                .rotationEffect(Angle(degrees: self.isAnimating ? 360.0 : 0.0))
                .animation(Animation.linear(duration: 2.0)
                            .repeatForever(autoreverses: false))
        })
        .onAppear {
            isAnimating = true
        }
    }
}
票数 2
EN

Stack Overflow用户

发布于 2020-10-02 10:47:13

代码语言:javascript
复制
struct RefreshingListView: View {
    var body: some View {
        ScrollView {
            LazyVStack {
                ForEach(0..<100) {
                    LabelView(id: $0)
                        .id($0)
                }
            }
            .animation(
                Animation
                    .linear(
                        duration: 2)
                    .repeatForever(
                        autoreverses: false))

        }
    }
}

struct LabelView: View {
    @State var
        animate = false
    var id: Int
    var body: some View {
        Label(
            title: {
                Text("<- \(id)")
            }, icon: {
                Image(
                    systemName:
                        "arrow.2.circlepath")
                    .foregroundColor(.blue)
                    .padding(.horizontal)
                    .rotationEffect(
                        Angle(
                            degrees:
                                animate ?
                                360 : 0))
            }
        )
        .onAppear {
            animate
                .toggle()
        }
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64165008

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档