简体   繁体   中英

How to disable position animation in SwiftUI?

I want to scroll a VStack programmatically and some of child views can be animated independently, but when changing the VStack 's offset the animated child view move to its final position witth animation, that's not what I want and how to disable animating this position changes? Here is my demo code,

import SwiftUI

struct ContentView: View {
    @State var angle:Double = 0
    @State var offset = 0
    var body: some View {
            LazyVStack {
                ForEach((0..<100)) { i in
                    if i == 25 {
                        Rectangle()
                            .frame(width: 30, height: 30)
                            .animation(.none)
                            .rotationEffect(.degrees(angle), anchor: .center)
                            .animation(Animation.linear(duration: 3).repeatForever(autoreverses: false))
                            .onAppear {
                                angle = 360
                            }
                    } else {
                        Text("number \(i)")
                    }
                }
            }
            .offset(y: CGFloat(offset))
            .animation(.none)
            .onAppear {
                offset = 200
            }
        
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

and the effect, 在此处输入图像描述

Try the following (tested in Xcode 12.0.1, iOS14):

Use an explicit animation call when setting the angle :

import SwiftUI

struct ContentView: View {
    @State var angle:Double = 0
    @State var offset = 0
    var body: some View {
        LazyVStack {
            ForEach((0..<100)) { i in
                if i == 25 {
                    Rectangle()
                        .frame(width: 30, height: 30)
                        .rotationEffect(.degrees(angle), anchor: .center)
                        .onAppear {
                            withAnimation(Animation.linear(duration: 3).repeatForever(autoreverses: false)) {
                                angle = 360
                            }
                        }
                } else {
                    Text("number \(i)")
                }
            }
        }
        .offset(y: CGFloat(offset))
        .onAppear {
            offset = 200
        }
        
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM